0

I'm trying to validate a form before submitting, but is not working as expected.

Here my XHTML

<h:outputLabel for="name">Name: </h:outputLabel>
<p:inputText id="name"  value="#{partyCreationBean.name}" />
<h:message for="name"/>
<h:outputLabel for="symbol">Symbol: </h:outputLabel>

Here my webBean:

@ManagedBean(name = "partyCreationBean")
@RequestScoped
public class PartyCreationBean {

    @EJB
    PartyManagerLocal ejb_partymanager;
    @EJB
    AuthenticationManagerLocal ejb_user;

    private PartyDTO party = new PartyDTO();

    public String getName() {
        return party.getName();
    }

    public void setName(String name) {
        party.setName(name);
}

Here my DTO:

import org.hibernate.validator.constraints.*;

public class PartyDTO implements IDataTransferObject {
    private Integer id;
    @NotEmpty()
    private String name;

    //Getters, setters and other stuff.
}

Now the point is that if I leave "Name" empty and I press the button to create my party, the page just remain all the sames, and i get Exceptions in the console stack traces (a lot like usual :D )

I tried to work with this XHTML (adding require="true"):

<h:outputLabel for="name">Name: </h:outputLabel>
<p:inputText id="name" required="true" value="#{partyCreationBean.name}" />
<h:message for="name"/>
<h:outputLabel for="symbol">Symbol: </h:outputLabel>

and it is working well but I want to put my "constraints" in the DTO level...

Thanks in advance :)

Tiny
  • 27,221
  • 105
  • 339
  • 599
Sam
  • 313
  • 4
  • 21

1 Answers1

0

@NotEmpty will not work like that..

You need to build getters and setters to this:

private PartyDTO party = new PartyDTO();

Then you need to create getters and setters to

@NotEmpty
String name; 

Then in the xhtml file..

<p:inputText id="name"  required="true" value="#{partyCreationBean.party.name}" />

And it should work. Of course if you will use required it will be fine as is..

This can also help you if you will have other issues.. Few jsf Paramters that need to be added sometimes: Bean Validation @NotNull, @NotBlank and @NotEmpty does not work in JSF+Tomcat

Community
  • 1
  • 1
Aviad
  • 1,539
  • 1
  • 9
  • 24
  • Okay i solved the problem... :) Then i just ask you something more if is possible... now i wanna also a Unique constraint for this... i have in the persistent tier a JPA annotation "Column(nullable=false, unique=true)" on the field name. but if i put the same name, just it will appear Exception on the console? Do i have to catch the exception and show the message in some way or is there some simplier way?!?!? thankyou again – Sam May 29 '14 at 10:29
  • Not sure i understood well.. If you are validate this value like that you don't have to do double validation also in the persistance layer.. In case you want handle the exception in this case(This case will be impossible in the current flow) You just need to wrap the call to persistance layer with try and catch an message the user from the jsf bean: FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Fatal!", "System Error")); Something like that – Aviad May 29 '14 at 10:35
  • no no the problem is on the unique constraint, the @NotEmpty is okay now. But now i try this solution. thankyou again :) – Sam May 29 '14 at 10:49
  • So if you can open another question and link the thread and i will help.. I just didn't understand you exactly i guess. NP – Aviad May 29 '14 at 10:51