API Reference 0.3.24dart_web_toolkit_validationStringValidator

StringValidator class

The StringValidator class validates that the length of a String is within a specified range between minLength and maxLength.

class StringValidator extends WidgetValidator {
 
 /** 
  *  Maximum length for a valid String. A value of null means this property 
  *  is ignored. It is equals null by default.
  */
 int maxLength;
 
 /** 
  *  Error message when the String is longer than the [maxLength] property. By
  *  default it's equals "This string is longer than the maximum allowed length. 
  *  This must be less than {0} characters long."
  */
 String tooLongError = "This string is longer than the maximum allowed length. This must be less than {maxLength} characters long.";
 
 /** 
  *  Minimum length for a valid String. A value of NaN means this property 
  *  is ignored. It is equals null by default.
  */
 int minLength;
 
 /** 
  *  Error message when the string is shorter than the [minLength] property. By
  *  default it's equals "This string is shorter than the minimum allowed length. 
  *  This must be at least {0} characters long."
  */
 String tooShortError =  "This string is shorter than the minimum allowed length. This must be at least {minLength} characters long.";
 
 StringValidator(Widget widget, {this.maxLength:null, this.minLength:null}) : super(widget);
 
 /**
  *  Executes the validation logic of this validator, including validating that 
  *  a missing or empty [value] causes a validation error as defined by the 
  *  value of the [required] property. 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 _validateString(value);
 }
 
 /**
  * Validate [value] and  return List of [ValidationResult] objects, with one
  * ValidationResult object for each field examined by the validator.
  */
 List<ValidationResult> _validateString(dynamic value) {
   List<ValidationResult> results = [];
   
   String val = value != null ? value.toString() : "";
   if (maxLength != null && val.length > maxLength) {
     results.add(new ValidationResult(true, substitute(tooLongError, '{maxLength}', maxLength)));
   } else if (minLength != null && val.length < minLength) {
     results.add(new ValidationResult(true,  substitute(tooShortError, '{minLength}', minLength)));
   }

   return results;
 }
}

Extends

Validator > WidgetValidator > StringValidator

Constructors

new StringValidator(Widget widget, {int maxLength: null, int minLength: null}) #

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
StringValidator(Widget widget, {this.maxLength:null, this.minLength:null}) : super(widget);

Properties

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

List<ValidationListener> listeners #

inherited from Validator

List of ValidationListener's

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

int maxLength #

Maximum length for a valid String. A value of null means this property is ignored. It is equals null by default.

int maxLength

int minLength #

Minimum length for a valid String. A value of NaN means this property is ignored. It is equals null by default.

int minLength

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."

String tooLongError #

Error message when the String is longer than the maxLength property. By default it's equals "This string is longer than the maximum allowed length. This must be less than {0} characters long."

String tooLongError = "This string is longer than the maximum allowed length. This must be less than {maxLength} characters long."

String tooShortError #

Error message when the string is shorter than the minLength property. By default it's equals "This string is shorter than the minimum allowed length. This must be at least {0} characters long."

String tooShortError =  "This string is shorter than the minimum allowed length. This must be at least {minLength} characters long."

Widget widget #

inherited from WidgetValidator
Widget widget

Methods

List<ValidationResult> doValidation(value) #

Executes the validation logic of this validator, including validating that a missing or empty value causes a validation error as defined by the value of the required property. 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 _validateString(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;
}