views:

86

answers:

2

Hi,

I've got a class that's meant to validate input fields to make sure the value is always a decimal. I've tested the regex here: http://livedocs.adobe.com/flex/3/html/help.html?content=validators_7.html, and it looks like it does the right thing, but in my app, I can't seem to get it to match to a number format.

Class Definition:

public class DecimalValidator {
    //------------------------------- ATTRIBUTES
    public var isDecimalValidator:RegExpValidator;

    //------------------------------- CONSTRUCTORS
    public function DecimalValidator() { 
        isDecimalValidator                  = new RegExpValidator();
        isDecimalValidator.expression       = "^-?(\d+\.\d*|\.\d+)$";
        isDecimalValidator.flags            = "g";
        isDecimalValidator.required         = true;
        isDecimalValidator.property         = "text";
        isDecimalValidator.triggerEvent     = FocusEvent.FOCUS_OUT;
        isDecimalValidator.noMatchError     = "Float Expected";
    }

}

Setting the source here:

public function registerDecimalInputValidator(inputBox:TextInput, valArr:Array):void  {
        // Add Validators
        var dValidator:DecimalValidator = new DecimalValidator();
        dValidator.isDecimalValidator.source = inputBox;
        dValidator.isDecimalValidator.trigger = inputBox;

        inputBox.restrict = "[0-9].\\.\\-";
        inputBox.maxChars = 10;

        valArr.push(dValidator.isDecimalValidator);
    }

And Calling it here:

registerDecimalInputValidator(textInput, validatorArr);

Where textInput is an input box created earlier.

Clearly I'm missing something simple yet important, but I'm not entirely sure what! Any help would be much appreciated.

+1  A: 

This strikes me as wrong; but I can't quite put my finger on it. For your DecimalValidator instead of composing a RegExpValidator; why not extend it?

public class DecimalValidator extend RegExpValidator{

//------------------------------- CONSTRUCTORS
public function DecimalValidator() {
   super()

    this.expression       = "^-?(\d+\.\d*|\.\d+)$";
    this.flags            = "g";
    this.required         = true;
    this.property         = "text";
    this.triggerEvent     = FocusEvent.FOCUS_OUT;
    this.noMatchError     = "Float Expected";
}
}

How when is the registerdecimalInputValidator called? I have a slight worry about the Validator instance is a local variable to a method instead of 'global' property to the function.

protected var dValidator:DecimalValidator = new DecimalValidator();

public function registerDecimalInputValidator(inputBox:TextInput):void  {
    dValidator.isDecimalValidator.source = inputBox;
    dValidator.isDecimalValidator.trigger = inputBox;

}

I'm not sure why you are setting restrictions on the TextInput in the registerDecimalInputValidator method; that should be done when you create the method (in createChildren() or possibly in response to public properties changing, in commitProperties . It is also not obvious to me what the validatorArr does. If you're expecting to access values inside the validatorArrray outside of the method; it would often be a common practice to return that value from the method. Without looking it up; I'm not sure if Arrays are passed by value or reference in Flex.

www.Flextras.com
Fair point about using extends. Originally I was trying to create a form validator so have numeorous validator inside. The application requires forms to be created on the fly, and each input box has its own restrictions. validatorArr is an array passed by the form creator to the inbut box creators (boxes may have values to be stored etc. it just allwos me to recheck the form before submission. I have validators for text input and integer input and the seem to work hapily. Ignore the text input restriction, that's just me trying to test this regex thing.
looking at the issue of the validator instance being a local var. It's definitely called properly on my numberValidator and StringValidator. Using a similar tactic. For the RegExpValidator, its not so much that it is ignored, but that it says EVERYTHING is wrong... :-(
Haha... was a backslash problem. I'll be extending RegExpValidator though :-)
+1  A: 

I don't know ActionScript, but as far as I know it's an ECMAScript language, so I expect you need to escape the backslashes if you use a string to define a regex:

isDecimalValidator.expression       = "^-?(\\d+\\.\\d*|\\.\\d+)$";
Tim Pietzcker
hahahaha!!! As suspected.... its always a small problem in the end!!!Thanks!!!