0

I'm trying to validate some values in my form, but this validation constraints I want to come from some annotations(javax.validation.constraints.*) on a class. I tried everything I can, spend a few hours searching google/stackoverflow - but I get the same thing, it may be that I'm missing something really simple, but still I can't see it

I use Primefaces 5.3, and webspehere8.5(java1.7)

Below is my Bean where I'm going to receive the values.

Please note,I tried a few ways to get this working as I was not sure which one would wokr:

  1. Use a holder for my values (see below SearchForm class)
  2. Directly in the the bean

    @ManagedBean
    @SessionScoped
    public class MagicBean implements Serializable {
        private static final long serialVersionUID = -8724952745982557284L;
    
        SearchForm formValues = new SearchForm();
    
        public void submitSearchForm() {
            System.out.println("is Ok");
        }
        ....
    
        @NotNull
        @Pattern(regexp = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$", message = "This is not a valid email")
        private String email;
        ...
    
    }
    

Here is the search form class

public class SearchForm {
    ...
    @NotNull
    @Size(min = 3, message = "Name should at least be 3 characters long")
    private String name;
    ...
}

And here is how my page looks like

<p:growl id="msgs" showDetail="true" />
    <p:messages id="msgs2"/>
        <h:form id="searchParamsForm">
           <p:fieldset widgetVar="searchParamsFormToggle" legend="Search params" toggleable="true" toggleSpeed="500">
                <h:panelGrid columns="3" cellpadding="5">

                    <h:outputText>Name</h:outputText>
                    <p:inputText id="name" required="true" >#{magicBean.formValues.name}    <!-- notice here I use some other class -->
                    </p:inputText>
                    <p:message id="errorName" for="name" style="color:red"/>

                    <h:outputText>Email</h:outputText>
                    <p:inputText id="email">#{magicBean.email}</p:inputText> <!-- direct from the magic bean -->
                    <p:message id="errorMail" for="email" style="color:red"/>

                    <p:commandButton value="Save" ajax="false" icon="ui-icon-check" validateClient="true"/>

                    <p:commandButton value="Search" update="msgs msgs2 errorMail searchParamsForm" ajax="false" icon="ui-icon-check" validateClient="true" action="#{magicBean.submitSearchForm}" process="searchParamsForm"  >

                </h:panelGrid>
            </p:fieldset>
        </h:form>

And of course here are some params I found I have to set

<context-param>
    <param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>primefaces.TRANSFORM_METADATA</param-name>
    <param-value>true</param-value>
</context-param>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ţîgan Ion
  • 176
  • 1
  • 12
  • only no action? You didn't get any errors? – Jordi Castilla Jun 09 '16 at 12:15
  • your form is submitted right? only problem is validation not applied? – Jordi Castilla Jun 09 '16 at 12:17
  • yes, I use the Validator in there, and it will find the "ConstraintViolation"s – Ţîgan Ion Jun 09 '16 at 12:17
  • it seems it doesn't render the validation elements(this is a theory, I'm not very familiar with this thing) – Ţîgan Ion Jun 09 '16 at 12:18
  • 1
    Where exactly did you learn that you should declare the input value as body of the input component instead of its `value` attribute? I have never seen that in any JSF tutorial/book/showcase. How to properly bind input values from view to model is demonstrated here: http://stackoverflow.com/q/3681123 – BalusC Jun 09 '16 at 12:20
  • usually I use the other form, I think I had this copied from somewhere, but now it works, thanks dude!! please do respond here, I will accept – Ţîgan Ion Jun 09 '16 at 12:24

0 Answers0