Maninder Kumar
Maninder Kumar

Reputation: 1

how to validate mobile number using input filter in entity ZF2

$inputFilter->add($factory->createInput(
    array(
        'name' => 'phone',
        'required' => true,
        'filters' => array(
            array('name' => 'Int'),
        ),
        'validators' => array(
            array(
                'name' => 'Between',
                'options' => array(
                    'min' => 10,
                    'max' => 11,
                ),
            ),
        ),
    )
));

It shows:

The input is not between '6' and '10', inclusively.

Everytime it shows the same message in all cases even in empty

Upvotes: 0

Views: 1076

Answers (2)

Greco Jonathan
Greco Jonathan

Reputation: 2524

When your validate an integer, the between option are the value of the int you input there. Not the length of it.

In this case :

55501015 is not between 6 and 10 inclusively.

7 is.

Your validator Between look like this :

public function isValid($value)
    {
        $this->setValue($value);

        if ($this->getInclusive()) {
            if ($this->getMin() > $value || $value > $this->getMax()) {
                $this->error(self::NOT_BETWEEN);
                return false;
            }
        } else {
            if ($this->getMin() >= $value || $value >= $this->getMax()) {
                $this->error(self::NOT_BETWEEN_STRICT);
                return false;
            }
        }

        return true;
    }

It's not a check for the length, but the value.

Plus, if it's empty, the value of your int is clearly not between 6 or 10 so this is not a problem to me, if you wanna show that the value can't be empty you have tu use another validator in validatorChain.

You have to put it as string and control maybe via a custom validator with a regex on this string as follow :

$inputFilter->add($factory->createInput(
    array(
        'name' => 'phone',
        'required' => true,
        'filters' => array(
            array('name' => 'StripTags'), // don't forget this one
            array('name' => 'StringTrim'),
        ),
        'validators' => array(
            array(
                'name' => 'StringLength',
                'options' => array(
                    'min' => 10,
                    'max' => 11,
                ),
            ),
        ),
    )
));

Upvotes: 0

Wilt
Wilt

Reputation: 44336

You want meant to use the ZF2 StringLength validator instead:

$inputFilter->add($factory->createInput(
    array(
        'name' => 'phone',
        'required' => true,
        'filters' => array(
            array('name' => 'StringTrim'),
        ),
        'validators' => array(
            array(
                'name' => 'StringLength',
                'options' => array(
                    'min' => 10,
                    'max' => 11,
                ),
            ),
        ),
    )
));

As you see I would suggest handling phone number as a string in such case.

Upvotes: 1

Related Questions