Skip to content

[Form] Deprecated max_length and pattern options #10001

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 2 commits 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 @@ -377,8 +377,6 @@
{%- if read_only %} readonly="readonly"{% endif -%}
{%- if disabled %} disabled="disabled"{% endif -%}
{%- if required %} required="required"{% endif -%}
{%- if max_length %} maxlength="{{ max_length }}"{% endif -%}
{%- if pattern %} pattern="{{ pattern }}"{% endif -%}
{%- for attrname, attrvalue in attr -%}
{{- " " -}}
{%- if attrname in ['placeholder', 'title'] -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>" <?php if ($read_only): ?>readonly="readonly" <?php endif ?>
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
<?php if ($required): ?>required="required" <?php endif ?>
<?php if ($max_length): ?>maxlength="<?php echo $view->escape($max_length) ?>" <?php endif ?>
<?php if ($pattern): ?>pattern="<?php echo $view->escape($pattern) ?>" <?php endif ?>
<?php foreach ($attr as $k => $v): ?>
<?php if (in_array($v, array('placeholder', 'title'), true)): ?>
<?php printf('%s="%s" ', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?>
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 @@ -5,6 +5,7 @@ CHANGELOG
------

* added an option for multiple files upload
* deprecated options "max_length" and "pattern" in favor of putting these values in "attr" option

2.4.0
-----
Expand Down
21 changes: 19 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
'value' => $form->getViewData(),
'data' => $form->getNormData(),
'required' => $form->isRequired(),
'max_length' => $options['max_length'],
'pattern' => $options['pattern'],
'max_length' => isset($options['attr']['maxlength']) ? $options['attr']['maxlength'] : null, // Deprecated
'pattern' => isset($options['attr']['pattern']) ? $options['attr']['pattern'] : null, // Deprecated
'size' => null,
'label_attr' => $options['label_attr'],
'compound' => $form->getConfig()->getCompound(),
Expand Down Expand Up @@ -170,6 +170,22 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'data',
));

// BC clause for the "max_length" and "pattern" option
// Add these values to the "attr" option instead
$defaultAttr = function (Options $options) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding @deprecated somehow in there, will make gripping for deprecated code easer when the time comes to remove it.

$attributes = array();

if (null !== $options['max_length']) {
$attributes['maxlength'] = $options['max_length'];
}

if (null !== $options['pattern']) {
$attributes['pattern'] = $options['pattern'];
}

return $attributes;
};

$resolver->setDefaults(array(
'data_class' => $dataClass,
'empty_data' => $emptyData,
Expand All @@ -190,6 +206,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
// According to RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt)
// section 4.2., empty URIs are considered same-document references
'action' => '',
'attr' => $defaultAttr
));

$resolver->setAllowedTypes(array(
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ public function createBuilderForProperty($class, $property, $data = null, array
$pattern = $patternGuess ? $patternGuess->getValue() : null;

if (null !== $pattern) {
$options = array_merge(array('pattern' => $pattern), $options);
$options = array_merge(array('attr' => array('pattern' => $pattern)), $options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it have to array_merge of attr element?
array_merge(array('attr' => array('pattern' => $pattern)), array('attr' => array('class' => 'form-input'))) gets array('attr' => ('class' => 'form-input')), guessed pattern is gone, which is breaking BC.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think you are right

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take care of this issue.

}

if (null !== $maxLength) {
$options = array_merge(array('max_length' => $maxLength), $options);
$options = array_merge(array('attr' => array('maxlength' => $maxLength)), $options);
}

if ($requiredGuess) {
Expand Down
12 changes: 5 additions & 7 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ public function testEmail()
public function testEmailWithMaxLength()
{
$form = $this->factory->createNamed('name', 'email', 'foo&bar', array(
'max_length' => 123,
'attr' => array('maxlength' => 123),
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
Expand Down Expand Up @@ -1419,7 +1419,7 @@ public function testPasswordSubmittedWithNotAlwaysEmpty()
public function testPasswordWithMaxLength()
{
$form = $this->factory->createNamed('name', 'password', 'foo&bar', array(
'max_length' => 123,
'attr' => array('maxlength' => 123),
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
Expand Down Expand Up @@ -1490,7 +1490,7 @@ public function testRadioWithValue()
public function testTextarea()
{
$form = $this->factory->createNamed('name', 'textarea', 'foo&bar', array(
'pattern' => 'foo',
'attr' => array('pattern' => 'foo'),
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
Expand Down Expand Up @@ -1519,7 +1519,7 @@ public function testText()
public function testTextWithMaxLength()
{
$form = $this->factory->createNamed('name', 'text', 'foo&bar', array(
'max_length' => 123,
'attr' => array('maxlength' => 123),
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
Expand Down Expand Up @@ -1899,9 +1899,7 @@ public function testWidgetAttributes()
'required' => true,
'disabled' => true,
'read_only' => true,
'max_length' => 10,
'pattern' => '\d+',
'attr' => array('class' => 'foobar', 'data-foo' => 'bar'),
'attr' => array('maxlength' => 10, 'pattern' => '\d+', 'class' => 'foobar', 'data-foo' => 'bar'),
));

$html = $this->renderWidget($form->createView());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ public function testPreSetDataResizesForm()

$this->factory->expects($this->at(0))
->method('createNamed')
->with(1, 'text', null, array('property_path' => '[1]', 'max_length' => 10, 'auto_initialize' => false))
->with(1, 'text', null, array('property_path' => '[1]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
->will($this->returnValue($this->getForm('1')));
$this->factory->expects($this->at(1))
->method('createNamed')
->with(2, 'text', null, array('property_path' => '[2]', 'max_length' => 10, 'auto_initialize' => false))
->with(2, 'text', null, array('property_path' => '[2]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
->will($this->returnValue($this->getForm('2')));

$data = array(1 => 'string', 2 => 'string');
$event = new FormEvent($this->form, $data);
$listener = new ResizeFormListener('text', array('max_length' => '10'), false, false);
$listener = new ResizeFormListener('text', array('attr' => array('maxlength' => 10)), false, false);
$listener->preSetData($event);

$this->assertFalse($this->form->has('0'));
Expand Down Expand Up @@ -112,12 +112,12 @@ public function testPreSubmitResizesUpIfAllowAdd()

$this->factory->expects($this->once())
->method('createNamed')
->with(1, 'text', null, array('property_path' => '[1]', 'max_length' => 10, 'auto_initialize' => false))
->with(1, 'text', null, array('property_path' => '[1]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
->will($this->returnValue($this->getForm('1')));

$data = array(0 => 'string', 1 => 'string');
$event = new FormEvent($this->form, $data);
$listener = new ResizeFormListener('text', array('max_length' => 10), true, false);
$listener = new ResizeFormListener('text', array('attr' => array('maxlength' => 10)), true, false);
$listener->preSubmit($event);

$this->assertTrue($this->form->has('0'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testSetDataAdjustsSize()
$form = $this->factory->create('collection', null, array(
'type' => 'text',
'options' => array(
'max_length' => 20,
'attr' => array('maxlength' => 20),
),
));
$form->setData(array('foo@foo.com', 'foo@bar.com'));
Expand All @@ -41,15 +41,18 @@ public function testSetDataAdjustsSize()
$this->assertCount(2, $form);
$this->assertEquals('foo@foo.com', $form[0]->getData());
$this->assertEquals('foo@bar.com', $form[1]->getData());
$this->assertEquals(20, $form[0]->getConfig()->getOption('max_length'));
$this->assertEquals(20, $form[1]->getConfig()->getOption('max_length'));
$formAttrs0 = $form[0]->getConfig()->getOption('attr');
$formAttrs1 = $form[1]->getConfig()->getOption('attr');
$this->assertEquals(20, $formAttrs0['maxlength']);
$this->assertEquals(20, $formAttrs1['maxlength']);

$form->setData(array('foo@baz.com'));
$this->assertInstanceOf('Symfony\Component\Form\Form', $form[0]);
$this->assertFalse(isset($form[1]));
$this->assertCount(1, $form);
$this->assertEquals('foo@baz.com', $form[0]->getData());
$this->assertEquals(20, $form[0]->getConfig()->getOption('max_length'));
$formAttrs0 = $form[0]->getConfig()->getOption('attr');
$this->assertEquals(20, $formAttrs0['maxlength']);
}

public function testThrowsExceptionIfObjectIsNotTraversable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,19 @@ public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly()
}

public function testPassMaxLengthToView()
{
$form = $this->factory->create('form', null, array('attr' => array('maxlength' => 10)));
$view = $form->createView();

$this->assertSame(10, $view->vars['attr']['maxlength']);
}

public function testPassMaxLengthBCToView()
{
$form = $this->factory->create('form', null, array('max_length' => 10));
$view = $form->createView();

$this->assertSame(10, $view->vars['max_length']);
$this->assertSame(10, $view->vars['attr']['maxlength']);
}

public function testSubmitWithEmptyDataCreatesObjectIfClassAvailable()
Expand Down
16 changes: 8 additions & 8 deletions src/Symfony/Component/Form/Tests/FormFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence()
->with('Application\Author', 'firstName')
->will($this->returnValue(new TypeGuess(
'text',
array('max_length' => 10),
array('attr' => array('maxlength' => 10)),
Guess::MEDIUM_CONFIDENCE
)));

Expand All @@ -408,15 +408,15 @@ public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence()
->with('Application\Author', 'firstName')
->will($this->returnValue(new TypeGuess(
'password',
array('max_length' => 7),
array('attr' => array('maxlength' => 7)),
Guess::HIGH_CONFIDENCE
)));

$factory = $this->getMockFactory(array('createNamedBuilder'));

$factory->expects($this->once())
->method('createNamedBuilder')
->with('firstName', 'password', null, array('max_length' => 7))
->with('firstName', 'password', null, array('attr' => array('maxlength' => 7)))
->will($this->returnValue('builderInstance'));

$this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
Expand Down Expand Up @@ -450,22 +450,22 @@ public function testOptionsCanBeOverridden()
->with('Application\Author', 'firstName')
->will($this->returnValue(new TypeGuess(
'text',
array('max_length' => 10),
array('attr' => array('maxlength' => 10)),
Guess::MEDIUM_CONFIDENCE
)));

$factory = $this->getMockFactory(array('createNamedBuilder'));

$factory->expects($this->once())
->method('createNamedBuilder')
->with('firstName', 'text', null, array('max_length' => 11))
->with('firstName', 'text', null, array('attr' => array('maxlength' => 11)))
->will($this->returnValue('builderInstance'));

$this->builder = $factory->createBuilderForProperty(
'Application\Author',
'firstName',
null,
array('max_length' => 11)
array('attr' => array('maxlength' => 11))
);

$this->assertEquals('builderInstance', $this->builder);
Expand Down Expand Up @@ -493,7 +493,7 @@ public function testCreateBuilderUsesMaxLengthIfFound()

$factory->expects($this->once())
->method('createNamedBuilder')
->with('firstName', 'text', null, array('max_length' => 20))
->with('firstName', 'text', null, array('attr' => array('maxlength' => 20)))
->will($this->returnValue('builderInstance'));

$this->builder = $factory->createBuilderForProperty(
Expand Down Expand Up @@ -559,7 +559,7 @@ public function testCreateBuilderUsesPatternIfFound()

$factory->expects($this->once())
->method('createNamedBuilder')
->with('firstName', 'text', null, array('pattern' => '[a-zA-Z]'))
->with('firstName', 'text', null, array('attr' => array('pattern' => '[a-zA-Z]')))
->will($this->returnValue('builderInstance'));

$this->builder = $factory->createBuilderForProperty(
Expand Down