validation - How to get the value of another component in a custom validator? -
this question has answer here:
i use custom validator. difficulty need check 2 fields inputtext , compare them. first field must greater second field. if not, have show message error information. need pass in custom validator value of first inputtext field. need read value of first inputtext field in validator class. how can id of necessary component in validator class? solution using tag not suit me. need go desired component directly maybe can done through methods of facescontext?
just pass whole component via <f:attribute>.
<h:form id="formid"> <h:inputtext value="#{bean.start}"> <f:validator validatorid="rangevalidator" /> <f:attribute name="endcomponent" value="#{endcomponent}" /> </h:inputtext> ... <h:inputtext binding="#{endcomponent}" value="#{bean.end}" /> ... </h:form> (note: binding code as-is, not let refer bean property!)
with in validator
uiinput endcomponent = (uiinput) component.getattributes().get("endcomponent"); object endcomponentvalue = endcomponent.getsubmittedvalue(); // ... important note components processed, converted , validated in order appear in tree. submitted value of components aren't converted/validated yet available uiinput#getsubmittedvalue() , of converted/validated available uiinput#getvalue(). so, in particular example, should value uiinput#getsubmittedvalue() instead of uiinput#getvalue().
if you'd work converted , validated value available uiinput#getvalue(), need move validator second component , pass first component along instead.
<h:form id="formid"> <h:inputtext binding="#{startcomponent}" value="#{bean.start}" /> ... <h:inputtext value="#{bean.end}" /> <f:validator validatorid="rangevalidator" /> <f:attribute name="startcomponent" value="#{startcomponent}" /> </h:inputtext> ... </h:form> uiinput startcomponent = (uiinput) component.getattributes().get("startcomponent"); object startcomponentvalue = startcomponent.getvalue(); // ...
Comments
Post a Comment