How to always show single validation message in ZF2 validators?

Boris_bulletdodger

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?

edigu

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!',
]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Knockout Validation - How to show a single error message

From Dev

How to show validation errors in the single message Codeigniter?

From Dev

Validation Summary to show a single message

From Dev

Validation message don't show single quote

From Dev

Yii2 validators don't show message

From Dev

zf2 validation: How can I validate dependent fields?

From Dev

How to input a calculated value after form validation in zf2

From Dev

How do I use ZF2 regex validators within the factory pattern

From Dev

how to show json event data on fullcalender in zf2?

From Dev

How SHOW TABLE sql with TABLE GATEWAY ZF2

From Dev

How to get Angular to show the correct validation message

From Dev

how to show textfield validation error message in a label

From Dev

In ngTagsInput how to show validation message for different cases?

From Dev

In ngTagsInput how to show validation message for different cases?

From Dev

angularJS show validation of the message

From Dev

Validation is always firing rule message

From Dev

ZF2 Annotation Validators NotEmpty and Int not working?

From Dev

ZF2 how to set message of required field when it's empty?

From Dev

ZF2 how to set message of required field when it's empty?

From Dev

ZF2 - Always open with https?

From Dev

ZF2 Navigation: How to show some pages ONLY in breadcrumb navigation

From Dev

how to show data from document to index.phtml in zf2?

From Dev

ZF2 navigation. How to show a page in breadcrumbs, but hide this page in menu?

From Dev

How to show utf8 character in ZF2 from MySql

From Dev

Rails + Single Table Inheritance: How to customize validation error message?

From Dev

zf2 session validation failed

From Dev

ZF2 - validation within a bootstrap modal

From Dev

ZF2 : Filedset validation is not working

From Dev

ZF2 nested data validation

Related Related

  1. 1

    Knockout Validation - How to show a single error message

  2. 2

    How to show validation errors in the single message Codeigniter?

  3. 3

    Validation Summary to show a single message

  4. 4

    Validation message don't show single quote

  5. 5

    Yii2 validators don't show message

  6. 6

    zf2 validation: How can I validate dependent fields?

  7. 7

    How to input a calculated value after form validation in zf2

  8. 8

    How do I use ZF2 regex validators within the factory pattern

  9. 9

    how to show json event data on fullcalender in zf2?

  10. 10

    How SHOW TABLE sql with TABLE GATEWAY ZF2

  11. 11

    How to get Angular to show the correct validation message

  12. 12

    how to show textfield validation error message in a label

  13. 13

    In ngTagsInput how to show validation message for different cases?

  14. 14

    In ngTagsInput how to show validation message for different cases?

  15. 15

    angularJS show validation of the message

  16. 16

    Validation is always firing rule message

  17. 17

    ZF2 Annotation Validators NotEmpty and Int not working?

  18. 18

    ZF2 how to set message of required field when it's empty?

  19. 19

    ZF2 how to set message of required field when it's empty?

  20. 20

    ZF2 - Always open with https?

  21. 21

    ZF2 Navigation: How to show some pages ONLY in breadcrumb navigation

  22. 22

    how to show data from document to index.phtml in zf2?

  23. 23

    ZF2 navigation. How to show a page in breadcrumbs, but hide this page in menu?

  24. 24

    How to show utf8 character in ZF2 from MySql

  25. 25

    Rails + Single Table Inheritance: How to customize validation error message?

  26. 26

    zf2 session validation failed

  27. 27

    ZF2 - validation within a bootstrap modal

  28. 28

    ZF2 : Filedset validation is not working

  29. 29

    ZF2 nested data validation

HotTag

Archive