次の2つの電子メールフィールドがあります。最初に標準の電子メールバリデーターを定義し、確認のために回答の最後に添付されたカスタムの電子メールバリデーターを定義します。
<mx:HBox
verticalAlign = "middle"
color = "#101010">
<mx:Label
text = "E-Mail Address:"
width = "120"/>
<mx:TextInput
id = "txtEmail"
width = "220"/>
</mx:HBox>
<mx:HBox
verticalAlign = "middle"
color = "#101010">
<mx:Text
text = "Rewrite E-Mail Address:"
width = "120"/>
<mx:TextInput
id = "txtEmail2"
width = "220"/>
</mx:HBox>
次に、バリデーターを追加します
<mx:EmailValidator
id = "valEmail"
required = "true"
source = "{txtEmail}"
property = "text"
invalidCharError = "Invalid format"
invalidDomainError = "Invalid format"
invalidIPDomainError = "Invalid format"
invalidPeriodsInDomainError = "Invalid format"
missingAtSignError = "Invalid format"
missingPeriodInDomainError = "Invalid format"
missingUsernameError = "Invalid format"
/>
<validators:EmailConfirmationValidator
id = "valEmail2"
required = "true"
source = "{txtEmail2}"
property = "text"
confirmationSource = "{txtEmail}"
confirmationProperty= "text"/>
また、別途、「バリデーター」パッケージ内で宣言したEmailConfirmationValidatorクラスを定義する必要があります。
package validators
{
import mx.validators.ValidationResult;
import mx.validators.Validator;
public class EmailConfirmationValidator extends Validator
{
public var confirmationSource: Object;
public var confirmationProperty: String;
public function EmailConfirmationValidator()
{
super();
}
// Define the doValidation() method.
override protected function doValidation(value:Object):Array
{
// Call base class doValidation().
var results:Array = super.doValidation(value);
if (value.text != value.confirmation)
{
results.push(new ValidationResult(true, null, "Mismatch",
"Emails do not match!"));
}
return results;
}
/**
* @private
* Grabs the data for the confirmation password from its different sources
* if its there and bundles it to be processed by the doValidation routine.
*/
override protected function getValueFromSource():Object
{
var value:Object = {};
value.text = super.getValueFromSource();
if (confirmationSource && confirmationProperty)
{
value.confirmation = confirmationSource[confirmationProperty];
}
return value;
}
}
}
フォームのバリデーターを手動で確認する必要がある場合。たとえば、登録のためにコンテンツを送信する前に、次の方法を使用します
public function isValid():Boolean
{
var validators:Array = [valEmail, valEmail2];
var validatorErrorArray:Array = Validator.validateAll(validators);;
var isValidForm:Boolean = validatorErrorArray.length == 0;
return isValidForm;
}