Validation with JSF

2

I want to validate the form displayed in the image in a different way than the framework imposes on me. Validation would be something like an alert, meaning the user would be informed that they can only add data if they select a combo option. This validation should occur after clicking the Add button. Something like: "Select an option before adding". Is it possible without JavaScript? How to do?

    
asked by anonymous 23.07.2014 / 15:10

2 answers

2

You can do this using Primefaces.

In your SelectOneMenu it would have the following properties:

  • a property called required="true" that would say the field is required.
  • a property called requiredMessade="Your message" where you would say which message you want to add.

Above your form would have a message field to be shown, which could look like this:

<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />

On your button you would need to add the following properties:

  • ajax="true" to validate using ajax.
  • immediate="false" (if true it will not do screen validations)

Or, if you do not want to use primefaces, you could put a message using Faces after the click of the button on your Bean, like this:

FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Qual a mensagem", "O que dizer na mensagem");

Any questions you can check on primefaces' own site:

link

    
23.07.2014 / 15:27
2

You can do what you want.

First add this to your XHTML:

<p:growl id="messageGrowl" />

Then, in the method triggered by the Add button, after doing the validation, add the following line of code to trigger a message on the page:

FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Mensagem", "Descrição");
FacesContext.getCurrentInstance().addMessage(null, facesMessage);

For more details, click here .

    
23.07.2014 / 15:22