WidgetValidator abstract class
abstract class WidgetValidator extends Validator { Widget widget; WidgetValidator(this.widget) { if (widget is HasAllKeyHandlers) { (widget as HasAllKeyHandlers).addKeyUpHandler(new KeyUpHandlerAdapter((IEvent evt){ _doValidate(); })); } if (widget is HasChangeHandlers) { (widget as HasChangeHandlers).addChangeHandler(new ChangeHandlerAdapter((IEvent evt){ _doValidate(); })); } } ValidationEvent _doValidate() { return validate((widget as HasValue).getValue()); } /** * Invokes all the validators in the [validators] Array. * Returns true if all validators valid other false. */ static bool validateAll(List<WidgetValidator> validators) { assert(validators != null); return !validators.any((WidgetValidator validator){ if (validator.enabled) { ValidationEvent resultEvent = validator._doValidate(); if (resultEvent.type == ValidationEvent.INVALID) { return true; } } return false; }); } }
Extends
Validator > WidgetValidator
Subclasses
NumberValidator, PhoneNumberValidator, RegExpValidator, SocialSecurityNumberValidator, StringValidator
Static Methods
bool validateAll(List<WidgetValidator> validators) #
Invokes all the validators in the validators Array. Returns true if all validators valid other false.
static bool validateAll(List<WidgetValidator> validators) { assert(validators != null); return !validators.any((WidgetValidator validator){ if (validator.enabled) { ValidationEvent resultEvent = validator._doValidate(); if (resultEvent.type == ValidationEvent.INVALID) { return true; } } return false; }); }
Constructors
new WidgetValidator(Widget widget) #
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.
WidgetValidator(this.widget) { if (widget is HasAllKeyHandlers) { (widget as HasAllKeyHandlers).addKeyUpHandler(new KeyUpHandlerAdapter((IEvent evt){ _doValidate(); })); } if (widget is HasChangeHandlers) { (widget as HasChangeHandlers).addChangeHandler(new ChangeHandlerAdapter((IEvent evt){ _doValidate(); })); } }
Properties
bool enabled #
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 #
List of ValidationListener's
List<ValidationListener> listeners = new List<ValidationListener>()
OnInvalid onInvalid #
Callback function invokes when result of validation is invalid.
OnInvalid onInvalid
OnValid onValid #
Callback function invokes when result of validation is valid.
OnValid onValid
bool required #
If true specifies that a missing or empty value causes a validation error.
bool required = true
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 = []; ValidationResult result = _validateRequired(value); if (result != null) results.add(result); return results; }
ValidationEvent validate(value, [bool fireEvent = true]) #
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; }