How to always show single validation message in ZF2 validators?

442 Views Asked by At

I have the following input:

private function addBirthdayElement()
{
    return $this->add(
        array(
            'type'       => 'DateSelect',
            'name'       => 'x_bdate',
            'options'    => [
                'label'             => 'astropay_birthday',
                'label_attributes'  => array(
                    'class' => 'astropay-label'
                ),
                'create_empty_option' => true,
                'render_delimiters' => false,
            ],
            'attributes' => array(
                'required' => true,
                'class' => 'astropay-input',
            )
        )
    );
}

It has the following filter:

public function addBirthdayFilter()
{
    $time = new \DateTime('now');
    $eighteenYearAgo = $time->modify(sprintf('-%d year', self::EIGHTEEN_YEARS))->format('Y-m-d');

    $this->add(
        [
            'name'       => 'x_bdate',
            'required'   => true,
            'validators' => [
                [
                    'name'                   => 'Between',
                    'break_chain_on_failure' => true,
                    'options'                => [
                        'min'      => 1900,
                        'max'      => $eighteenYearAgo,
                        'messages' => [
                            Between::NOT_BETWEEN        => 'astropay_invalid_birth_date_18',
                            Between::NOT_BETWEEN_STRICT => 'astropay_invalid_birth_date_18',
                        ]
                    ]
                ],
                [
                    'name'                   => 'Date',
                    'break_chain_on_failure' => true,
                    'options'                => [
                        'messages' => [
                            Date::INVALID      => 'astropay_invalid_birth_date',
                            Date::FALSEFORMAT  => 'astropay_invalid_birth_date',
                            Date::INVALID_DATE => 'astropay_invalid_birth_date',
                        ],
                    ]
                ],
            ],
        ]
    );

    return $this;
}

However, putting an empty date, I get the error message defined for: Date::INVALID_DATE

But it's not the overridden one. The break_chain_on_failure works for the two validators I have defined, but the default Zend message is always there. For example I get this as an error in my form:

The input does not appear to be a valid date
astropay_invalid_birth_date_18

How can I display only the overidden error messages and 1 at a time?

1

There are 1 best solutions below

0
On BEST ANSWER

You can use a message key in your validator configuration instead of a messages array to always show a single message per validator.

For example, replace this:

'options' => [
    'messages' => [
        Date::INVALID      => 'astropay_invalid_birth_date',
        Date::FALSEFORMAT  => 'astropay_invalid_birth_date',
        Date::INVALID_DATE => 'astropay_invalid_birth_date',
    ],
]

with this one:

'options' => [
    'message' => 'Invalid birth date given!',
]