Note that there are some explanatory texts on larger screens.

plurals
  1. PO<f:validator disabled flag not working
    primarykey
    data
    text
    <p>I've created a custom validator for my project, it simply checks the select ones value and 'validates' the value is not '0'. We have a standard (I'm sure not uncommon) of manually setting the first value of our selectOneMenu compents to:</p> <pre><code>&lt;f:selectItem itemValue="0" itemLabel="-- Select One --"/&gt; </code></pre> <p>Which works fine, but then makes the component always pass the required check. So this validator simply treats this value as if there was no selection made. </p> <p>SelectOneMenu example:</p> <pre><code>&lt;h:selectOneMenu id="eligibility" value="#{reg.eligibility}" required="#{reg.fieldsRequired}"&gt; &lt;f:selectItem itemValue="0" itemLabel="-- Select One --"/&gt; &lt;f:selectItems value="#{reg.eligibilityList}" /&gt; &lt;f:validator validatorId="selectOneValidator" disabled="#{!reg.fieldsRequired}"/&gt; </code></pre> <p>Custom Validator:</p> <pre><code>@FacesValidator("selectOneValidator") </code></pre> <p>public class SelectOneValidator implements Validator {</p> <pre><code>@Override public void validate(FacesContext context, UIComponent uiComponent, Object o) throws ValidatorException { String val = null; if (uiComponent instanceof HtmlSelectOneMenu) { HtmlSelectOneMenu oneMenu = (HtmlSelectOneMenu) uiComponent; if (oneMenu.isRequired() &amp;&amp; !oneMenu.isDisabled()) { if (o instanceof String) { val = (String) o; } else if (o instanceof Number) { val = String.valueOf(o); } if ("0".equals(val)) { FacesMessage msg = new FacesMessage(); msg.setSummary("Please select a value from the list."); msg.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ValidatorException(msg); } } } } </code></pre> <p>}</p> <p>This has worked just fine thus far, however, the current use-case I'm running into trouble. My page has multiple SelectOneMenus one of which toggles the required and disabled attributes via an ajax call for the page. I'm not having any issues with the <code>required="#{reg.fieldsRequired}"</code>, however; the <code>disabled="#{!reg.fieldsRequired}"</code> attribute on my custom validator does not seem to make a difference. I'm just thinking out loud, but when the page first loads the <code>#{reg.fieldsRequired}</code> expression is false. If I then change the SelectOneMenu to set this boolean value to true, then press the submit button, the disabled attribute doesn't seem to have been set. I wondered if this is simply a ajax issue and that all of components simply needed to be re-renderd so I added the @form in my ajax call: <code>&lt;a4j:ajax render="@form" listener="#{reg.saveActionChanged}"/&gt;</code> in hopes that would fix the problem, but no difference in the behavior. </p> <p>Environment:<br> JSF 2.0.3<br> Tomcat 6.0.14<br> ajax call being made with RichFaces 4.0 </p> <p>Any help is most appreciated! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload