jsf (richfaces) readonly input text validation [du

2019-01-26 13:01发布

问题:

This question already has an answer here:

  • Force JSF to process, validate and update readonly/disabled input components anyway 3 answers

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

回答1:

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:

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.



回答3:

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.



回答4:

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.

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.