diff --git a/haxe/ui/core/InteractiveComponent.hx b/haxe/ui/core/InteractiveComponent.hx index d1b9cbdf1..7b7efddb5 100644 --- a/haxe/ui/core/InteractiveComponent.hx +++ b/haxe/ui/core/InteractiveComponent.hx @@ -6,7 +6,7 @@ import haxe.ui.events.FocusEvent; import haxe.ui.events.UIEvent; import haxe.ui.focus.FocusManager; import haxe.ui.focus.IFocusable; -import haxe.ui.validators.IValidator; +import haxe.ui.validators.Validators; /** A component that can be interacted with and gain input focus via either mouse or keyboard @@ -112,19 +112,20 @@ class InteractiveComponent extends Component implements IFocusable implements IV return view; } - private var _validators:Array = null; - @:clonable public var validators(get, set):Array; - private function get_validators():Array { + private var _validators:Validators = null; + @:clonable public var validators(get, set):Validators; + private function get_validators():Validators { return _validators; } - private function set_validators(value:Array):Array { + private function set_validators(value:Validators):Validators { if (value == null) { unregisterEvent(UIEvent.CHANGE, _onInteractiveChange); } _validators = value; if (_validators != null) { registerEvent(UIEvent.CHANGE, _onInteractiveChange); - applyValidators(); + _validators.setup(this); + _validators.validate(this); } return value; } @@ -133,32 +134,9 @@ class InteractiveComponent extends Component implements IFocusable implements IV // Internal //*********************************************************************************************************** - private function applyValidators() { - if (_validators == null) { - return; - } - for (v in _validators) { - if (v == null) { - continue; - } - v.setup(this); - validateComponentValue(); - } - } - - private function validateComponentValue() { - if (_validators == null) { - return; - } - for (v in _validators) { - if (v == null) { - continue; - } - v.validate(this); - } - } - private function _onInteractiveChange(event:UIEvent) { - validateComponentValue(); + if (_validators != null) { + _validators.validate(this); + } } } diff --git a/haxe/ui/validators/Validators.hx b/haxe/ui/validators/Validators.hx new file mode 100644 index 000000000..e7da01a59 --- /dev/null +++ b/haxe/ui/validators/Validators.hx @@ -0,0 +1,87 @@ +package haxe.ui.validators; + +import haxe.ui.core.Component; + +@:forward +@:forward.new +abstract Validators(ValidatorsImpl) from ValidatorsImpl { + @:arrayAccess + public inline function get(index:Int) { + return this.list[index]; + } + + @:from + static function fromArray(list:Array):Validators { + var ret = new ValidatorsImpl(); + ret.list = list; + return ret; + } +} + +private class ValidatorsImpl { + public var list:Array = []; + + public function new() { + } + + public var length(get, null):Int; + private function get_length():Int { + if (list == null) { + return 0; + } + return list.length; + } + + public function setup(component:Component) { + for (item in list) { + if (item == null) { + continue; + } + item.setup(component); + } + } + + private var _isValid:Bool = true; + public var isValid(get, null):Bool; + private function get_isValid():Bool { + return _isValid; + } + + public function validate(component:Component) { + _isValid = true; + for (item in list) { + if (item == null) { + continue; + } + var r = item.validate(component); + if (r != null && r == false) { + _isValid = false; + } + } + } + + public function iterator() { + return new ValidatorsIterator(list); + } +} + +private class ValidatorsIterator { + private var i:Int; + private var list:Array; + + public function new(list:Array) { + this.list = list; + this.i = 0; + } + + public function hasNext() { + if (list == null) { + return false; + } + return i < list.length; + } + + public function next() { + return list[i++]; + } +} \ No newline at end of file