API Reference 0.3.24dart_web_toolkit_validationRegExpValidator

RegExpValidator class

The RegExpValidator class lets you use a regular expression to validate a field. You pass a regular expression to the validator using the expression property, and additional flags to control the regular expression pattern matching using the flags property.

The validation is successful if the validator can find a match of the regular expression in the field to validate. A validation error occurs when the validator finds no match.

class RegExpValidator extends WidgetValidator {
 
 RegExp _regExp;
 
 bool _foundMatch = false;
 
 /**
  * The regular expression to use for validation.
  */
 String _expression;
 
 void set expression(String value) {
   _expression = value;
   _createRegExp();
 }
 
 String get expression => _expression;
 
 /**
  * The regular expression multiLine flag to use when matching.
  */
 bool _multiLine = false;
 
 void set multiLine(bool value) {
   _multiLine = value;
   _createRegExp();
 }
 
 bool get multiLine => _multiLine;
 
 /**
  * The regular expression caseSensitive flag to use when matching.
  */
 bool _caseSensitive = true;
 
 void set caseSensitive(bool value) {
   _caseSensitive = value;
   _createRegExp();
 }
 
 bool get caseSensitive => _caseSensitive;
 
 /**
  * Error message when there is no regular expression specifed. 
  * By default value is "The expression is missing."
  */
 String noExpressionError;
 
 /**
  * Error message when there are no matches to the regular expression. 
  * By default value is "The field is invalid."
  */
 String noMatchError;
 
 RegExpValidator(Widget widget, String expression, {
   bool multiLine:false,
   bool caseSensitive:true,
   this.noExpressionError:"The expression is missing.",
   this.noMatchError:"The field is invalid."}) : super(widget) {
   
   this.expression = expression;
   this.multiLine = multiLine;
   this.caseSensitive = caseSensitive;
 }
 
 /**
  * Create RegExp as combination of [expression], [multiLine] and [caseSensitive].
  */
 RegExp _createRegExp() {
   if (_expression != null) {
     _regExp = new RegExp(_expression, multiLine:multiLine, caseSensitive:caseSensitive);
   }
 }
 
 /**
  *  Executes the validation logic of this validatorto validate a regular 
  *  expression. For an invalid result returns an List of [ValidationResult] 
  *  objects, with one ValidationResult object for each field examined by the 
  *  validator that failed validation.
  *
  *  You must override this method in a subclass of a validator class.
  */
 List<ValidationResult> doValidation(dynamic value) {
   List<ValidationResult> results = super.doValidation(value);

   // Return if there are errors or if the required property is set to false 
   // and length is 0.
   String val = value != null ? value.toString() : "";
   
   if (results.length > 0 || ((val.length == 0) && !required))
     return results;
   else
     return _validateRegExpression(value);
 }
 
 /**
  * Validate [value] and  return List of [ValidationResult] objects, with one
  * ValidationResult object for each field examined by the validator.
  */
 List<ValidationResult> _validateRegExpression(dynamic value) {
   List<ValidationResult> results = [];
   
   _foundMatch = false;
   
   if (_regExp != null && expression != "") {
     List result = _regExp.allMatches(value.toString()).toList();
     
     result.forEach((Match match){
       results.add(new RegExpValidationResult(false, "", matchedString:match.str, matchedIndex:match.start));
       _foundMatch = true;
     });
     
     if (results.length == 0) {
       results.add(new ValidationResult(true, noMatchError));
     }
   } else {
     results.add(new ValidationResult(true, noExpressionError));
   }
   
   return results;
 }
}

Extends

Validator > WidgetValidator > RegExpValidator

Constructors

new RegExpValidator(Widget widget, String expression, {bool multiLine: false, bool caseSensitive: true, String noExpressionError: "The expression is missing.", String noMatchError: "The field is invalid."}) #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
RegExpValidator(Widget widget, String expression, {
 bool multiLine:false,
 bool caseSensitive:true,
 this.noExpressionError:"The expression is missing.",
 this.noMatchError:"The field is invalid."}) : super(widget) {
 
 this.expression = expression;
 this.multiLine = multiLine;
 this.caseSensitive = caseSensitive;
}

Properties

bool caseSensitive #

bool get caseSensitive => _caseSensitive;
void set caseSensitive(bool value) {
 _caseSensitive = value;
 _createRegExp();
}

bool enabled #

inherited from Validator

Property to enable/disable validation process.

Setting this value to false will stop the validator from performing validation. When a validator is disabled, it dispatches no events, and the validate method returns ValidationEvent.VALID instance.

bool enabled = true

String expression #

String get expression => _expression;
void set expression(String value) {
 _expression = value;
 _createRegExp();
}

List<ValidationListener> listeners #

inherited from Validator

List of ValidationListener's

List<ValidationListener> listeners = new List<ValidationListener>()

bool multiLine #

bool get multiLine => _multiLine;
void set multiLine(bool value) {
 _multiLine = value;
 _createRegExp();
}

String noExpressionError #

Error message when there is no regular expression specifed. By default value is "The expression is missing."

String noExpressionError

String noMatchError #

Error message when there are no matches to the regular expression. By default value is "The field is invalid."

String noMatchError

OnInvalid onInvalid #

inherited from Validator

Callback function invokes when result of validation is invalid.

OnInvalid onInvalid

OnValid onValid #

inherited from Validator

Callback function invokes when result of validation is valid.

OnValid onValid

bool required #

inherited from Validator

If true specifies that a missing or empty value causes a validation error.

bool required = true

String requiredError #

inherited from Validator

Error message when a value is missing and the required property is true.

By default equals "This field is required."

String requiredError = "This field is required."

Widget widget #

inherited from WidgetValidator
Widget widget

Methods

List<ValidationResult> doValidation(value) #

Executes the validation logic of this validatorto validate a regular expression. For an invalid result returns an List of ValidationResult objects, with one ValidationResult object for each field examined by the validator that failed validation.

You must override this method in a subclass of a validator class.

List<ValidationResult> doValidation(dynamic value) {
 List<ValidationResult> results = super.doValidation(value);

 // Return if there are errors or if the required property is set to false 
 // and length is 0.
 String val = value != null ? value.toString() : "";
 
 if (results.length > 0 || ((val.length == 0) && !required))
   return results;
 else
   return _validateRegExpression(value);
}

ValidationEvent validate(value, [bool fireEvent = true]) #

inherited from Validator

Performs validation of value and notifies the listeners of the result if fireEvent equals true.

ValidationEvent validate(dynamic value, [bool fireEvent = true]) {
 ValidationEvent resultEvent;
 if (enabled && (value != null || required)) {
   // Validate if the target is required or our value is non-null.
   resultEvent = _processValidation(value);
 } else {
   // We assume if value is null and required is false that
   // validation was successful.
   resultEvent = new ValidationEvent(ValidationEvent.VALID);
 }
 // Send notification to listeners
 if (fireEvent && resultEvent != null) {
   listeners.forEach((ValidationListener listener){
     listener(resultEvent);
   });
 }
 // Invoke callback validation functions
 if (onValid != null && resultEvent.type == ValidationEvent.VALID) {
   onValid();
 } else if (onInvalid != null && resultEvent.type == ValidationEvent.INVALID) {
   onInvalid(resultEvent.results);
 }
 return resultEvent;
}