This project is archived and is in readonly mode.

#568 ✓ resolved
Nico

new Validator : length & validate-zero-integer

Reported by Nico | May 11th, 2011 @ 11:25 AM | in 1.4.0.1 (closed)

In Validtor we have MinLength and MaxLength but not a precise length.

validate-integer don't accept initial zero (01568764 => like a phone number, but we don't want space or ".") so we can add validate-zero-integer ?

Locale.define('fr-FR', 'FormValidator', {
    'length': "ce champ doit faire {length} caractères (vous avez entré {elLength} caractères)",
});
Locale.define('en-US', 'FormValidator', {
    'length': "Please enter {length} characters (you entered {elLength} characters)",
});



Form.Validator.add('length', {
    errorMsg: function(element, props){
        if (typeOf(props.length) != 'null')
            return Form.Validator.getMsg('length').substitute({length:props.length,elLength:element.get('value').length });
        else return '';
    },
    test: function(element, props){
        if (typeOf(props.length) != 'null') return (element.get('value').length == props.length || element.get('value').length == 0);
        else return true;
    }
});

Form.Validator.add('validate-zero-integer', {
    errorMsg: Form.Validator.getMsg.pass('integer'),
    test: function(element){
        return Form.Validator.getValidator('IsEmpty').test(element) || (/^(-?[0-9]\d*|0)$/).test(element.get('value'));
    }
});

Comments and changes to this ticket

  • Arian

    Arian May 15th, 2011 @ 12:33 PM

    • State changed from “new” to “open”
    • Milestone set to 1.4.0.1
    • Assigned user set to “Aaron Newton”
    • Milestone order changed from “197598” to “0”

    The cool thing is that you can define your own validators for those that are not by default in Form.Validator. However if Aaron approves your code, we can add it.

    Why are you allowing length == 0?

  • Nico

    Nico May 15th, 2011 @ 02:50 PM

    because "0" is the work of "required", may be the input is not mandatory and if he is not empty he must have a length.

  • Aaron Newton

    Aaron Newton May 17th, 2011 @ 01:07 AM

    The length validator is fine by me - do you want to send a pull request? i.e. write docs and a spec for it and send a pull on github? If not that's fine.

    As for the validate-zero-integer, I don't get it; doesn't the current validator allow for 0? that's what that |0 does, yeah? Am I reading that wrong?

  • Nico

    Nico May 17th, 2011 @ 08:43 AM

    validate-integer allow zero alone but don't allow initial zero like 0123456789, it's wrong for him.

  • Aaron Newton

    Aaron Newton May 18th, 2011 @ 09:53 PM

    Yeah, I don't think that validator is likely to be included, but the length one makes sense. If you want to do this coding and send us a pull request, that would be awesome, but other wise will get to it as we have time. Thanks for the info.

  • Arian

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The MooTools Extensions

Shared Ticket Bins

People watching this ticket

Pages