Skip to content

[Form] Couldn't set invalid_message for DateType #5880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scourgen opened this issue Oct 31, 2012 · 5 comments
Closed

[Form] Couldn't set invalid_message for DateType #5880

scourgen opened this issue Oct 31, 2012 · 5 comments

Comments

@scourgen
Copy link
Contributor

in my case, I have a form,that has a date column,and I want validate this date column like this: If user did something wrong,I would like to tell them what happen(forgot or typed/select the wrong date)

I assume what could happen is like:

  • If user selected (nothing)-(4)-(1),the error message will be "year is missing",
  • If user selected (1980)-(nothing)-(1),the error message will be "month is missing"
  • If user selected (1980)-(nothing)-(nothing),the error message will be "month is missing" and "day is missing"

But I found out that I could't set the invalid_message for each input(year/month/day)

the truth is I can't define the invalid_message separately. When I typed wrong, I always get the error "This value is not valid."

so I spent some time on the code, finally I got this:

Symfony/Component/Form/Extension/Core/Type/DateType.php

            if ('choice' === $options['widget']) {
                // Only pass a subset of the options to children
                $yearOptions['choices'] = $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years']));
                $yearOptions['empty_value'] = $options['empty_value']['year'];
                $monthOptions['choices'] = $this->formatTimestamps($formatter, '/M+/', $this->listMonths($options['months']));
                $monthOptions['empty_value'] = $options['empty_value']['month'];
                $dayOptions['choices'] = $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days']));
                $dayOptions['empty_value'] = $options['empty_value']['day'];
            }

looks like It doesn't pass the invalid_message to field year/month/day . so when the field gets wrong,It will always shows the default message . I can't even change it !

I made a simple fix like following code:

diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php
index cb956e7..32b9399 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php
@@ -84,10 +84,13 @@ class DateType extends AbstractType
                 // Only pass a subset of the options to children
                 $yearOptions['choices'] = $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years']));
                 $yearOptions['empty_value'] = $options['empty_value']['year'];
+                $yearOptions['invalid_message'] = $options['invalid_message_year'];
                 $monthOptions['choices'] = $this->formatTimestamps($formatter, '/M+/', $this->listMonths($options['months']));
                 $monthOptions['empty_value'] = $options['empty_value']['month'];
+                $monthOptions['invalid_message'] = $options['invalid_message_month'];
                 $dayOptions['choices'] = $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days']));
                 $dayOptions['empty_value'] = $options['empty_value']['day'];
+                $dayOptions['invalid_message'] = $options['invalid_message_day'];
             }

             // Append generic carry-along options
@@ -214,6 +217,9 @@ class DateType extends AbstractType
             // this option.
             'data_class'     => null,
             'compound'       => $compound,
+            'invalid_message_day'       => 'This value(day) is not valid.',
+            'invalid_message_month'       => 'This value(month) is not valid.',
+            'invalid_message_year'       => 'This value(year) is not valid.',
         ));

         $resolver->setNormalizers(array(

so I can define them separately like following:

        $form = $this->createFormBuilder()
        ->add('birthdate','birthday',array(
            'empty_value'=>array(
                    'year'=>'year',
                    'month'=>'month',
                    'day'=>'day'
                ),
            'widget'=>'choice',
            'invalid_message'=>'invalid_message for birthday',
                'invalid_message_year'=>'invalid_message for birthday:year',
                'invalid_message_month'=>'invalid_message for birthday:month',
                'invalid_message_day'=>'invalid_message for birthday:day',
        ))->getForm();
@scourgen
Copy link
Contributor Author

btw, by default, If I select (nothing)-(1)-(1) at following form,I will get "This value is not valid." twice

        $form = $this->createFormBuilder()
        ->add('birthdate','birthday',array(
            'empty_value'=>array(
                    'year'=>'year',
                    'month'=>'month',
                    'day'=>'day'
                ),
            'widget'=>'choice'
        ))->getForm();

@scourgen
Copy link
Contributor Author

or we can disable the error_bubbling and assume they are a whole?

@rvanlaak
Copy link
Contributor

rvanlaak commented May 5, 2015

I'd like to edit the invalid_message on a related entity of an object, which is validated using @Assert\Valid(). But as the Valid constraint has no message, it seems like it can't be modified.

So, this does not work:

/**
* @Assert\Valid(message="Custom invalid message")
*/

And this does not work either:

$formBuilder->add('editor', 'entity', array(
    'required'      => true,
    'constraints' => new Valid(array('message' => 'Custom invalid message')),
    'invalid_message' => 'Custom invalid message',
));

@awudarowicz
Copy link

awudarowicz commented Nov 23, 2017

What about this bug? Because it's 2017 and I'm facing now this issue :)

Solution provided by @scourgen looks ok. If you don't want to add separated messages for year, month, day then add just invalid_message to all sub types:

$yearOptions['invalid_message'] = $options['invalid_message'];
$monthOptions['invalid_message'] = $options['invalid_message'];
$dayOptions['invalid_message'] = $options['invalid_message'];

because now when error occurs in one of the sub types (year|month|day) then message is always "This value is not valid." and we cannot change it.

@Simperfit
Copy link
Contributor

@awudarowicz Do you want to provide a fix ?

nicolas-grekas added a commit that referenced this issue Sep 20, 2018
…abbuh)

This PR was merged into the 2.8 branch.

Discussion
----------

[Form] forward the invalid_message option in date types

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #5880
| License       | MIT
| Doc PR        |

Commits
-------

5318e2e forward the invalid_message option in date types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants