1

I have a JSF component which is mapped on a List in the corresponding bean. As I want the mapped field to contain at least one value when the form is submitted, this field is annoted @NotEmpty.

Bean :

public class MyBean {

    @NotEmpty(message="Validation failed")
    private List<String> selectedOptions = new ArrayList<>();

    ... getter & setter

}

Each time the user select something on my component I want the selection to be immediatly mapped in the bean, so I add an ajax behaviour to the component (Here the component is a p:selectManyMenu but the issue seems to be present with any component mapped to a List) :

XHTML :

<h:form>
    <p:outputLabel for="optionslist" value="Options :" />
    <p:selectManyMenu id="optionslist" value="#{myBean.selectedOptions}" style="margin-bottom:50px">
        <f:selectItem itemLabel="Option 1" itemValue="1" />
        <f:selectItem itemLabel="Option 2" itemValue="2" />
        <f:selectItem itemLabel="Option 3" itemValue="3" />
        <p:ajax process="@this" update="result optionsListMessage" /> 
    </p:selectManyMenu>

    <p:commandButton value="Submit" update="result optionsListMessage" />

    <p:message for="optionslist" id="optionsListMessage" />

    <p:dataList id="result" value="#{myBean.selectedOptions}" var="option">
        <h:outputText value="#{option}" />
    </p:dataList>
</h:form>

My issue happens in the following situation :

  1. The user selects one or several choices (Ctrl + click here).
  2. He unselects every choices he just selected.
  3. He submits the form.
  4. We can see in the result dataList that the last unselected value is still in the bean.

My understanding of the situation is that when the user unselects the last value, the validation fails because of the @NotEmpty annotation on the field (as confirmed by the p:message validation failure message). As a consequence the setter is not called and the last unselected value remains in the bean.

How can I, in a proper way, allow the user to unselect every items without validation failures, and run the validator on this field only when the form is submitted ?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Tom
  • 345
  • 4
  • 13
  • i copy and past and it work for me. Can you try to refresh page. by Using Ctrl+F5. – ankush yadav Jun 01 '16 at 07:15
  • @ankushyadav It works even when you deselect every choices and submit the form ? In my case the `datalist` will contain the last unselected value. – Tom Jun 01 '16 at 07:34
  • how did you check that your datalist have last unselected value. when i check is show size zero. – ankush yadav Jun 01 '16 at 08:25
  • When I submit the form the datalist is updated with the current content of the selectedOptions field. – Tom Jun 01 '16 at 08:37
  • I think we are not in same page correct me if i am wrong. in last comment you want when user unselect the last choice from list, that time below datalist should not display any thing am i right? I checked my code is working fine i am getting list size zero, when i submit the form. can you explain more about your problem and put more log, that can help. Thanks Ankushy – ankush yadav Jun 01 '16 at 10:14
  • If you want to update your datalist on UI(VIEW), you can use this will update the datalist. – ankush yadav Jun 01 '16 at 10:21
  • Thanks, I've updated the code with your suggestion and the update of a validation message on each action. I guess you're right, not sure we're discussing about the same scenario ;) – Tom Jun 01 '16 at 11:23
  • If you select option 1 and then unselect it (Ctrl+click), do you have a "validation failed" message on the UI and is the selectedOptions setter called in the bean ? – Tom Jun 01 '16 at 11:23
  • yes, my setter method get call each time, when i select it call with one value in list, when unselect it call with zero value. validation take place only when you submit the form. and i am getting error message on UI, when i am submit the form. better to update your form on submit button. – ankush yadav Jun 01 '16 at 11:53
  • It's weird, my setter is called each time except when I unselect the last option, which seems to be normal behaviour because of the validation failure. The behaviour you describe is the one I want but I really don't understand how you pass the validation. – Tom Jun 01 '16 at 12:14
  • this link may work for you [link](http://stackoverflow.com/questions/8677985/primefaces-jsf-update-after-validation-failed-doesnt-work) – ankush yadav Jun 01 '16 at 13:13

0 Answers0