开发者

How to remove validation programmatically from flex component

开发者 https://www.devze.com 2022-12-20 07:49 出处:网络
How to remove validation programmatically from flex component This is my method public static function validateRequired(txt:TextInput, errorMessage:String=\"This field is required\"):Boolean

How to remove validation programmatically from flex component This is my method

public static function validateRequired(txt:TextInput, errorMessage:String="This field is required"):Boolean
        {
                var v:Validator = new Validator();

                v.listener = txt;
                var result:ValidationResultEvent = v.validate(txt.text);
                var returnResult:Boolean = (result.type == ValidationResultEvent.VALID);
                //Alert.show("validation result is " + returnResult);
                if (!returnResult) {
                    v.requiredFieldError = errorMessage;
                }
                return returnResult;
        }

But, as each time i am creating new validator, so pop-up contains multiple messages like

This field is required.

This field is required.

How to remove error messages attached with component开发者_JAVA技巧?


I had the same problem, I understood that i had to clear the last validation before the next one.

private function resetValidationWarnings():void {
                for each (var validator:Validator in arrValidators) {
                    validator.dispatchEvent(new ValidationResultEvent(ValidationResultEvent.VALID));
                }
            }

this is a kinda POG but it got the job done!

hope it helps !


The Validator.enabled property lets you enable and disable a validator. When the value of the enabled property is true, the validator is enabled; when the value is false, the validator is disabled. When a validator is disabled, it dispatches no events, and the validate() method returns null.

For example, you can set the enabled property by using data binding, as the following code shows:

<?xml version="1.0"?>
<!-- validators\EnableVal.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> 

    <mx:ZipCodeValidator id="zcVal" 
        source="{inputA}" 
        property="text" 
        required="true" 
        enabled="{enableV.selected}"/>

    <mx:TextInput id="inputA"/> 
    <mx:TextInput/> 
    <mx:CheckBox id="enableV" 
        label="Validate input?"/>
</mx:Application>


I also encountered similar problem. In my case, the root cause is I created the validator object every time the validation is called (just as you did). As a result, the UIComponent see it as different validator object (see UIComponent.errorObjectArray) and stored the error message again. The solution is to have a global or static validator and it solves the duplicate error message for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消