LoginSignup
0
0

More than 1 year has passed since last update.

JSF入力チェック

Last updated at Posted at 2023-01-28

bootstrap-5-jbvalidator

image.png

.html
<h:inputText id="userId" value="#{loginManagedBean.userId}" 
  p:required="true" p:data-v-min-length="2" p:data-v-max-length="10"
  styleClass="form-control" />
.js
$(function () {

    let validator = $('form.needs-validation').jbvalidator({
        errorMessage: true,
        successClass: true,
        language: 'static/lib/bootstrap-5-jbvalidator/lang/ja.json'
    });

    $("form").on('submit', function (e) {
        console.log('validator.checkAll()', validator.checkAll());
        if(validator.checkAll() > 0){
            // チェックエラー時はsubmitをキャンセル
            return false;
        }
    });

});

bean validation

image.png

.html
<h:inputText id="userId" value="#{loginManagedBean.userId}" 
  styleClass="form-control" />
<h:message for="userId" styleClass="text-danger"></h:message>
.java
@Named
@ViewScoped
@Getter
@Setter
public class LoginManagedBean implements Serializable {

    /**
     * ユーザID
     */
    @NotEmpty
    @Min(5)
    private String userId;

業務ロジックチェック

image.png

.html

<h:messages globalOnly="true" style="list-style:none" 
  styleClass="my-2 px-2" errorClass="alert alert-danger" 
  infoClass="alert alert-info" />
.java
FacesMessage errmsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
facesContext.addMessage(null, errmsg);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0