2

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.RequestScoped;

@ManagedBean
@RequestScoped
public class StackOverflow {

    private Date firstDate;
    private Date secondDate;

    public void submit(){
       //validate here and show error on form
    }

}

and the xhtml like:

<h:inputText value="#{stackOverflow.firstDate}">
    <f:convertDateTime pattern="d/M/yyyy" />
</h:inputText>
<h:inputText value="#{stackOverflow.secondDate}">
    <f:convertDateTime pattern="d/M/yyyy" />
</h:inputText>

<h:commandLink action="#{stackOverflow.submit}">
    <span>Submit</span>
</h:commandLink>

I want to validate the first and second date that the second date is not before the first date

  • Possible duplicate:http://stackoverflow.com/questions/16162554/mutually-restricting-begin-and-end-date-times-using-pcalendar-no-validation – Rong Nguyen Apr 24 '13 at 07:49
  • JSF utility library OmniFaces has an `` for the exact purpose: http://showcase.omnifaces.org/validators/validateOrder – BalusC Apr 24 '13 at 12:21

1 Answers1

4

Here's one of the ways:

<h:messages globalOnly="true"/>
<h:form>
    <h:inputText value="#{stackOverflow.firstDate}" binding="#{firstDate}">
        <f:convertDateTime pattern="d/M/yyyy" />
    </h:inputText>
    <h:inputText value="#{stackOverflow.secondDate}" validator="dateValidator">
        <f:attribute name="firstDate" value="#{firstDate}" />
        <f:convertDateTime pattern="d/M/yyyy" />
    </h:inputText>
    <h:commandButton value="Submit" action="#{stackOverflow.submit}"/>
</h:form>

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);
        }
    }

}
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • how did you get the other value for the firstdate ?? – Mohamed Wagdy Khorshid Apr 24 '13 at 10:59
  • 1
    Added corresponding `` of first date as an attribute of another `` and later accessed it in `validate` method. Really worth reading is [JSF2.0 doesn't support cross-field validation, is there a workaround?](http://stackoverflow.com/a/6282509/1820286). – skuntsel Apr 24 '13 at 11:15
  • this does not work for me except with direct values .. binding is not working – Mohamed Wagdy Khorshid Apr 24 '13 at 11:46
  • What do you mean by 'except withdirect values?' – skuntsel Apr 24 '13 at 11:51
  • it worked if I put value="hello" but not with value=#{helloField} that's beacuse I didn't write the binding="#{....}" in the target field – Mohamed Wagdy Khorshid Apr 24 '13 at 12:30
  • I have some issues using this method .. as binding with components sometimes retreives older values according to the order of the bound field relative to the target field .. I wish if you can tell if validating the form in the submit method in the back bean is a good idea and if so could you give some samples ?? – Mohamed Wagdy Khorshid Apr 24 '13 at 13:10