zendframework / zend-i18n

I18n component from Zend Framework
BSD 3-Clause "New" or "Revised" License
65 stars 49 forks source link

Added test to check if possible to set country with context #42

Closed svycka closed 8 years ago

svycka commented 8 years ago

I guess this was desired behavior but didn't worked.

this is extremely useful for example with input filters, example:

class User extends InputFilter
{
    public function __construct()
    {
        $this->add([
            'name'       => 'country_code',
            'required'   => true,
            'validators' => [
                new CountryCode()
            ],
        ]);
        $this->add([
            'name'       => 'phone',
            'required'   => true,
            'validators' => [
                [
                    'name' => 'PhoneNumber',
                    'options' => [
                        'country' => 'country_code',
                    ]
                ],
        ]);
    }
}

before I had to wrap PhoneNumber into callback validator and pass country in constructor.

svycka commented 8 years ago

workaround example:

class User extends InputFilter
{
    public function __construct()
    {
        $this->add([
            'name'       => 'country_code',
            'required'   => true,
            'validators' => [
                new CountryCode()
            ],
        ]);
        $this->add([
            'name'       => 'phone',
            'required'   => true,
            'validators' => [
                'name' => 'Zend\Validator\Callback',
                'options'  => array(
                    'messages' => [
                        \Zend\Validator\Callback::INVALID_VALUE => 'Invalid phone number.',
                    ],
                    'callback' => function ($phone, $context) {

                        $phoneValidator = new PhoneNumber([
                            'country' => $context['country_code'],
                        ]);
                        return $phoneValidator->isValid($phone);
                    },
                ),
            ],
        ]);
    }
}
weierophinney commented 8 years ago

Thanks, @svycka