1

I want to change a div's class dynamically based on if an input value is true or false. Thought the function that validates the input is called the class is never changed. Here's the xhmtl:

<td>
    <div class="#{regBean.nameclass}">
    <h:inputText class="form-control input-sm" value="#{regBean.name}">
    <f:ajax event="blur" listener="#{regBean.validateName}" render="namemsg" />
    </h:inputText>
    </div>
    </td>
    <td class="error">
    <h:outputText  class="error-msg" id="namemsg" value="#{regBean.nameMsg}"></h:outputText>
</td>

and here's the function from the according bean:

private static final String SUCCESS="form-group has-success";
private static final String ERROR="form-group has-error";


public void validateName(){     
    if(ValidatorUtilities.empty(name)){
        nameMsg="Παρακαλώ εισάγετε όνομα!";     
    }
    nameMsg=ValidatorUtilities.validateName(name);

    if(nameMsg.equals("")){
        nameclass=SUCCESS;
        validations.put("name",true);
    }
    else{
        nameclass=ERROR;
        validations.put("name",false);
    }

}

thought the message at element with id="namemsg" is correctly shown the class remains the same. Is there anything wrong or am I going for the wrong implementation and should do this client side by jquery?

Note:I've set the getter-setter for the string nameclass

JmRag
  • 1,443
  • 7
  • 19
  • 56

1 Answers1

0

Your code is not re-rendering the <div>. Your code is only re-rendering the <h:outputText>. I'm not sure why and how exactly you expected that the <div> is also covered by the re-rendering. All you need to so is to specify its client ID in the render attribute as well. You only need to replace <div> by <h:panelGroup layout="block">, because rendering can only target true JSF components.

<td>
    <h:panelGroup id="field" layout="block" styleClass="#{regBean.nameclass}">
        <h:inputText ...>
            <f:ajax ... render="field namemsg" />
        </h:inputText>
    </h:panelGroup>
</td>
<td class="error">
    <h:outputText id="namemsg" ... />
</td>

Unrelated to the concrete problem, your whole validation approach is utterly broken. You're supposed to use a true Validator and a <h:message>, exactly like as shown in every decent JSF book. This is also a strong hint that you still don't have one. I warmly recommend to work on that first.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thx for your advice! If I do the validation the way suggested in the link you sent, I have to update the values of the bean by importing the bean to each validator file. Is it a good technique or should I find another approach? – JmRag Aug 19 '14 at 11:29
  • Why would you want to do that? Just work with the provided `value` argument. JSF will automatically update the model if validation succeeds. This all is also explained in a decent JSF book. – BalusC Aug 19 '14 at 11:30
  • Hence none of the books is free can you provide me a very brief example on how the values of bean are updated when a validator from a differt file? – JmRag Aug 19 '14 at 11:51
  • The value you'd like to validate is already offered as 3rd argument of `validate()` method. All you need to do is to throw `ValidatorException` is this value is not valid. See also the "see also" link. You can find links to online tutorials in http://stackoverflow.com/tags/jsf/info But nothing beats reading a good book. – BalusC Aug 19 '14 at 11:52
  • I've read thoroughly what you 've sent but I still cannot figure out how to it. Lets say that I have a bucking bean with an int foo=5; I want to modify it's value whether the validation succeeds or fails (eg foo=10; on success foo=5; on failure). The validator doesn't "know" the int foo unless I import the bean. So how am I going to handle this? – JmRag Aug 19 '14 at 12:07
  • Just make use of the fact that action(listener)s are **not** invoked when validation fails. You can just do the job in ``. It won't be invoked when validation fails. For future questions, please press `[Ask Question]` button. This all is unrelated to the concrete problem being asked in the current question. – BalusC Aug 19 '14 at 12:08
  • I made a new question ( http://stackoverflow.com/questions/25383552/jsf-correct-way-to-update-a-value-from-a-bean-depending-on-validation). If you have time check it. Thx again for all your time and effort! – JmRag Aug 19 '14 at 12:36