0
votes

I use this class for doing validation from input fields:

@ManagedBean
@RequestScoped
public class UserInputValidation {

    public void validateCity(FacesContext context, UIComponent validate,
            Object value) {
        String inputFromField = (String) value;     

        if (inputFromField.equals("") || inputFromField.equals(" ")) {
            FacesMessage msg = new FacesMessage("Odaberite grad");
            throw new ValidatorException(msg);
        }
    }
//...
}

And this is the managed bean that holds the inputed values:

@ManagedBean
@RequestScoped
public class InputController {

    //Attributes
    private String city;
   //Get set methods

Why when i create a selectOneComponent and i select the first component(blank input), the validation message is not shown?

<h:selectOneMenu id="city" value="#{inputController .city}">                
                <f:selectItems value="#{searchController.formatedCities()}" validator="#{userInputValidation.validateCity}"/>
            </h:selectOneMenu>
            <span style="color: red;"><b><h:message for="city"
                    showDetail="true" /></b></span>

The first of the fields in the selectOneMenu is a blank(The formatedCitiesMethod() returns a "" as first element), so why the validation message is not being shown if the form is submit button is clicked with the blank selected?

1

1 Answers

3
votes

The validator attribute has to go in the <h:selectOneMenu>, not in the <f:selectItems>

Said that, why don't you just use required="true"? Why is the validator a @ManagedBean instead of a @FacesValidator?