1

I'm having a trouble with getting rid of required message. I have a form in which I have a few fields and a button. When I press a button there is validation that checks if required fields where filled with values if not then required message is displayed for invalid value/component. Now I want to select a value from selectOneMenu or type something into inputText and when I do that I want the required message to dissapear without need to press the button again.

How would you do that? I've tried to remove message with sth like this, but it doesn't seems to work:

Iterator<FacesMessage> msgIterator = FacesContext.getCurrentInstance().getMessages();
while (msgIterator.hasNext())
{
    FacesMessage facesMessage = msgIterator.next();
    msgIterator.remove();
}

Could you help me with that?

Here is example code:

<h:form id="mainForm">
<h:selectOneMenu required="true" id="dictionaryValueId" value="#{SomeBean.dictionarySelectedValue}">
    <f:selectItem itemValue="#{null}" itemLabel="#{i18n['view.choose']}" />
    <f:selectItems value="#{SomeBeanBean.dictionaryValuesMap}" var="element"
        itemLabel="#{element.descripption}" itemValue="#{element.key}" />
    <f:ajax event="change" execute="@this msgId" render="msgId dictionaryValueId"/>
</h:selectOneMenu>
<h:message id="msgId" style="display:none;" for="dictionaryValueId" />
...

<h:commandButton value="#{i18n['button.forward.name']}"
        actionListener="#{SomeBean.forward}" >
        <p:ajax process="@form" update="mainForm"/>
</h:commandButton>

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Emil Smęt
  • 819
  • 3
  • 13
  • 28
  • does it work with an `f:ajax`? if not, it is not PrimeFaces related (which I think it is not) – Kukeltje Nov 25 '15 at 10:29
  • It would work without primefaces, but ajax attached to the commandButton is not the problem. the problem is how to make required message dissapear without using commandButton when value is changed. – Emil Smęt Nov 25 '15 at 10:55
  • I know, but I asked in relation to the tagging.. And just the jsf tag would have been enough, or jsf-2 ;-). Checked this: https://stackoverflow.com/questions/32400491/ajax-reset-value-when-validation-error-happens and http://stackoverflow.com/questions/6642242/how-can-i-populate-a-text-field-using-primefaces-ajax-after-validation-errors-oc/ – Kukeltje Nov 25 '15 at 11:02
  • Ok, I'll check this links and let you know what was the result. – Emil Smęt Nov 25 '15 at 11:20
  • So you're basically saying that `` didn't work? – BalusC Nov 25 '15 at 12:22
  • Yes. I've tried https://stackoverflow.com/questions/32400491/ajax-reset-value-when-validation-error-happens as sugested by @Kukeltje and it didn't work for me. I'll try solution from the second link. – Emil Smęt Nov 25 '15 at 12:42

2 Answers2

1

I am not sure, but is not there a problem with style="display:none;"
for <h:message id="msgId"/>

delovepr
  • 71
  • 1
  • 1
  • 6
0

You can wrap your message with <h:panelGroup/> and render by this panelGroup Id, this Id will be always present on your form.

<h:form id="mainForm">
   <h:selectOneMenu required="true" id="dictionaryValueId" value="#{SomeBean.dictionarySelectedValue}">
    <f:selectItem itemValue="#{null}" itemLabel="#{i18n['view.choose']}" />
    <f:selectItems value="#{SomeBeanBean.dictionaryValuesMap}" var="element" itemLabel="#{element.descripption}" itemValue="#{element.key}" />
    <f:ajax event="change" execute="@this" render="messageBundle1 dictionaryValueId"/>
   </h:selectOneMenu>
   <h:panelGroup id="messageBundle1">
      <h:message id="msgId" style="display:none;" for="dictionaryValueId" />
   </h:panelGroup>

<h:commandButton value="#{i18n['button.forward.name']}"
        actionListener="#{SomeBean.forward}" >
        <p:ajax process="@form" update="mainForm"/>
</h:commandButton>
</h:form>
delovepr
  • 71
  • 1
  • 1
  • 6
  • in case you trying to render unrecognised Id(msgId) which is not presented on form due to style="display:none;" you can not manipulate with this component. So to fix this problem you need the component which you can manipulate always. ** ... ** solve this issue due to always present on page. – delovepr Nov 30 '15 at 08:27