Skip to content

[DX][Form] Change default value of "required" option to false #12018

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
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function testSetDataMultipleNonExpandedNull()
public function testSubmitSingleExpandedNull()
{
$field = $this->factory->createNamed('name', 'entity', null, array(
'required' => true,
'multiple' => false,
'expanded' => true,
'em' => 'default',
Expand All @@ -257,6 +258,7 @@ public function testSubmitSingleExpandedNull()
public function testSubmitSingleNonExpandedNull()
{
$field = $this->factory->createNamed('name', 'entity', null, array(
'required' => true,
'multiple' => false,
'expanded' => false,
'em' => 'default',
Expand All @@ -271,6 +273,7 @@ public function testSubmitSingleNonExpandedNull()
public function testSubmitMultipleNull()
{
$field = $this->factory->createNamed('name', 'entity', null, array(
'required' => true,
'multiple' => true,
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* added "html5" option to Date, Time and DateTimeFormType to be able to
enable/disable HTML5 input date when widget option is "single_text"
* the default value of the "required" option was changed to false

2.5.0
------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ private function addSubForms(FormBuilderInterface $builder, array $choiceViews,
$choiceOpts = array(
'value' => $choiceView->value,
'label' => $choiceView->label,
'required' => $options['required'],
'translation_domain' => $options['translation_domain'],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'data_class' => $dataClass,
'empty_data' => $emptyData,
'trim' => true,
'required' => true,
'required' => false,
'read_only' => false,
'max_length' => null,
'pattern' => null,
Expand Down
18 changes: 9 additions & 9 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$secondOptions['choices'] = $seconds;
$secondOptions['placeholder'] = $options['placeholder']['second'];
}
}

// Append generic carry-along options
foreach (array('required', 'translation_domain') as $passOpt) {
$hourOptions[$passOpt] = $options[$passOpt];
// Append generic carry-along options
foreach (array('required', 'translation_domain') as $passOpt) {
$hourOptions[$passOpt] = $options[$passOpt];

if ($options['with_minutes']) {
$minuteOptions[$passOpt] = $options[$passOpt];
}
if ($options['with_minutes']) {
$minuteOptions[$passOpt] = $options[$passOpt];
}

if ($options['with_seconds']) {
$secondOptions[$passOpt] = $options[$passOpt];
}
if ($options['with_seconds']) {
$secondOptions[$passOpt] = $options[$passOpt];
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/FormConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
/**
* @var bool
*/
private $required = true;
private $required = false;

/**
* @var bool
Expand Down
25 changes: 22 additions & 3 deletions src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ public function testRowOverrideVariables()

$this->assertMatchesXpath($html,
'/div
[
./label[@for="name"][@class="my&label&class"][.="[trans]foo&bar[/trans]"]
/following-sibling::input[@id="name"][@class="my&class"]
]
'
);
}

public function testRowOverrideVariablesRequiredField()
{
$view = $this->factory->createNamed('name', 'text', null, array('required' => true))->createView();
$html = $this->renderRow($view, array(
'attr' => array('class' => 'my&class'),
'label' => 'foo&bar',
'label_attr' => array('class' => 'my&label&class'),
));

$this->assertMatchesXpath($html,
'/div
[
./label[@for="name"][@class="my&label&class required"][.="[trans]foo&bar[/trans]"]
/following-sibling::input[@id="name"][@class="my&class"]
Expand Down Expand Up @@ -525,7 +544,7 @@ public function testRepeatedWithCustomOptions()
{
$form = $this->factory->createNamed('name', 'repeated', null, array(
// the global required value cannot be overridden
'first_options' => array('label' => 'Test', 'required' => false),
'first_options' => array('label' => 'Test', 'required' => true),
'second_options' => array('label' => 'Test2'),
));

Expand All @@ -535,12 +554,12 @@ public function testRepeatedWithCustomOptions()
./div
[
./label[@for="name_first"][.="[trans]Test[/trans]"]
/following-sibling::input[@type="text"][@id="name_first"][@required="required"]
/following-sibling::input[@type="text"][@id="name_first"]
]
/following-sibling::div
[
./label[@for="name_second"][.="[trans]Test2[/trans]"]
/following-sibling::input[@type="text"][@id="name_second"][@required="required"]
/following-sibling::input[@type="text"][@id="name_second"]
]
/following-sibling::input[@type="hidden"][@id="name__token"]
]
Expand Down
72 changes: 65 additions & 7 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,18 @@ public function testLabel()
);
}

public function testLabelOnForm()
public function testLabelForRequiredField()
{
$form = $this->factory->createNamed('name', 'date');
$form = $this->factory->createNamed('name', 'text', null, array(
'required' => true,
));
$view = $form->createView();
$this->renderWidget($view, array('label' => 'foo'));
$html = $this->renderLabel($view);

$this->assertMatchesXpath($html,
'/label
[@for="name"]
[@class="required"]
[.="[trans]Name[/trans]"]
'
Expand Down Expand Up @@ -222,7 +225,6 @@ public function testLabelDoesNotRenderFieldAttributes()
$this->assertMatchesXpath($html,
'/label
[@for="name"]
[@class="required"]
'
);
}
Expand All @@ -238,6 +240,25 @@ public function testLabelWithCustomAttributesPassedDirectly()

$this->assertMatchesXpath($html,
'/label
[@for="name"]
[@class="my&class"]
'
);
}

public function testRequiredLabelWithCustomClass()
{
$form = $this->factory->createNamed('name', 'text', null, array(
'required' => true,
));
$html = $this->renderLabel($form->createView(), null, array(
'label_attr' => array(
'class' => 'my&class',
),
));

$this->assertMatchesXpath($html,
'/label
[@for="name"]
[@class="my&class required"]
'
Expand All @@ -256,7 +277,7 @@ public function testLabelWithCustomTextAndCustomAttributesPassedDirectly()
$this->assertMatchesXpath($html,
'/label
[@for="name"]
[@class="my&class required"]
[@class="my&class"]
[.="[trans]Custom label[/trans]"]
'
);
Expand All @@ -277,7 +298,7 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
$this->assertMatchesXpath($html,
'/label
[@for="name"]
[@class="my&class required"]
[@class="my&class"]
[.="[trans]Custom label[/trans]"]
'
);
Expand Down Expand Up @@ -366,6 +387,7 @@ public function testSingleChoice()
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
'required' => true,
'multiple' => false,
'expanded' => false,
));
Expand Down Expand Up @@ -400,6 +422,7 @@ public function testSingleChoiceWithPreferred()
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
'preferred_choices' => array('&b'),
'required' => true,
'multiple' => false,
'expanded' => false,
));
Expand All @@ -423,6 +446,7 @@ public function testSingleChoiceWithPreferredAndNoSeparator()
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
'preferred_choices' => array('&b'),
'required' => true,
'multiple' => false,
'expanded' => false,
));
Expand All @@ -445,6 +469,7 @@ public function testSingleChoiceWithPreferredAndBlankSeparator()
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
'preferred_choices' => array('&b'),
'required' => true,
'multiple' => false,
'expanded' => false,
));
Expand All @@ -468,6 +493,7 @@ public function testChoiceWithOnlyPreferred()
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
'preferred_choices' => array('&a', '&b'),
'required' => true,
'multiple' => false,
'expanded' => false,
));
Expand Down Expand Up @@ -703,6 +729,7 @@ public function testSingleChoiceExpanded()
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
'required' => true,
'multiple' => false,
'expanded' => true,
));
Expand All @@ -725,6 +752,7 @@ public function testSingleChoiceExpandedWithPlaceholder()
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
'required' => true,
'multiple' => false,
'expanded' => true,
'placeholder' => 'Test&Me',
Expand All @@ -750,6 +778,7 @@ public function testSingleChoiceExpandedWithBooleanValue()
{
$form = $this->factory->createNamed('name', 'choice', true, array(
'choices' => array('1' => 'Choice&A', '0' => 'Choice&B'),
'required' => true,
'multiple' => false,
'expanded' => true,
));
Expand All @@ -772,9 +801,9 @@ public function testMultipleChoiceExpanded()
{
$form = $this->factory->createNamed('name', 'choice', array('&a', '&c'), array(
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B', '&c' => 'Choice&C'),
'required' => true,
'multiple' => true,
'expanded' => true,
'required' => true,
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
Expand Down Expand Up @@ -1611,6 +1640,35 @@ public function testTimeText()

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[
./input
[@type="text"]
[@id="name_hour"]
[@name="name[hour]"]
[@value="04"]
[@size="1"]
/following-sibling::input
[@type="text"]
[@id="name_minute"]
[@name="name[minute]"]
[@value="05"]
[@size="1"]
]
[count(./input)=2]
'
);
}

public function testTimeTextRequired()
{
$form = $this->factory->createNamed('name', 'time', '04:05:06', array(
'input' => 'string',
'widget' => 'text',
'required' => true,
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[
./input
[@type="text"]
Expand Down Expand Up @@ -1918,7 +1976,7 @@ public function testWidgetAttributeNameRepeatedIfTrue()
$html = $this->renderWidget($form->createView());

// foo="foo"
$this->assertSame('<input type="text" id="text" name="text" required="required" foo="foo" value="value" />', $html);
$this->assertSame('<input type="text" id="text" name="text" foo="foo" value="value" />', $html);
}

public function testWidgetAttributeHiddenIfFalse()
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public function testRepeatedWithCustomOptions()
{
$form = $this->factory->createNamed('name', 'repeated', 'foobar', array(
'type' => 'password',
'first_options' => array('label' => 'Test', 'required' => false),
'first_options' => array('label' => 'Test', 'required' => true),
'second_options' => array('label' => 'Test2'),
));

Expand All @@ -413,14 +413,14 @@ public function testRepeatedWithCustomOptions()
./td
[./label[@for="name_first"][.="[trans]Test[/trans]"]]
/following-sibling::td
[./input[@type="password"][@id="name_first"][@required="required"]]
[./input[@type="password"][@id="name_first"]]
]
/following-sibling::tr
[
./td
[./label[@for="name_second"][.="[trans]Test2[/trans]"]]
/following-sibling::td
[./input[@type="password"][@id="name_second"][@required="required"]]
[./input[@type="password"][@id="name_second"]]
]
/following-sibling::tr[@style="display: none"]
[./td[@colspan="2"]/input
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy