converter - JSF Save value of input before conversion -
i have input field user enter social security number(userid). number has in format use custom converter format correctly. later number checked against db. when check fails, want number displayed way user entered ux reasons. conversion before check , userid in backing bean set converted value, original number lost. best way save original value?
<h:inputtext id="userid" value="#{bean.userid}"> <f:converter converterid="idconverter" /> </h:inputtext>
if understand problem correctly not need save original value. take advantage of jsf lifecycle.
in addition custom converter, need custom validator. in validator, if check between converted input , data on db successful getasstring
of custom converter return converted input. however, if conversion succeeds validation fails (meaning check against db record unsuccessful) throw validatorexception. getasstring
not called , raw input displayed.
there 2 ways can think of on how can achieve this. first approach, can define validator method within bean , move validation logic in method. example :
public class bean { //remainder omitted public void validate(facescontext fc, uicomponent uic, object o) { //get converted input. assuming of type string string convertedinput = (string) o; //move db check in method. if fails throw //a validatorexception below. if succeeds, don't return //throw new validatorexception(new facesmessage("validation failed")); } }
be mindful of return type , parameters.
then add validator
attribute in <h:inputtext>
<h:inputtext id="userid" value="#{bean.userid}" validator="#{bean.validate}">
one advantage approach method able access other instance variables of bean
class downside, less portable.
another approach have separate class implement validator interface. example:
@facesvalidator("customvalidator") public class myvalidator implements javax.faces.validator.validator { @override public void validate(facescontext fc, uicomponent uic, object o) { //same logic bean validator method } }
then add in <h:inputtext>
<f:validator validatorid="customvalidator" />
you problem seems indicate lack of understanding of jsf lifecycle. highly suggest take time understand concept as possible. might not understand in 1 sitting more involved jsf, aspects become clearer. here's place start.
Comments
Post a Comment