Validation with JSF

Asked

Viewed 869 times

2

I want to perform the validation of the form displayed in the image in a different way than the framework requires. The validation would be something like an alert, IE, the user would be informed that can only add data if you select a combo option. This validation should occur after clicking the Add button. Something like: "Select an option before adding". Is this possible without using Javascript? How to do?

inserir a descrição da imagem aqui

2 answers

2

You can do what you want.

First add this to your XHTML:

<p:growl id="messageGrowl" />

Then in the method triggered by the button Add, after doing the proper 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.

2


You can do this using Primefaces.

In your Selectonemenu you would have the following properties:

  • a Property called required="true" that would say that 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 be so:

<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 don’t want to use the first faces, you could put a message using Faces after the click of the button on your Bean, in this way:

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

Any questions you can check in the site itself:

http://www.primefaces.org/

  • 1

    Hello @Dante, the attribute "ajax" comes set with "true" by default mano.

  • @Coldhack is true, I did not remember this detail, but the information was passed at least. Thanks for the correction!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.