validation - How to detect valueChange event in validator? -


mojarra 2.1

i need write validator h:inputtext performs logic if value input changed. i.e.

public class myvalidator implements validator{      public void validate(facescontext context,                          uicomponent component,                          object value) throws validatorexception;          if(valuechanged(uicomponent component)){ //the method checks if value's changed                //do piece of logic          }          return;      } } 

i dug queuing events of uiinput , found this:

validatevalue(context, newvalue);  // if our value valid, store new value, erase // "submitted" value, , emit valuechangeevent if appropriate if (isvalid()) {         object previous = getvalue();         setvalue(newvalue);         setsubmittedvalue(null);         if (comparevalues(previous, newvalue)) {             queueevent(new valuechangeevent(this, previous, newvalue));         }     } 

this piece of code method, executed validation phase callback. first thought popped head queriyng events fired during handling request. method queueevent(facesevent) implemented follows:

 public void queueevent(facesevent event) {      if (event == null) {         throw new nullpointerexception();     }     uicomponent parent = getparent();     if (parent == null) {         throw new illegalstateexception();     } else {         parent.queueevent(event);     }  } 

therefore every such invokation end in uiviewroot.queueevent(facesevent) implemented as:

public void queueevent(facesevent event) {      if (event == null) {         throw new nullpointerexception();     }     // uiviewroot, no need check ise     if (events == null) {         int len = phaseid.values.size();         list<list<facesevent>> events = new arraylist<list<facesevent>>(len);         (int = 0; < len; i++) {             events.add(new arraylist<facesevent>(5));         }         this.events = events;     }     events.get(event.getphaseid().getordinal()).add(event); } 

which means, events stored list<list<facesevent>> each phase. list<list<facesevent>> events private field, it's impossible direct acces it.

another thing actual validation being perfromed before quingevent, implemting valuechangelistener doesn't seem useful well.

question: possible implements such validator in jsf in natural way?

just value comparison yourself. in validator, old value readily available via uicomponent argument.

@override public void validate(facescontext context, uicomponent component, object submittedvalue) {     if (component instanceof editablevalueholder) {         object newvalue = submittedvalue;         object oldvalue = ((editablevalueholder) component).getvalue();          if (newvalue == null ? oldvalue == null : newvalue.equals(oldvalue)) {             return; // not changed, skip validation.         }     }      // actual validation here. } 

if happen use jsf utility library omnifaces, has valuechangevalidator this.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -