We have a multi field validator with field across two DIVs ,if we try to bind value across the fields and pass it as attributes to one of the validator ,it doesn’t get the submitted value….The field phone 3 got a validator ,but when it goes to validator the submitted value is coming as null.
I have three divs “phone1” , “phone2” , “phone3” ,I would like to validate in the field “phone3” across these three fields. The validator doesn’t get the submitted values of left and middle fields .I added the extra input field “confirm” inside the div “phone3” and the validtor gets the value of the “confirm” field
<div class="home_phone">
<div id="phone1" >
<div class="left"></div>
<div class="middle">
<h:inputText id="phonePrefix"
maxlength="4" value="#{phoneNumberTO.phonePrefix}"
immediate="true"
binding="#{phonePrefix}">
</h:inputText>
</div>
<div class="right"></div>
</div>
<div id="phone2" >
<div class="left"></div>
<div class="middle">
<h:inputText id="phoneAreaCode"
maxlength="4"
immediate="true"
binding="#{phoneAreaCode}">
</h:inputText>
</div>
<div class="right"></div>
</div>
<span class="sep_field">-</span>
<div id="phone3" >
<div class="left"></div>
<div class="middle">
<h:inputText id="input3"
maxlength="4" value="#{phoneNumberTO.phoneSuffix}"
>
<f:validator validatorId="phoneValidator" />
<f:attribute name="phonePrefix" value="#{phonePrefix}" />
<f:attribute name="phoneAreaCode" value="#{phoneAreaCode}" />
<f:attribute name="confirm" value="#{confirm}" />
</h:inputText>
<h:inputSecret id="confirm" binding="#{confirm}" required="true" />
</div>
<div class="right"></div>
</div>
</div>
No, this is not what
immediate="true"is for. Remove it.In JSF, components are processed and converted/validated in the order as they appear in the JSF component tree. Every single input component is step-by-step converted and validated during Validations Phase as follows:
UIInput#getSubmittedValue()UIInput#setValue()and set the component’s submitted value tonullbyUIInput#setSubmittedValue().falsetoUIInput#isValid(). Note that the submitted value is kept on the component!In the order as you have it in your view, with the validator on the last component, you should be collecting values of other inputs by
UIInput#getValue()instead ofUIInput#getSubmittedValue(), otherwise you will indeed keep gettingnulls when the validation has succeed for them.As of now I can’t explain why it worked when you have put them outside the divs. I think that you have actually accidently rearranged the order of the components.
As to the proper usage of
immediate="true"attribute, it should help if you understand what exactly it does. Here’s an extract of relevance from the debug JSF lifecycle article: