1

I need to validate across multiple fields in such a way that validation should be violated only when one of the given fields is violated.

It is distinct from cross field validation in which a value of one field is dependent upon the value(s) of one or more of the rest of the fields.

Given below a simple scenario.

<p:inputText id="txt1" value="#{testBean.txt1}" required="false" maxlength="45"/>
<p:inputText id="txt2" value="#{testBean.txt2}" required="false" maxlength="45"/>
<p:inputText id="txt3" value="#{testBean.txt3}" required="false" maxlength="45"/>

<p:commandButton id="btnSubmit" actionListener="#{testBean.insert}" 
                 icon="ui-icon-check" value="Save"/>

In which, validation violation should be occurred only when one of the given three text fields is left blank. If anyone of them is filled with a value then, all should be validated. In which case, validation should not be violated.

How to proceed with this scenario? Does JSF/PrimeFaces provide some way to perform validation in this way?

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Hi mate, I would like to know the advantages, problems, performances, enhances, or just is a following the standar, etc. the validation with validators instead of the validation with the managed bean? It according the answer @Fernando Paladini. Is there others reasons in addition you have said? Thanks! – Fernando Pie May 04 '17 at 18:04

2 Answers2

3

I have a hard time in wrapping my head around your concrete functional requirement, but I believe you're looking for "all or none" validation. I.e. either all fields should be blank, or all fields should be filled. JSF utility library OmniFaces has a validator for exactly this purpose, the <o:validateAllOrNone>.

Here's how you could use it:

<p:inputText id="txt1" value="#{testBean.txt1}" maxlength="45" />
<p:inputText id="txt2" value="#{testBean.txt2}" maxlength="45" />
<p:inputText id="txt3" value="#{testBean.txt3}" maxlength="45" />
<o:validateAllOrNone components="txt1 txt2 txt3" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

Of course!

Primefaces provide a lot of ways that can satisfact you. First of all, you can make validations in your MBean method. In your case, you're calling insert method, so you can do something like this:

public String insert(){

   boolean error = false;
   if(txt1.isEmpty()){
        error = true;
   }

   if(txt2.isEmpty()){
        error = true;
   }

   if(txt3.isEmpty()){
        error = true;
   }

   if(error == true){
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Empty fields!", "Insert something in at least one input!"));
        return null;
   }else{
        return "myPage"
   }
}

Note that you can improve the validations by yourself, following your needs. You can also change the message from:

FacesMessage.SEVERITY_WARN

to:

FacesMessage.SEVERITY_INFO
FacesMessage.SEVERITY_ERROR
FacesMessage.SEVERITY_FATAL

What can give your application a better error message.


Before this works, add this above your input fields:
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" /> 

enter image description here Probably this will work like a charm! If you're interested, check the Primefaces Messages showcase, where you can find some examples and understand better how <p:messages> works.

Also, feel free to check <p:growl>, that in my opinion is a lot better than simple messages. Check out the growl here.
enter image description here

Hope I helped you (:

Paladini
  • 4,522
  • 15
  • 53
  • 96
  • If you have any question, feel free to ask (: – Paladini Dec 03 '13 at 05:33
  • Actually, I was looking for a way to perform validation through validator(s). Performing validation in JSF managed beans themselves is dreadful. Moreover, I have over 15 such fields on a page which require so many such conditional checks which is tedious. I hope JSF/PrimeFaces should have a fair way to perform this kind of validations. Thanks. – Tiny Dec 03 '13 at 13:43
  • Sorry Tiny, I don't understood your question. I tought that you needed a way to check if all fields are blank, but if one ore more than one are selected, isn't a problem. @Balusc can understand your question better than me, sorry if I didn't answer with you need :/ – Paladini Dec 03 '13 at 23:14