Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] BirthdayType has automatic attr when widget is single_text #58871

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Add `LazyChoiceLoader` and `choice_lazy` option in `ChoiceType` for loading and rendering choices on demand
* Use `form.post_set_data` instead of `form.pre_set_data` in `ResizeFormListener`
* Change the priority of `DataCollectorListener` from 255 to -255
* `BirthdayType` will now have default `attr` values when in `widget` option is set to `single_text`

7.1
---
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BirthdayType extends AbstractType
Expand All @@ -35,4 +37,12 @@ public function getBlockPrefix(): string
{
return 'birthday';
}

public function buildView(FormView $view, FormInterface $form, array $options): void
{
$currentMonth = date('m');
$currentDay = date('d');
$view->vars['attr']['min'] ??= sprintf('%d-%s-%s', reset($options['years']), $currentMonth, $currentDay);
$view->vars['attr']['max'] ??= sprintf('%d-%s-%s', end($options['years']), $currentMonth, $currentDay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,35 @@ public function testSetInvalidYearsOption()
'widget' => 'choice',
]);
}

public function testWidgetSingleTextHasDefaultAttrMinMax()
{
$currentMonth = date('m');
$currentDay = date('d');
$form = $this->factory->create(static::TESTED_TYPE, null, [
'widget' => 'single_text',
]);
$formView = $form->createView();
$options = $form->getConfig()->getOptions();
$expectedMin = sprintf('%d-%s-%s', reset($options['years']), $currentMonth, $currentDay);
$expectedMax = sprintf('%d-%s-%s', end($options['years']), $currentMonth, $currentDay);
$this->assertEquals($expectedMin, $formView->vars['attr']['min']);
$this->assertEquals($expectedMax, $formView->vars['attr']['max']);
}

public function testWidgetSingleTextDoesntRemoveUserAttr()
{
$expectedMin = date('Y-m-d', strtotime('10 years ago'));
$expectedMax = date('Y-m-d', strtotime('1 years ago'));
$form = $this->factory->create(static::TESTED_TYPE, null, [
'widget' => 'single_text',
'attr' => [
'min' => $expectedMin,
'max' => $expectedMax
]
]);
$formView = $form->createView();
$this->assertEquals($expectedMin, $formView->vars['attr']['min']);
$this->assertEquals($expectedMax, $formView->vars['attr']['max']);
}
}
Loading