Description
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();