Professional Jakarta Struts [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Professional Jakarta Struts [Electronic resources] - نسخه متنی

James Goodwill, Richard Hightower

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید












Extending Validate and Custom Validation in the Validate Method of an Action


There are two main types of form validation. The first type is per field—in other words, the validation takes into account only one field at a time. The Validator Framework excels with this type of form validation.

The other type of form validation is the relationship between two fields. For example, let's say that your product form has a price and a list price field. The rule is that the list price should never be less than the price. You can add this validation in the ActionForm's validate method, but you could argue that this type of validation is more of a business rule than true validation.

Another example comes to mind. Let's say that you need the end user to enter two passwords. One is the password itself, and the second is to ensure that users entered what they thought they did. To write this, you could choose to override the validate method of the ValidatorForm class.

The ValidatorForm is needed to integrate the Validator Framework to Struts. The ValidatorForm subclasses ActionForm and overrides the validate method to use the framework. You can take this a step further by overriding the validate method of ValidatorForm and extending the functionality of the ValidatorForm to provide your own custom validation by following these steps:



    Override the validate method.



    Call the superclass validate method.



    Save a reference to the errors (if any) that the ValidatorForm superclass produces.



    Perform additional error checking; add errors.



    Return the errors.




To add password support to our ActionForm, add the following code:


import org.apache.struts.validator.ValidatorForm;
public class InputFormAll extends ValidatorForm {
private String userName;
private String birthDate;
private String email;
private String creditCard;
private String firstName;
private String lastName;
private String middleName;
private String website;
private String password=";
private String passwordCheck=";
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request){
ActionErrors errors =
super.validate(mapping, request);
if (!(password.equals(passwordCheck))){
errors.add(
"password",
new ActionError("errors.password.nomatch"));
}
return errors;
}

You can use this approach anytime you want to add validation that the Validator Framework does not yet provide. Now this is one way to extend the Validator Framework without writing your own custom rules. However, you could just write your own validation rule; in the next section, we show you how.

/ 183