JSF validate on submit form

前端 未结 1 477
轮回少年
轮回少年 2020-12-11 11:22

I\'m working on a JSF 2.0 form, I have a managedbean with 2 fields

import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.Reque         


        
1条回答
  •  有刺的猬
    2020-12-11 11:52

    Here's one of the ways:

    
    
        
            
        
        
            
            
        
        
    
    

    with

    @FacesValidator(value="dateValidator")
    public class DateValidator implements Validator {
    
        @Override
        public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
            UIInput sd = (UIInput)component.getAttributes().get("firstDate");
            Date firstDate = (Date)sd.getValue();
            Date secondDate = (Date)value;
            if(!firstDate.before(secondDate)){
                FacesMessage msg = new FacesMessage("Entered dates are invalid: first date must be before second date");
                throw new ValidatorException(msg);
            }
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题