Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 1658D200C59 for ; Sun, 2 Apr 2017 11:13:06 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 14EB3160B8E; Sun, 2 Apr 2017 09:13:06 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 0F4BB160B9A for ; Sun, 2 Apr 2017 11:13:04 +0200 (CEST) Received: (qmail 35060 invoked by uid 500); 2 Apr 2017 09:13:02 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 34727 invoked by uid 99); 2 Apr 2017 09:13:02 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 02 Apr 2017 09:13:02 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 96846E117B; Sun, 2 Apr 2017 09:13:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lukaszlenart@apache.org To: commits@struts.apache.org Date: Sun, 02 Apr 2017 09:13:10 -0000 Message-Id: <74bf349e1fee4143a38c2c38578ac817@git.apache.org> In-Reply-To: <12149d097e884182b77af4f51f467c31@git.apache.org> References: <12149d097e884182b77af4f51f467c31@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [09/13] struts-site git commit: cleaned up form-vlaidation-using-xml page archived-at: Sun, 02 Apr 2017 09:13:06 -0000 cleaned up form-vlaidation-using-xml page Project: http://git-wip-us.apache.org/repos/asf/struts-site/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-site/commit/4d57b53e Tree: http://git-wip-us.apache.org/repos/asf/struts-site/tree/4d57b53e Diff: http://git-wip-us.apache.org/repos/asf/struts-site/diff/4d57b53e Branch: refs/heads/master Commit: 4d57b53ee49157836dc3034edb2b173a1c77b164 Parents: 338b55f Author: Stefaan Dutry Authored: Sun Apr 2 09:23:06 2017 +0200 Committer: Stefaan Dutry Committed: Sun Apr 2 09:23:06 2017 +0200 ---------------------------------------------------------------------- .../form-validation-using-xml.md | 119 ++++++++----------- 1 file changed, 48 insertions(+), 71 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts-site/blob/4d57b53e/source/getting-started/form-validation-using-xml.md ---------------------------------------------------------------------- diff --git a/source/getting-started/form-validation-using-xml.md b/source/getting-started/form-validation-using-xml.md index 7e9a8a1..88e576f 100644 --- a/source/getting-started/form-validation-using-xml.md +++ b/source/getting-started/form-validation-using-xml.md @@ -6,19 +6,13 @@ title: Form validation using XML The example code for this tutorial, form_xml_validation, is available for checkout at [https://github.com/apache/struts-examples](https://github.com/apache/struts-examples) -> +__Introduction__ -#####Introduction##### +In this tutorial we'll cover how to validate a user's input in form fields using Struts 2's XML validation methodology. In the [Form Validation](form-validation.html) tutorial we discussed validating a user's input using the validate method in the Action class. Using a separate XML validation file gives you the ability to use validators built-in to the Struts 2 framework. -In this tutorial we'll cover how to validate a user's input in form fields using Struts 2's XML validation methodology. In the _Form Validation_ tutorial we discussed validating a user's input using the validate method in the Action class. Using a separate XML validation file gives you the ability to use validators built-in to the Struts 2 framework. +The [Struts 2 user mailing list](http://struts.apache.org/mail.html) is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list. - - -| The [Struts 2 user mailing list](http://struts.apache.org/mail.html)^[http://struts.apache.org/mail.html] is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list. - -| - -#####Example Application##### +__Example Application__ The example application that supports this tutorial shows how to use Struts 2's XML validation methodology. The information that can be edited is encapsulated in an object of class Person. @@ -28,110 +22,93 @@ To enable the user to edit his information that is stored in the Person object, When the user submits the form, we want to validate his entries into the form fields. -#####Validation Using XML##### +__Validation Using XML__ To validate a user's form field entries you can use a separate XML file that contains your validation rules. The XML file that contains the validation rules must be named as ActionClassName-validation.xml. In the example application, the XML validation file is named EditAction-validation.xml (see src/main/resources/org/apache/struts/edit/action). -Struts 2 provides several different validators that you can use in the XML validation file. See _Validation_ for a list of validators you can employ. +Struts 2 provides several different validators that you can use in the XML validation file. See [Validation](//struts.apache.org/docs/validation.html) for a list of validators you can employ. -In the above form, we want to ensure the user enters a first name. To have the Struts 2 framework enforce that rule we can used the Struts 2 _requiredstring validator_ . This validator checks that the user has entered a string value in the form field. +In the above form, we want to ensure the user enters a first name. To have the Struts 2 framework enforce that rule we can used the Struts 2 [requiredstring validator](//struts.apache.org/docs/requiredstring-validator.html). This validator checks that the user has entered a string value in the form field. -#####XML Validator Format##### +__XML Validator Format__ In the XML validation file (for this example that is EditAction-validation.xml), is this XML: **XML Validator Required String** - -~~~~~~~ - +```xml + - - personBean.firstName - First name is required. - + + personBean.firstName + First name is required. + +``` +Within the validators node you can have 1 or more validator nodes. The type attribute specifies which validator you want the Struts 2 framework to use (see [Validation](//struts.apache.org/docs/validation.html) ). The param name="fieldname" node is used to tell the framework which form field entry to apply the rule to. See edit.jsp for the form fields and their name value (review [Struts 2 Form Tags](form-tags.html) if you're not familiar with how to use Struts 2 form tags). The message node is used to tell the framework what message to display if the validation fails. -~~~~~~~ - -Within the validators node you can have 1 or more validator nodes. The type attribute specifies which validator you want the Struts 2 framework to use (see _Validation_ ). The param name="fieldname" node is used to tell the framework which form field entry to apply the rule to. See edit.jsp for the form fields and their name value (review [Struts 2 Form Tags](#PAGE_19300595) if you're not familiar with how to use Struts 2 form tags). The message node is used to tell the framework what message to display if the validation fails. - - - -| There are alternate ways to write the XML that goes in the validation XML file. See _Validation_ in the Struts 2 documentation for a full discussion. - -| +| There are alternate ways to write the XML that goes in the validation XML file. See [Validation](//struts.apache.org/docs/validation.html) in the Struts 2 documentation for a full discussion. For example if the user doesn't enter a value in the first name form field and clicks on the Save Changes button, he will see the following. ![form-validation-2.png](attachments/att20873264_form-validation-2.png) -#####Validating An Email Address##### +__Validating An Email Address__ -You can use the Struts 2 _email validator_ to validate the user's input in the email field. Here is the validator node that is in the EditAction-validation.xml file. +You can use the Struts 2 [email validator](//struts.apache.org/docs/email-validator.html) to validate the user's input in the email field. Here is the validator node that is in the `EditAction-validation.xml` file. **Email Validator** - -~~~~~~~ - - personBean.email - Email address is required. - - - personBean.email - Email address not valid. - - - -~~~~~~~ +```xml + + personBean.email + Email address is required. + + + personBean.email + Email address not valid. + +``` Note that in the example, we are requiring the user to enter an email address and then validating the email address the user entered. -#####Validating A User's Input Using A Regular Expression##### +__Validating A User's Input Using A Regular Expression__ -The Struts 2 framework provides a powerful way to validate a user's form field input by using the _regex validator_ . In the example application, we want to ensure the user enters the phone number in the format 999-999-9999. We can use a regular expression and the _regex validator_ to enforce this rule. +The Struts 2 framework provides a powerful way to validate a user's form field input by using the [regex validator](//struts.apache.org/docs/regex-validator.html) . In the example application, we want to ensure the user enters the phone number in the format 999-999-9999. We can use a regular expression and the [regex validator](//struts.apache.org/docs/regex-validator.html) to enforce this rule. **REGEX Validator** - -~~~~~~~ +```xml - personBean.phoneNumber - Phone number is required. - + personBean.phoneNumber + Phone number is required. + - personBean.phoneNumber - - Phone number must be entered as 999-999-9999. + personBean.phoneNumber + + Phone number must be entered as 999-999-9999. - - -~~~~~~~ +``` The param name="expression" node is used to specify the regular expression that will be applied to the user's input. Note how the regular expression is contained within a CDATA section. -#####Validating A User's Input Using An OGNL Expression##### +__Validating A User's Input Using An OGNL Expression__ -In the example application, we want to ensure the user checks at least one of the car model check boxes. To enforce this rule we can use the _fieldexpression validator_ . Here's the XML for that validator node. +In the example application, we want to ensure the user checks at least one of the car model check boxes. To enforce this rule we can use the [fieldexpression validator](//struts.apache.org/docs/fieldexpression-validator.html) . Here's the XML for that validator node. **FieldExpression Validator** - -~~~~~~~ +```xml - personBean.carModels - 0]]> - You must select at least one car model. + personBean.carModels + 0]]> + You must select at least one car model. +``` - -~~~~~~~ - -The param name="expression" node contains an OGNL expression that evaluates to true or false. We haven't previously discussed OGNL, which stands for Object-Graph Navigation Language (see [http://www.opensymphony.com/ognl/](http://www.opensymphony.com/ognl/) and _OGNL_ ). OGNL expressions can be evaluated by the Struts 2 framework as Java statements. +The param name="expression" node contains an OGNL expression that evaluates to true or false. We haven't previously discussed OGNL, which stands for Object-Graph Navigation Language (see [http://www.opensymphony.com/ognl/](http://www.opensymphony.com/ognl/) and [OGNL](//struts.apache.org/docs/ognl.html) ). OGNL expressions can be evaluated by the Struts 2 framework as Java statements. In the above XML the value of the param name="expression" node, personBean.carModels.length \> 0, will be evaluated by the framework as a Java statement. The part personBean.carModels tells the framework to call the getCarModels method of class Person. That method returns an Array. Since class Array has a length attribute, the framework will get the value of the length attribute of the Array returned by the getCarModels method. @@ -141,6 +118,6 @@ If the user did not check any of the check boxes, the Array returned by the getC The fieldexpression validator is useful when doing conditional validation of a user's input. If the OGNL expression doesn't evaluate to true then the user's input won't be allowed. -#####Summary##### +__Summary__ The Struts 2 framework provides easy-to-use validation methodologies. You can add a validate method to the Action class or have a separate XML file with validation rules or you can use a combination of both methodologies.