2 Replies Latest reply on Dec 24, 2008 12:06 PM by Dirk Ho

    Annotated Validation does not send error

    Dirk Ho Newbie

      Hello,


      I have entities with annotations to validate the attributes. If I update my database with a formular, validation works and I get errors send from the entities if the RegEx doesn't fit.


      But if I use my registration formular, I don't get messages, except the not-null-messages from my AJAX-validation.


      Perhaps you can help me and tell me, what is the problem?!?!



      @Stateful
      @Scope(EVENT)
      @Name("registerAction")
      public class RegisterAction implements Register {
              
              @Logger
              Log log;
      
              @EJB
              private Benutzerverwaltung bv;
              
              @PersistenceContext
              private EntityManager em;
         
              @In
              private transient FacesContext facesContext;
              
              // Attribute müssen @In sein, da sonst Fehler geworfen werden
              @In(create = true)
              private Kunde kunde;
              
              @In(create = true)
              private Adresse adresse;
              
              @In(create = true)
              private Bankverbindung bankverbindung;
              
              @In(create = true)
              private Login login;
              
              ArrayList<Anrede> anreden;
              ArrayList<Bank> banken;
              
              // Attribute für Select-Felder dürfen NICHT @In sein
              private String anrede;
              private String bank;
              
              private String verify;
              
              private static String ROLLE_BEI_REGISTRIERUNG = "kunde";
                      
          public List<Anrede> anredenSelect() {
              anreden = new ArrayList<Anrede>(bv.findAllAnreden());
              return anreden;
          }
          
          public List<Bank> bankenSelect() {
              banken = new ArrayList<Bank>(bv.findAllBanken());
              return banken;
          }
      
         public String register() throws AnredeNotFoundException, BankNotFoundException, KundeDuplikatException, KundeCreateAdresseException, Exception {
                 
                 // Kunde-Liste erzeugen, die dann gebraucht wird, um den zu speichernden Kunden
                 // an den Anwendungskern übergeben zu können.
                 List<Kunde> kunden = new ArrayList<Kunde>();
      
                 // Kunden-Objekt zusammenbauen
                 Anrede anrede = bv.findAnredeByAnrede(this.anrede);
                 Bank bank = bv.findBankByName(this.bank);
                                 
                 bankverbindung.setBank(bank);
                 kunde.setAnrede(anrede);
                 
                 kunde.setBankverbindung(bankverbindung);
                 kunde.setAdresse(adresse);
                 kunde.setLogin(login);
                 kunde.setRolle(ROLLE_BEI_REGISTRIERUNG);
                 
                 // Anmeldedatum generieren
                 Calendar calendar = new GregorianCalendar();
                 calendar.setTimeZone(TimeZone.getTimeZone("ECT"));
                 Date anmeldeDatum = calendar.getTime();
       
                 kunde.setAnmeldeDatum(anmeldeDatum);
                 
                 // Den neuen Kunden in die kunden-Liste hinzufügen
                 kunden.add(kunde);
                 
                 // Temporäre Collection für die zurückgelieferten Kunden anlegen
                 Collection<Kunde> tmp = null;
                 
                 try {
                         // Eintrag des neuen Kunden in die DB
                         tmp = bv.createKunden(kunden);          
                 } catch(KundeDuplikatException kde) {
                         throw kde;
                 } catch(KundeCreateAdresseException kcae) {
                         throw kcae;
                 } catch(Exception e) {
                         throw e;
                 }
                 
                 log.info(kunde);
                 
                 facesContext.addMessage(null, new FacesMessage("Registrierung erfolgreich!"));
                 
                 return "";
         }
      
         public String getVerify()
         {
            return verify;
         }
      
         public void setVerify(String verify)
         {
            this.verify = verify;
         }
         
         @Remove
         public void destroy() {}
      }
      



      I deleted getter and setter for the attributes - don't wonder.


      This is an example for my validation in the entity:


      And this is my register.xhtml


      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                                   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
      <ui:composition xmlns:a4j="http://richfaces.org/a4j"
                                      xmlns="http://www.w3.org/1999/xhtml"
                      xmlns:s="http://jboss.com/products/seam/taglib"
                      xmlns:ui="http://java.sun.com/jsf/facelets"
                      xmlns:f="http://java.sun.com/jsf/core"
                      xmlns:h="http://java.sun.com/jsf/html"
                      xmlns:rich="http://richfaces.org/rich"
                      template="layout/template.xhtml">
      
      <ui:define name="body">
              <h:messages globalOnly="true" styleClass="message" style="width: 150px;" id="globalMessages" />
      
              <h2>#{messages['website.headline.personalData']}</h2>       
              <h:form id="register" styleClass="">
                      <label class="formularFelder">#{messages['website.label.title']}:</label>
                      <h:selectOneMenu value="#{registerAction.anrede}" id="anrede" styleClass="selectFelder" required="true" requiredMessage="#{messages['website.validation.error.titleRequired']}">
                              <s:selectItems var="anrede" value="#{registerAction.anredenSelect()}" 
                              label="#{anrede.anrede}" itemValue="#{anrede.anrede}" noSelectionLabel="#{messages['website.select.selectTitle']}"/>
                      </h:selectOneMenu>
                                                                                                              <h:message for="anrede" styleClass="inputError" /><br class="inputBreak" />
          
                      <label class="formularFelder">#{messages['website.label.surname']}:</label><h:inputText id="vorname" value="#{kunde.vorname}" styleClass="inputFelder" required="true" requiredMessage="#{messages['website.validation.error.surnameRequired']}" />
                                                                                                              <h:message for="vorname" styleClass="inputError" /><br class="inputBreak" />
                      <label class="formularFelder">#{messages['website.label.name']}:</label><h:inputText id="name" value="#{kunde.name}" styleClass="inputFelder" required="true" requiredMessage="#{messages['website.validation.error.nameRequired']}" />
                                                                                                              <h:message for="name" styleClass="inputError" /><br class="inputBreak" />
                      <label class="formularFelder">#{messages['website.label.dateOfBirth']}:</label><rich:calendar id="date" value="#{kunde.gebDatum}" datePattern="yyyy-MM-dd" inputClass="inputFelder" inputStyle="width: 131px;" required="true" requiredMessage="#{messages['website.validation.error.dateOfBirthRequired']}" />
                                                                                                              <h:message for="date" styleClass="inputError" /><br class="inputBreak" />
                      <label class="formularFelder">#{messages['website.label.phoneNumber']}:</label><h:inputText id="telefon" value="#{kunde.telefon}" styleClass="inputFelder" />
                                                                                                              <h:message for="telefon" styleClass="inputError" /><br class="inputBreak" />
                      <label class="formularFelder">#{messages['website.label.mobileNumber']}:</label><h:inputText id="handy" value="#{kunde.handy}" styleClass="inputFelder" />
                                                                                                              <h:message for="handy" styleClass="inputError" /><br class="inputBreak" />
      
              <h2>#{messages['website.headline.address']}</h2>
                      <label class="formularFelder">#{messages['website.label.street']} | #{messages['website.label.houseNumber']}:</label><h:inputText id="strasse" value="#{adresse.strasse}" styleClass="inputFelder" style="width: 120px; margin-right: 5px;" required="true" requiredMessage="#{messages['website.validation.error.streetRequired']}" /><h:inputText id="hausnr" value="#{adresse.hausnr}" styleClass="inputFelder" style="width: 22px;" required="true" requiredMessage="#{messages['website.validation.error.houseNumberRequired']}" />
                                                                                                              <h:message for="strasse" styleClass="inputError" /><h:message for="hausnr" styleClass="inputError" /><br class="inputBreak" />
                      <label class="formularFelder">#{messages['website.label.postal']} | #{messages['website.label.city']}:</label><h:inputText id="plz" value="#{adresse.plz}" styleClass="inputFelder" style="width: 35px; margin-right: 5px;" required="true" requiredMessage="#{messages['website.validation.error.postalRequired']}" /><h:inputText id="ort" value="#{adresse.ort}" styleClass="inputFelder" style="width: 107px;" required="true" requiredMessage="#{messages['website.validation.error.cityRequired']}" />
                                                                                                              <h:message for="plz" styleClass="inputError" /><h:message for="ort" styleClass="inputError" /><br class="inputBreak" />
      
              <h2>#{messages['website.headline.bankDetails']}</h2>
                      <label class="formularFelder">#{messages['website.label.bank']}:</label>
                      <h:selectOneMenu value="#{registerAction.bank}" id="bank" styleClass="selectFelder" required="true" requiredMessage="#{messages['website.validation.error.bankRequired']}">
                              <s:selectItems var="bank" value="#{registerAction.bankenSelect()}" 
                      label="#{bank.bank}" itemValue="#{bank.bank}" noSelectionLabel="#{messages['website.select.selectBank']}" />
                      </h:selectOneMenu>
                                                                                                              <h:message for="bank" styleClass="inputError" /><br class="inputBreak" />
                      <label class="formularFelder">#{messages['website.label.accountNumber']}:</label><h:inputText id="kontonummer" value="#{bankverbindung.kontonummer}" styleClass="inputFelder"  required="true" requiredMessage="#{messages['website.validation.error.accountNumberRequired']}" />
                                                                                                              <h:message for="kontonummer" styleClass="inputError" /><br class="inputBreak" />
              
              <h2>#{messages['website.headline.loginData']}</h2>                                                                                                  
                      <label class="formularFelder">#{messages['website.label.email']}:</label><h:inputText id="email" value="#{login.email}" styleClass="inputFelder" required="true" requiredMessage="#{messages['website.validation.error.emailRequired']}" />
                                                                                                              <h:message for="email" styleClass="inputError" /><br class="inputBreak" />
                      <label class="formularFelder">#{messages['website.label.password']}:</label><h:inputText id="passwort" value="#{login.passwort}" styleClass="inputFelder" required="true" requiredMessage="#{messages['website.validation.error.passwordRequired']}" />
                                                                                                              <h:message for="passwort" styleClass="inputError" /><br class="inputBreak" />
                      <label class="formularFelder">#{messages['website.label.password']}:</label> <!--h:inputText id="name" value="#{kunde.name}" styleClass="inputFelder" required="true" requiredMessage="#{messages['website.validation.error.passwordRequired']}" /><h:message for="name" styleClass="inputError" /--><br class="inputBreak" />
                      
                      <label class="formularFelder"></label><h:commandButton action="#{registerAction.register()}" value="#{messages['website.button.register']}" styleClass="submitButton"/>
              
              </h:form>
      </ui:define>
      </ui:composition>
      



      Hopefully you can help me getting this work.


      Thanks and best regards,


      Dirk


      PS: I wish you a merry Xmas!

        • 1. Re: Annotated Validation does not send error
          Dirk Ho Newbie
                 @Column
                  @NotNull(message="#{messages['website.validation.error.surnameRequired']}")
                  @Length(min=2, max=32, message="#{messages['website.validation.error.wrongSurameLength']}")
                  @Pattern(regex="^[A-ZÄÖÜ][a-zäöüß]+(-[A-ZÄÖÜ][a-zäöüß]+)?$", message="#{messages['website.validation.error.wrongSurnameFormat']}")
                  public String vorname;
          



          I'm sorry, this is an example out of one of my entites for the attribute validation...

          • 2. Re: Annotated Validation does not send error
            Dirk Ho Newbie

            Oh my goodness! Searching in google helped me to find my error: I forgot the
            <s:validateAll> Tag in my register.xhtml...


            Best regards and merry Xmas,


            Dirk