3 Replies Latest reply on Nov 14, 2009 10:59 AM by Joshua D

    Problem validating strings with only white spaces

    Miguel Angel Goicochea Newbie

      Hello I have the following problem.


      I am validating a field with the @NotEmpty validator and it usually works, but when the string contains only white spaces it does not work as I expect.


      It does not save the entity, which is correct, and then loads the search page and the messages are being shown there. This happens because no errors are thrown when persist is called in the entity manager so the save method returns persisted and the page rules are executed and it contains a rule to redirect to the search page after a record has been successfully created. However, the validation messages have been created because they appear on the search page.


      Is there a way to know if the validation is going to fail and prevent my code from returning the persisted message, so it stays on the current page?


      This is my form:



      <ui:composition 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"
           xmlns:a4j="http://richfaces.org/a4j"
           template="/layout/smTemplate.xhtml">
      
           <ui:define name="body">
                <ui:include src="/layout/sectionLabel.xhtml">
                     <ui:param name="sectionName"
                          value="#{skillAction.new ? 'Add' : 'Edit'} Skill" />
                </ui:include>
      
                <h:form id="skillMaintenance">
                     <s:validateAll>
                          <rich:panel>
                               <f:facet name="header">
                                    <h:outputText value="Basic Information" />
                               </f:facet>
                               <rich:messages showDetail="true" showSummary="false"
                                    errorClass="errorMessage" warnClass="warnMessage"
                                    fatalClass="fatalMessage" infoClass="infoMessage" />
                               <h:panelGrid columns="2">
                                    <h:outputText value="Code: " />
                                    <h:inputText size="8" maxlength="4" disabled="#{!skillAction.new}"
                                         value="#{skill.code}" id="skillCode">
                                         <rich:beanValidator summary="Skill Code" />
                                    </h:inputText>
                                    <h:outputText value="Name: " />
                                    <h:inputText size="50" maxlength="50" value="#{skill.name}"
                                         id="skillName">
                                         <rich:beanValidator summary="Skill Name" />
                                    </h:inputText>
                                    <h:outputText value="Description: " />
                                    <h:inputTextarea value="#{skill.description}" cols="50" rows="4"
                                         id="skillDescription">
                                         <rich:beanValidator summary="Skill Description" />
                                    </h:inputTextarea>
                               </h:panelGrid>
      
                               <a4j:commandButton id="save" value="Save"
                                    action="#{skillAction.save}" rendered="#{skillAction.new}"
                                    styleClass="smButton" />
      
                               <a4j:commandButton id="update" value="Update"
                                    action="#{skillAction.update}" rendered="#{!skillAction.new}"
                                    styleClass="smButton" />
      
                               <a4j:commandButton id="delete" value="Delete"
                                    action="#{skillAction.delete}" rendered="#{!skillAction.new}"
                                    immediate="true" styleClass="smButton" />
      
                               <s:button id="cancel" value="Cancel" propagation="end"
                                    view="/maintenance/skillSearch.xhtml" styleClass="smButton" />
      
                          </rich:panel>
                     </s:validateAll>
                </h:form>
      
           </ui:define>
      </ui:composition>




      This is my bean:



      package org.test.skillssystem.model.entity;
      
      import java.io.Serializable;
      
      import javax.persistence.Column;
      import javax.persistence.Embedded;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.SequenceGenerator;
      import javax.persistence.Table;
      import javax.persistence.UniqueConstraint;
      
      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotEmpty;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.annotations.Name;
      
      import org.test.skillssystem.interfaces.Auditable;
      import org.test.skillssystem.model.embeddable.AuditFields;
      
      @Entity
      @Name("skill")
      @Table(name = "skills", uniqueConstraints = { @UniqueConstraint(columnNames = "skill_code") })
      public class Skill implements Serializable, Auditable
      {
           private static final long serialVersionUID = -1318225598262268995L;
      
           private Long id;
           private String code;
           private String name;
           private String description;
      
           @Embedded
           private AuditFields auditFields;
      
           public Skill()
           {
           }
      
           @Id
           @NotNull
           @GeneratedValue(generator = "skill_id", strategy = GenerationType.SEQUENCE)
           @SequenceGenerator(name = "skill_id", allocationSize = 0, sequenceName = "skill_skill_id_seq")
           @Column(name = "skill_id")
           public Long getId()
           {
                return id;
           }
      
           public void setId(Long id)
           {
                this.id = id;
           }
      
           @NotEmpty(message = "Skill cannot be empty")
           @Column(name = "skill_code")
           @Length(max = 4, min = 4, message = "Skill code has to be four characters long")
           public String getCode()
           {
                return code;
           }
      
           public void setCode(String code)
           {
                this.code = code.trim().toUpperCase();
           }
      
           @Column(name = "skill_name")
           @Length(max = 100)
           @NotEmpty(message = "Skill name cannot be empty")
           public String getName()
           {
                return name;
           }
      
           public void setName(String name)
           {
                this.name = name.trim();
           }
      
           @Column(name = "skill_desc")
           @Length(max = 250, message = "Description length cannot be larger than 250 characters")
           public String getDescription()
           {
                return description;
           }
      
           public void setDescription(String description)
           {
                this.description = description;
           }
      
           public void setAuditFields(AuditFields auditFields)
           {
                this.auditFields = auditFields;
           }
      
           public AuditFields getAuditFields()
           {
                if (auditFields == null) this.auditFields = new AuditFields();
                return auditFields;
           }
      
           public void createAudit()
           {
                getAuditFields().createAudit();
      
           }
      
           public void updateAudit()
           {
                getAuditFields().updateAudit();
           }
      
      }



      Thanks