JSF skip Required-Validation without immediate=true

后端 未结 6 1304
余生分开走
余生分开走 2020-12-04 12:11

I have multiple forms, where I have mandatory fields and optional fields.

To submit such a form I require the validation on the required-attribute to be exe

6条回答
  •  抹茶落季
    2020-12-04 12:47

    if you want to skip validation when click on button then easly add parameter to button where you want to skip it. Example:

    
       
    
    

    Then in validator you can read this parameter and if it is true then skip it:

    @FacesValidator("com.validators.MyValidator")
    public class MyValidator implements Validator{
    
      public void validate(FacesContext ct, UIComponent co, Object obj) throws ValidatorException { 
        if(!continueValidation()){
          return;
        }
        // validation process
      }
    
    protected boolean continueValidation() {
        String skipValidator= FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("skipValidator");
        if (skipValidator != null && skipValidator.equalsIgnoreCase("true")) {
          return false;
        }
        return true;
      } 
    }
    

提交回复
热议问题