6
votes

Is there any way I can validate a jsf input text that is readonly but the value is changed upon some other events triggered?

4
Woudn't it be easier to validate it in the jsf page backing bean? - Richie
And how do I trigger the validation and issue the validation message? - Alina Danila
I have added an answer as reply to the comment. Cheers - Richie
Are you changing the value by JavaScript? What exactly is the functional requirement? - BalusC
@BalusC the first component, the inputText, is displaying some value that is modified by a second component, which is more complex. I did not want to make the validation on the second component, because it would have been more complicated. - Alina Danila

4 Answers

14
votes

Set the readonly attribute to true only during render response phase. This way it won't be seen as readonly in all other phases than the render response phase.

<h:inputText ... readonly="#{facesContext.currentPhaseId.ordinal eq 6}" />
2
votes

Answering from comment: "Woudn't it be easier to validate it in the jsf page backing bean?" You can do application-level validation on a button click or some similar event. You can do something like this in your backing bean and link it to the event.

public String login(){
        FacesContext context = FacesContext.getCurrentInstance();
        if(**<<some validation for value in field 'firstName' in form 'userForm'>>**){
            FacesMessage message = new FacesMessage();
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            message.setSummary("Some Valication check");
            message.setSummary("Some Valication check!!");
            context.addMessage("userForm:firstName", message);//adds validation message for field firstName in form userForm
            return "ERROR";
        }
        return "SUCCESS";
    }

Hope this helped.

2
votes

I have solved this problem with a workaround. I have added an inputHidden field that has the required attribute true that will not appear on the interface, but will be validated:

<h:inputHidden value="#{bean.value}" required="true"
                    requiredMessage="Data must be entered" />

The value of bean.value is changed by other event, and at some reRender on the inputHidden, the validation takes place.

1
votes

You will most likely have to manually call the validation logic directly from your event. Here is a model of the JSF lifecycle from IBM.

enter image description here

You will notice that Process Validations phase occurs before the Invoke Application phase where events are typically handled. This occurs well after so validation will not occur automatically.