jsf - Adding validators to UIInputs outside of tag -
jsf - Adding validators to UIInputs outside of <h:inputText> tag -
is possible add together validators inputtext fields outside h:inputtext tag?
<!-- instead of --> <h:inputtext id="field1" value="#{backingbean.field1}"> <f:validator validatorid="customvalidator" /> </h:inputtext> <!-- --> <h:inputtext id="field1" value="#{backingbean.field1}" /> <abc:validations> <abc:validation for="field1" validator="customvalidatorname" /> </abc:validations>
the abc:validation short custom component
<cc:interface componenttype="validation"> <cc:attribute name="for" /> <cc:attribute name="validator" /> </cc:interface> <cc:implementation> </cc:implementation> </ui:composition>
and faces component class
package at.sozvers.ecm.webclient.frontend.component; import java.io.serializable; import javax.faces.component.facescomponent; import javax.faces.component.namingcontainer; import javax.faces.component.uicomponentbase; import javax.faces.component.uinamingcontainer; import javax.faces.validator.validator; @facescomponent("validation") public class validation extends uicomponentbase implements namingcontainer, serializable { private static final long serialversionuid = 1l; private string for1; private validator validator; @override public string getfamily() { homecoming uinamingcontainer.component_family; } public string getfor() { homecoming for1; } public void setfor(string for1) { this.for1 = for1; } public validator getvalidator() { homecoming validator; } public void setvalidator(validator validator) { this.validator = validator; } }
i read taghandlers , componenthandlers have no thought how start.
i found solution
i created class extending componenthandlerwrapper , override oncomponentpopulated method read viewmap , set validatorset each inputtext id has validated. validatorset contains set write facesvalidator names into.
public class parentaddingcomponenthandlerwrapper extends componenthandlerwrapper { @override public void oncomponentpopulated(faceletcontext ctx, uicomponent c, uicomponent parent) { super.oncomponentpopulated(ctx, c, parent); map<string, object> viewmap = ctx.getfacescontext().getviewroot().getviewmap(); if (c instanceof validation) { // validation facescomponent class <abc:validation> tagattribute uiinputname = getattribute("for"); if (null == viewmap.get(uiinputname.getvalue())) { viewmap.put(uiinputname.getvalue(), new validatorset()); } tagattribute validator = getattribute("validator"); // validator attribute contains facesvalidator name validatorset validators = (validatorset) viewmap.get(uiinputname.getvalue()); if (validator != null) { validators.addvalidator(validator.getvalue()); } } }
than created customcomponent i'm "decorating" inputtext. created componenttype/facescomponent named field annotated @listenerfor(systemeventclass = prevalidateevent.class) had override processevent method added facesvalidator uiinput component
@override public void processevent(componentsystemevent event) throws abortprocessingexception { super.processevent(event); map<string, object> viewmap = facescontext.getcurrentinstance().getviewroot().getviewmap(); string id = getattributevalue("id", ""); validatorset validators = (validatorset) viewmap.get(id); if (validators != null) { uicomponent findcomponent = findcomponent(id); if (findcomponent instanceof uiinput) { uiinput input = (uiinput) findcomponent; list<validator> inputvalidators = arrays.aslist(input.getvalidators()); (validator validator : validators.getvalidators()) { if (!doesuiinputcontainsvalidator(inputvalidators, validator)) { input.addvalidator(validator); } } } } } private boolean doesuiinputcontainsvalidator(list<validator> inputvalidators, validator validator) { (validator inputvalidator : inputvalidators) { if (inputvalidator.getclass().isinstance(validator)) { homecoming true; } } homecoming false; }
jsf jsf-2 jsf-2.2 custom-component
Comments
Post a Comment