Is there a way to update component even when JSF fail during Process Validation phase?

You want to use a validator instead of an action listener.

Up vote 2 down vote favorite share g+ share fb share tw.

I have an input text that have required=true attribute, like below So my requirement is, if the value is empty, then it will display Required, since required=true, it should fail at Process Validation phase. If the value is unique, then display a check image, if duplicated, then display Duplicated text. The problem that I run into is, after I type something and tab away (let say I type something unique), it displays the check image, I then erase the text, and tab away again, now the Required text appear, but so is the check image.

My theory is that, at the Process validation phase, it fail due to the value is empty, so at the update component phase, it does not invoke that method handleLooseFocusCCMTextbox() that will set the boolean ccmNameUnique to false. Is there a way to fix this? NOTE: handleLooseFocusCCMTextbox() just turn the boolean value on and off to display the check image or Duplicated text.

Answered. Create Validator class, take out required=true public void validate(FacesContext fc, UIComponent uic, Object value) throws ValidatorException { FacesContext context = FacesContext. GetCurrentInstance(); SetupView setupView = (SetupView) context.getApplication().

EvaluateExpressionGet(context, "#{setupView}", SetupView. Class); if (value == null || value.toString().isEmpty()) { setupView. SetCcmNameUnique(false); FacesMessage message = new FacesMessage(); message.

SetSeverity(FacesMessage. SEVERITY_ERROR); message. SetSummary("Error"); message.

SetDetail("Required"); //This will end up in throw new ValidatorException(message); } String rootPath = setupView.getRootPath(); File rootFolder = new File(rootPath); if (rootFolder.exists() && rootFolder.canRead()) { List folderNames = Arrays. AsList(new File(rootPath).list()); if (folderNames. Contains(value.toString())) { setupView.

SetCcmNameUnique(false); FacesMessage message = new FacesMessage(); message. SetSeverity(FacesMessage. SEVERITY_ERROR); message.

SetSummary("Error"); message. SetDetail("Duplicate"); //This will end up in throw new ValidatorException(message); } else { setupView. SetCcmNameUnique(true); } } else { logger.

Log(Level. SEVERE, "Please check the root folder path as " + "we cannot seems to see it. The path is {0}", rootPath); } } jsf jsf-2.0 primefaces link|improve this question edited Feb 29 at 19:37 asked Feb 27 at 17:50Thang Pham1,90222980 81% accept rate.

You want to use a validator instead of an action listener. With public void validateDuplicateCCM(FacesContext context, UIComponent component, Object value) throws ValidatorException { if (value == null || value.toString().isEmpty()) { return; // Let required="true" handle. } // ... if (duplicate) { ((UIInput) component).

SetValid(false); ccmNameExisted = true; // I'd rather throw ValidatorException instead of above two lines here so that it ends up in } else { ccmNameUnique = true; } }.

I love your suggestion about throw new ValidatorException(). However, I still having the same problem. That if I don't type in anything, and tab away, it does not invoke validateDuplicateCCM.

Therefore, if I type in something unique, and tab away, tick mark show up, and if I erase what I just type, and tab away the Required message show up, but the tick mark still there, because validateDuplicateCCM did not get invoked. Any idea BalusC? Same behavior if I switch to h:inputText – Thang Pham Feb 28 at 14:16 Oh yes, you've hit a bug in validator attribute.

See this related question/answer: stackoverflow.com/questions/9415059/… It will indeed not be invoked when the value is null. I'd move it into a normal Validator class which you declare by . – BalusC Feb 28 at 14:23 I see, however, I need to set ccmNameUnique = true; if no duplication found.

CcmNameUnique is a variable in my view scoped managed bean SetupView. Can we inject managed bean into validator, BalusC? – Thang Pham Feb 28 at 16:02 You can only inject it if the validator class is registered as @ManagedBean and is been used as .

Alternatively it's available by Application#evaluateExpressionGet(). As another alternative you could also try rendered="#{facesContext. Postback and not facesContext.

ValidationFailed}" on the check icon (this may only have undesireable side effects when also rendered by another inputs, so be careful with this). – BalusC Feb 28 at 16:06 I put the Validator as managed bean, but I still experienced two issues: 1. It still not get into my validator when the field is null.

And 2. It does not set my ccmNameUnique to true. I post the code in my original post.

Can you take a look, BalusC – Thang Pham Feb 28 at 16:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions