Struts Validator Validator Guide (based on Struts Validator Guide) Author: David Winterfeldt David James Turner James Rob Leland Robert translation: Hou Si super validator: from 0.5 version, the validator in some form in has been achieved, he was first included in the development In the packet, the core code moved to the Jakarta Commons package and the Struts special extension as part of Struts 1.1. Many developers have always used the Struts validator, which first outlines the core functionality of the validator, and then introduces the changes and new features in Struts1.1. If you configure the validator plugin, you should extend the ValidatorForm instead of Actionform so that it can load your Validator resource. According to the name attribute of the Action in the struts-config.xml file, he is the corresponding verifier corresponding to the current Form, so the name property of the FORM element in Validator-Rules.xml should match the name attribute value of the Action. Another option is to extend the ValidatorActionform instead of validatorform, ValidatorActionForm uses the path property of the action in struts-config.xml, so the value of the Path property should be matched to the FORM of the value in Validator-Rules.xml. A separated action can define each page of multi-page FORM, and the verification rule can be associated with an action rather than the page number, just like the multi-page Form example in the verification example. Internationalization in the Validator-Rules.xml file can organize Formset. Formset has the properties corresponding to the Java.util.Locale class: such as language, country, and variable properties, if they are not defined, the Formset will set it to the default value. A Formset can also have associated constants. You can also define global Global elements with Formset at the same level, and he also has a constant as Formset. Note: You must declare a default formset without an internationalization before the Internationalized Formset. This can have a default version if Validator does not find local. The default error message value of the plug-in verifier can be overwritten by the MSG element. So the alternative method for generating error messages for the Mask verifier is to use the MSG property, if the field's Name property matches the Name property of the validator, the MSG property of the field will use the field. Error Messages can set parameters such as Arg0-Arg3. If no Arg0-Arg3's Name property is set, Error Messages will use them as the default build parameter value. If the Name property is set, you can specify the parameter to a specific pluggable verifier, and then these parameters will be used when constructing an error message.
As an example, a FORM containing the communication address and the mailbox field is considered. If the communication address is not empty, the mailbox field is a must-have required. You can define the ValidWhen rules:
To make a brief description of the program: All comparison must be encapsulated in the Parens. All Comparsons Must Be Enclosed in Parens. You can link and or or or or later. If the two items of comparison can be converted to an integer, use the NuMERIC comparison, otherwise the string is compared. Plugable verifier verification is loaded from the validation.xml file, the default authentication rule defines in the Validation.xml file, defaults Required, Mask, Byte, Short, Int, Long, Float, Double, Date (no local) Support), And a nameic range. "Mask" mode depends on the default value installation requirements, which means "required" can be done, "'Mask" will run the previous "Required" and "Mask" methods to be included in the frame. Any field is not "Required" and is empty or zero length will skip other verification. If you use the JavaScript tag, client JavaScript looks for values in the Validator's JavaScript property and generates an object that verifies the FORM method. To get more detailed explanations about the Detail of JavaScript Validator Tags, see the HTML tag API reference. "'mask'" means you use a regular expression mask to verify fields, which use Jakarta's regular expression pack, all valid rules store in the Validator-Rules.xml file, the main class used by Org.apache. Regexp.re. validation.xml authentication configuration file Example:
Defining the parameters of the plug-in verifier method are some class lists separated by commas, and the method properties need to have a signature that meets the above list. The list is combined by the following: java.lang.object - the bean to verify. Org.apache.commons.validator.validatorAction - Current ValidatorAction. Org.apache.commons.validator.field - the field to verify org.apache.struts.Action.ActionerRORS - If the validation error will join the analog object javax.servlet.http.httpservletRequest - the current REQUEST object. Javax.servlet.ServletContext - Application ServletContext. Org.apache.commons.validator.validator - Current Org.Apache.commons.validator.validator instance. Java.util.locale - the current user's Locale. Multi-page Form field section has an optional page property, which can be set to an integer, all validations of the page are less than or equal to the current page of the server-side authentication, all the fields of the field less than or equal to all fields on the client page Verify the current page of the current page verification less than or equal to the server-side verification. A MUTLI-Part form requires defining page properties:
IF (! genericvalidator.isblankornull (value) {try {if (! value.equals (value2)) {Errors.Add (Field.GetKey (), Resources.getActionError (Application, Request, Va, Field); Return False }}} catch (Exception E) {Errors.Add (Field.getKey (), Resources.getActionError (Application, Request, Va, Field); Return False;}}} Known Bug Struts Validator depends on Commons Validator Package Therefore, problems reporting and enhanced demand may be listed in two products. · Struts Validator Bugzilla Reports · Commons Validator Bugzilla Reports Change and DepRecations New Tag Properties.
The currently used verification XML file is DTD according to Commons-Validator.jar. Struts is not at Validator-Rules.xml and Validator.xml. Separate a separate DTD separately, and Commons-Validator now maintains a unified validator.dtd. Modify all Validator.xml files DTD references to Space field.
The current default ignores blank fields in all base verification types, if you ask a field to be entered, add "Required" in the Depends property defined by the Validator.xml file of your application. New range RANGE method.
The Intrange & FloatRange method has been added in JavaScript and Java. Conditionally Required field.
The biggest modification is the ability to add conditionally Require authentication based on the value of its field. It allows you to define logices such as "Only the x field is not empty" Y field is 'Male' is valid ", this is a recommended method for implementing the above logic, this method will be implemented in the first version of the 1.1 version. The RequireDIF verification rule added in version 1.1 will be removed in the new version. However, if you are preparing to use Requiredif, there is a short tutorial. Let us assume that you have a medical information form with 3 fields, gender SEX, pregnancy test pregnancytest, test results test, if gender is 'f' or 'f', pregnancy test pregnancytest is Required, if pregnancytest is not empty, Test results TestResult are Required.
The input item for your validation.xml file should be like this: