2
votes

I have a picklist which requires a value to be provided every time for which I use required and I am using requiredMessage to display a message when a value is not entered. But both of them are not working for my picklist which is as shown below:

<p:pickList value="#{parameterValueDialog.selectedModelSeries}"
            var="item" itemValue="#{item}" required="true"
            converter="pickListConverter" requiredMessage="#{msg.validation_message}">

Does anyone have an idea as to how to solve this problem? Is this a problem of the Primefaces library itself?

EDIT: Posting getAsObject() method

@Override

public final Object getAsObject(final FacesContext ctx, final UIComponent component, final String string) {

Object returnObject = null;
Object dualList = ((PickList) component).getValue();
DualListModel<Item<?>> listModel = (DualListModel<NamedItem<?>>) dualList;
for (Item<?> item : listModel.getSource()) {
    String id = String.valueOf(item.getId());
    if (string.equals(id)) {
        returnObject = item;
        break;
        }
    }
    if (returnObject == null) {
        for (Item<?> item : listModel.getTarget()) {
            String id = String.valueOf(item.getId());
            if (string.equals(id)) {
                returnObject = item;
                break;
            }
        }
    }

return returnObject;

}

1
Show the getAsObject() of your converter. The problem might be caused in there. It might incorrectly have returned non-null for an empty string value. - BalusC
@BalusC - Included code. - Rajath

1 Answers

2
votes

You can solve your problem by providing a validator method for use in your pickList.

In my case, my target list contains members names and I must ensure that the list contains at least one member and that the first member has a special status. You first have to cast the object return by the pickList into a DualListModel and get a hand on the target list. You can then check if it is empty and perform any other validation needed. Here is my validator method :

public void valideLocationsResponsables(FacesContext contexte, UIComponent composant, Object locationResps) throws ValidatorException {
    DualListModel<LocationsResponsables> dualListModel;
    if (locationResps instanceof DualListModel) {
        dualListModel = (DualListModel<LocationsResponsables>) locationResps;
        List<LocationsResponsables> listeResponsables = dualListModel.getTarget();
        ListIterator<LocationsResponsables> iterateur = listeResponsables.listIterator();
        // Your validation criteria here
        if (listeResponsables.isEmpty() || !iterateur.next().getIdMembreReserv().getStatut().isPeutReserver()) {
            ((UIInput) composant).setValid(false); // Pick list is made invalid
            FacesMessage msgErreur = new FacesMessage();
            msgErreur.setSeverity(FacesMessage.SEVERITY_ERROR);
            msgErreur.setDetail("Your error message here");
            throw new ValidatorException(msgErreur);
        }
    }
}

The validator method is hooked into the pickList with the validator keyword as follows :

        <p:pickList id="listeResp" value="#{locationsControleur.locationsResponsablesListe}" var="resp" effect="puff"
                            itemValue="#{resp}" itemLabel="#{resp.idMembreReserv.prenomNom}"
                            converter="#{locationsResponsablesListeConvertisseur}"
                            validator="#{locationsControleur.valideLocationsResponsables}">
                <f:facet name="sourceCaption">#{msgs.LocationsMembresEligibles}</f:facet>
                <f:facet name="targetCaption">#{msgs.LocationsResponsablesChoisis}</f:facet>
        </p:pickList>
                <p/><p/>
                <h:message id="listeRespMsg" for="listeResp" errorStyle="color: red"/>

Hope this can help.