9 Replies Latest reply on Jun 12, 2013 11:57 AM by Mohammad Weshah

    Bean validation problem

    Mohammad Weshah Novice

      Dears,

      as i know to fire the bean validation (@notation validation jsr 303) just i have to add <richvalidator inside the input fileds but it is not fire the validation till i add <a4j:ajax event="change"/> is that true or there is somthing wrong .

       

      code :

       

      <h:inputText id="editNameAr"

                       valueChangeListener="#{outsourcedBacking.arNameChangeListener}"

                       value="#{outsourcedBacking.editOutSourcedBean.nameAr}" 

                       maxlength="100"

                       size="100" >

           <rich:validator/>

          </h:inputText>

          <rich:message for="editNameAr" />

       

      to fire the validation i have to add

      <a4j:ajax event="change"/>

       

       

      Regards

      Wish

        • 1. Re: Bean validation problem
          Brian Leathem Master

          The validator will not be apllied until you submit the form.  By adding the <a4j:ajax .../> you are triggering a form submission then the value is changed.

          • 2. Re: Bean validation problem
            Mohammad Weshah Novice

            Yes, but in Richfaces showcase there is no <a4j:ajax .../> and the validation works successfully .

            see the link

            http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=clientValidation&sample=jsr303&skin=blueSky

             

            they dose not use <a4j:ajax .../> and it works but in my code if i remove <a4j:ajax .../> the validation dose not fire

             

            note:

            i take the demo code and i add it to my project, the validation dose not work till i add commmand buuton and press it , strange , my richfaces v4.3.2

            • 3. Re: Bean validation problem
              Juraj Húska Master

              Hello Mohammad,

               

              to achieve client side validation you have to precisely follow the examples you are referencing.

               

              In you example, to use JSR303, you have to put the validation constraints in the bean with use of annotations over the managed properties (e.g. @Size(min = 3, max = 12)).

              See the bean code which is connected with the example:

              http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=clientValidation&sample=jsr303&skin=blueSky

              • 4. Re: Bean validation problem
                Mohammad Weshah Novice

                already i did that, again the problem to fire the validation i have to add a4j:ajax on every input

                • 5. Re: Bean validation problem
                  Juraj Húska Master

                  Could you please attach complete facelet and your bean code as well ?

                  • 6. Re: Bean validation problem
                    Mohammad Weshah Novice

                    xhtml :

                     

                     

                     

                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                    <html xmlns="http://www.w3.org/1999/xhtml"

                              >

                              <ui:composition xmlns:h="http://java.sun.com/jsf/html"

                          xmlns:f="http://java.sun.com/jsf/core"

                          xmlns:ui="http://java.sun.com/jsf/facelets"

                          xmlns:rich="http://richfaces.org/rich"

                          xmlns:a4j="http://richfaces.org/a4j"

                          xmlns:bbt="http://java.sun.com/jsf/composite/bbt">

                     

                     

                     

                     

                     

                     

                     

                     

                    <rich:popupPanel   id="addPopupOutSourced"

                                       domElementAttachment="form"

                                       modal="true"

                                       header="#{lbs.OUT_ADD_FN}" 

                                       moveable="true"

                                       autosized="true"  

                                       >

                            <f:facet name="controls">

                                <h:outputLink value="#" onclick="#{rich:component('addPopupOutSourced')}.hide(); return false;">

                                    X

                                </h:outputLink>

                            </f:facet>

                         

                     

                     

                    <h:form id="addOutsourcedForm">

                    <rich:panel>

                     

                      <h:panelGrid  columns="3">

                     

                     

                      <f:validateBean validationGroups="com.wish.diwan.common.bean.validation.groups.AddValidationGroup"  >

                       <rich:validator>

                     

                         <h:panelGroup>

                                    <h:outputLabel for="addType" value="#{lbs.OUT_TYPE}" />

                                    <h:outputLabel for="addType" value="#{lbs.GEN_SPERATOR}" />

                                    <h:outputLabel for="addType" value="#{lbs.GEN_REQUIRED}" styleClass="required" />

                        </h:panelGroup>

                     

                     

                     

                     

                       

                        <rich:select  id="addType"

                                      defaultLabel="#{msg.GEN_SELECT_VALUE} #{lbs.OUT_TYPE}"

                                      

                                      value="#{outsourcedBacking.addOutSourcedBean.type.id}"

                                      valueChangeListener="#{outsourcedBacking.addRepTypeChangeListener}" >

                         <f:selectItems value="#{outsourcedBacking.populateLKPListByParentId(1)}"/>

                            <a4j:ajax event="change" execute="@this" />

                           

                        </rich:select>

                       

                        <rich:message id="addTypeMSG" for="addType" />

                     

                     

                         <h:panelGroup>

                                    <h:outputLabel for="addNameAr" value="#{lbs.OUT_NAME_AR}" />

                                    <h:outputLabel for="addNameAr" value="#{lbs.GEN_SPERATOR}" />

                                    <h:outputLabel for="addType" value="#{lbs.GEN_REQUIRED}" styleClass="required" />

                        </h:panelGroup>

                        <h:inputText id="addNameAr"

                                     valueChangeListener="#{outsourcedBacking.arNameChangeListener}"

                                     value="#{outsourcedBacking.addOutSourcedBean.nameAr}" 

                                     maxlength="100"

                                     size="100" >

                         <a4j:ajax event="change" />

                        

                        </h:inputText>

                        <rich:message for="addNameAr" />

                       

                        <h:panelGroup>

                                    <h:outputLabel for="addNameEn" value="#{lbs.OUT_NAME_EN}" />

                                    <h:outputLabel for="addNameEn" value="#{lbs.GEN_SPERATOR}" />

                                    <h:outputLabel for="addType" value="#{lbs.GEN_REQUIRED}" styleClass="required" />

                        </h:panelGroup>

                        <h:inputText id="addNameEn" valueChangeListener="#{outsourcedBacking.enNameChangeListener}" value="#{outsourcedBacking.addOutSourcedBean.nameEn}"  maxlength="100" size="100" >

                         <a4j:ajax event="change" />

                        </h:inputText>

                        <rich:message for="addNameEn"  />

                       

                        <h:panelGroup>

                                    <h:outputLabel for="addPersonName" value="#{lbs.OUT_PERSON_NAME}" />

                                    <h:outputLabel for="addPersonName" value="#{lbs.GEN_SPERATOR}" />

                        </h:panelGroup>

                        <h:inputText id="addPersonName" value="#{outsourcedBacking.addOutSourcedBean.personName}"  maxlength="200" size="100" />

                        <rich:message for="addPersonName"  />

                       

                        <h:panelGroup>

                                    <h:outputLabel for="addPhone" value="#{lbs.OUT_PHONE}" />

                                    <h:outputLabel for="addPhone" value="#{lbs.GEN_SPERATOR}" />

                        </h:panelGroup>

                        <h:inputText id="addPhone" value="#{outsourcedBacking.addOutSourcedBean.phoneNumber}"  maxlength="20" size="20" />

                        <rich:message for="addPhone"  />

                       

                        <h:panelGroup>

                                    <h:outputLabel for="addFax" value="#{lbs.OUT_FAX}" />

                                    <h:outputLabel for="addFax" value="#{lbs.GEN_SPERATOR}" />

                        </h:panelGroup>

                        <h:inputText id="addFax" value="#{outsourcedBacking.addOutSourcedBean.faxNumber}"  maxlength="20" size="20" />

                        <rich:message for="addFax"  />

                       

                            <h:panelGroup>

                                    <h:outputLabel for="addMobile" value="#{lbs.OUT_MOBILE}" />

                                    <h:outputLabel for="addMobile" value="#{lbs.GEN_SPERATOR}" />

                        </h:panelGroup>

                        <h:inputText id="addMobile" value="#{outsourcedBacking.addOutSourcedBean.mobileNumber}"  maxlength="20" size="20" />

                        <rich:message for="addMobile"  />

                       

                            <h:panelGroup>

                                    <h:outputLabel for="addPobox" value="#{lbs.OUT_POBOX}" />

                                    <h:outputLabel for="addPobox" value="#{lbs.GEN_SPERATOR}" />

                        </h:panelGroup>

                        <h:inputText id="addPobox" value="#{outsourcedBacking.addOutSourcedBean.pobox}"  maxlength="200" size="100" />

                        <rich:message for="addPobox"  />

                       

                            <h:panelGroup>

                                    <h:outputLabel for="addEmail" value="#{lbs.OUT_EMAIL}" />

                                    <h:outputLabel for="addEmail" value="#{lbs.GEN_SPERATOR}" />

                        </h:panelGroup>

                        <h:inputText id="addEmail" value="#{outsourcedBacking.addOutSourcedBean.emailAddress}"  maxlength="200" size="100" >

                         <a4j:ajax event="change"  />

                        </h:inputText>

                        <rich:message id="addEmailMsg" for="addEmail"  />

                       

                            <h:panelGroup>

                                    <h:outputLabel for="addNotes" value="#{lbs.OUT_NOTES}" />

                                    <h:outputLabel for="addNotes" value="#{lbs.GEN_SPERATOR}" />

                        </h:panelGroup>

                        <h:inputText id="addNotes" value="#{outsourcedBacking.addOutSourcedBean.note}"  maxlength="200" size="100" />

                        <rich:message for="addNotes"  />

                       

                            <h:panelGroup>

                                    <h:outputLabel for="addOrder" value="#{lbs.OUT_ORDER}" />

                                    <h:outputLabel for="addOrder" value="#{lbs.GEN_SPERATOR}" />

                        </h:panelGroup>

                        <rich:inputNumberSpinner id="addOrder" value="#{outsourcedBacking.addOutSourcedBean.order}"  maxValue="9999" minValue="0" />

                        <rich:message for="addOrder"  />

                     

                     

                    </rich:validator>

                    </f:validateBean>

                     

                     

                    </h:panelGrid>

                     

                      

                       <a4j:commandButton   id="addOutSourcedBTN"

                                                              

                                                                    render="outsourcedTable"

                                                                          value="#{lbs.BTN_ADD}"

                                                                          oncomplete="if(#{outsourcedBacking.closeAddDialog}){#{rich:component('addPopupOutSourced')}.hide();}"                                                                                                                

                                                                          actionListener="#{outsourcedBacking.saveAddAction()}"/>

                     

                     

                     

                     

                      </rich:panel>

                    </h:form>

                    </rich:popupPanel>

                     

                    </ui:composition>

                    </html>

                     

                     

                    bean:

                     

                     

                     

                    @Entity(name="DW_OUT_SOURCED")

                    @Table(name="DW_OUT_SOURCED")

                    @TableGenerator(name = "outsourcedGenerator", table = "DW_SEQUENCE", schema = "DIWAN", pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_VALUE", pkColumnValue = "OUT_SOURCED_SEQUENCE", allocationSize = 1, initialValue = 0)

                     

                     

                    public class OutsourcedBean extends BaseBean{

                     

                              @Id

                              @GeneratedValue(strategy = GenerationType.TABLE, generator="outsourcedGenerator")

                              @Column(name="ID")

                              private int Id;

                     

                              @NotEmpty(message="{GEN_REQUIRED}" , groups={AddValidationGroup.class , EditValidationGroup.class,ManageValidationGroup.class})

                              @JoinColumn(name="TYPE_ID",referencedColumnName="ID",nullable=false)

                              @ManyToOne

                              private ChildLKPBean type;

                     

                              @NotEmpty(message="{GEN_REQUIRED}" , groups={AddValidationGroup.class , EditValidationGroup.class})

                              @Column(name="NAME_AR",length=100, nullable = false,unique = true)

                              private String nameAr;

                     

                              @NotEmpty(message="{GEN_REQUIRED}" , groups={AddValidationGroup.class , EditValidationGroup.class})

                              @Column(name="NAME_EN",length=100, nullable = false,unique = true)

                              private String nameEn;

                     

                              @Column(name="PERSON_NAME",length=200)

                              private String personName;

                     

                              @Column(name="PHONE_NUMBER",length=20)

                              private String phoneNumber;

                     

                              @Column(name="FAX_NUMBER",length=20)

                              private String faxNumber;

                     

                              @Column(name="MOBILE_NUMBER",length=20)

                              private String mobileNumber;

                     

                              @Column(name="POBOX",length=200)

                              private String pobox;

                     

                              @Column(name="EMAIL_ADDRESS",length=200)

                              @Email(message="{GEN_EMAIL_FORMAT_IS_NOT_CORRECT}",groups={AddValidationGroup.class , EditValidationGroup.class})

                              private String emailAddress;

                     

                              @Column(name="NOTE",length=200)

                              private String note;

                     

                              @Column(name="ORDER_ID",length=10)

                              private int order;

                    • 7. Re: Bean validation problem
                      Juraj Húska Master

                      You should use annotations from javax.validation.constraints package.

                       

                      See

                      http://docs.oracle.com/javaee/6/api/javax/validation/constraints/package-summary.html

                      • 8. Re: Bean validation problem
                        Lukáš Fryč Master

                        There might be two problems,

                         

                        1. as Juraj said, @NonEmpty is not part of Bean Validation 1.0 (Java EE 6)
                        2. you are using Validation Groups feature which is not supported by rich:validator (it's supported just by rich:graphValidator)

                         

                        For (2), nice catch, this is something we should really support, could you please create a feature request?

                         

                        If you wouldn't use validation groups (just try to remove them [1]), then rich:validator will at least know that there is some bean validation involved (even if using custom validators/annotations).

                        In order to let the rich:validator work just client-side (without JavaScript fallback), you need to define Javascript implementation of validator and register it in csv.xml.

                        But even if you don't have Javascript impl, rich:validator should always do AJAX fallback, where server-side impl determines whether bean property pass validation.

                        So in your case it's just about rich:validator which doesn't know there are some validation concerns (because it doesn't support validation groups).

                         

                        Anyway, Mohammad, could you please create a feature request?

                         

                        [1]

                         

                        @NotEmpty(message="{GEN_REQUIRED}" , groups={AddValidationGroup.class , EditValidationGroup.class})

                                  @Column(name="NAME_AR",length=100, nullable = false,unique = true)

                                  private String nameAr;

                        1 of 1 people found this helpful