16

I implemented my validation logic as follows:

        <h:inputText id="title" value="#{...}" 
            required="true" requiredMessage="...some..text..." 
            validatorMessage="...some..other..text..." >
            <f:validateLength minimum="10" maximum="50"/>
        </h:inputText>

I read a lot about clientside and serverside validation and about their advantages and disadvantages. But I have no idea about what the code above does.

Can somebody please explain that :-)

Cheers

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sven
  • 6,288
  • 24
  • 74
  • 116

1 Answers1

34

In client side validation, it's the client (webbrowser) which validates the input with help of a client side language, e.g. JavaScript. In server side validation, it's the server (webserver) which validates the input with help of a server side language, e.g. Java.

You should never do only client side validation, because the result is controllable (and thus also hackable/spoofable) by the enduser. Usually, you'd like to use client side validation because it gives much sooner feedback. The enduser doesn't need to wait for the form submit being completed and doesn't need to face a "flash of content" (page blanks out and then redisplays with new content). You'd like to use server side validation to ensure the integrity of the submitted data. The enduser has in no way control over the outcome of the server side validation.

In case of JSF, the validation via built-in required="true" attribute and standard/custom validators is always server side. Since JSF 2.0 it's possible to submit a form (and thus also validate the form) using builtin ajaxical functionality. This combines the best of the two worlds: having instant feedback without flash of content and the robustness/integrity of the server side validation.

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for these wise words. Could you please give me a link (or an example) how to include ajax to my code :-) that would be great :-) ...or is it simply the sometimes confusing tag? – Sven Oct 25 '10 at 12:13
  • 1
    Yes, `f:ajax` is one way to introduce javascript/AJAX to your code. For pointers of how to start using it, see a relevant question: http://stackoverflow.com/questions/3138488/how-to-use-ajax-with-jsf-2-0 – Tuukka Mustonen Oct 26 '10 at 21:24
  • 2
    @Sven: put `` inside `UICommand` component. Or if you want to validate on blur of each input element, put `` inside each `UIInput` component. – BalusC Oct 26 '10 at 21:51