8000 Deprecated max_length and pattern options · symfony/symfony@52c07c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52c07c7

Browse files
Stefano Salafabpot
Stefano Sala
authored andcommitted
Deprecated max_length and pattern options
1 parent 37d484c commit 52c07c7

File tree

10 files changed

+56
-33
lines changed

10 files changed

+56
-33
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,6 @@
377377
{%- if read_only %} readonly="readonly"{% endif -%}
378378
{%- if disabled %} disabled="disabled"{% endif -%}
379379
{%- if required %} required="required"{% endif -%}
380-
{%- if max_length %} maxlength="{{ max_length }}"{% endif -%}
381-
{%- if pattern %} pattern="{{ pattern }}"{% endif -%}
382380
{%- for attrname, attrvalue in attr -%}
383381
{{- " " -}}
384382
{%- if attrname in ['placeholder', 'title'] -%}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>" <?php if ($read_only): ?>readonly="readonly" <?php endif ?>
22
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
33
<?php if ($required): ?>required="required" <?php endif ?>
4-
<?php if ($max_length): ?>maxlength="<?php echo $view->escape($max_length) ?>" <?php endif ?>
5-
<?php if ($pattern): ?>pattern="<?php echo $view->escape($pattern) ?>" <?php endif ?>
64
<?php foreach ($attr as $k => $v): ?>
75
<?php if (in_array($v, array('placeholder', 'title'), true)): ?>
86
<?php printf('%s="%s" ', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?>

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.5.0
55
------
66

7+
* deprecated options "max_length" and "pattern" in favor of putting these values in "attr" option
78
* added an option for multiple files upload
89
* form errors now reference their cause (constraint violation, exception, ...)
910
* form errors now remember which form they were originally added to

src/Symfony/Component/Form/Extension/Core/Type/FormType.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff F438 line change
@@ -92,8 +92,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
9292
'value' => $form->getViewData(),
9393
'data' => $form->getNormData(),
9494
'required' => $form->isRequired(),
95-
'max_length' => $options['max_length'],
96-
'pattern' => $options['pattern'],
95+
'max_length' => isset($options['attr']['maxlength']) ? $options['attr']['maxlength'] : null, // Deprecated
96+
'pattern' => isset($options['attr']['pattern']) ? $options['attr']['pattern'] : null, // Deprecated
9797
'size' => null,
9898
'label_attr' => $options['label_attr'],
9999
'compound' => $form->getConfig()->getCompound(),
@@ -170,6 +170,22 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
170170
'data',
171171
));
172172

173+
// BC clause for the "max_length" and "pattern" option
174+
// Add these values to the "attr" option instead
175+
$defaultAttr = function (Options $options) {
176+
$attributes = array();
177+
178+
if (null !== $options['max_length']) {
179+
$attributes['maxlength'] = $options['max_length'];
180+
}
181+
182+
if (null !== $options['pattern']) {
183+
$attributes['pattern'] = $options['pattern'];
184+
}
185+
186+
return $attributes;
187+
};
188+
173189
$resolver->setDefaults(array(
174190
'data_class' => $dataClass,
175191
'empty_data' => $emptyData,
@@ -190,6 +206,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
190206
// According to RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt)
191207
// section 4.2., empty URIs are considered same-document references
192208
'action' => '',
209+
'attr' => $defaultAttr
193210
));
194211

195212
$resolver->setAllowedTypes(array(

src/Symfony/Component/Form/FormFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ public function createBuilderForProperty($class, $property, $data = null, array
113113
$pattern = $patternGuess ? $patternGuess->getValue() : null;
114114

115115
if (null !== $pattern) {
116-
$options = array_merge(array('pattern' => $pattern), $options);
116+
$options = array_merge(array('attr' => array('pattern' => $pattern)), $options);
117117
}
118118

119119
if (null !== $maxLength) {
120-
$options = array_merge(array('max_length' => $maxLength), $options);
120+
$options = array_merge(array('attr' => array('maxlength' => $maxLength)), $options);
121121
}
122122

123123
if ($requiredGuess) {

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ public function testEmail()
12521252
public function testEmailWithMaxLength()
12531253
{
12541254
$form = $this->factory->createNamed('name', 'email', 'foo&bar', array(
1255-
'max_length' => 123,
1255+
'attr' => array('maxlength' => 123),
12561256
));
12571257

12581258
$this->assertWidgetMatchesXpath($form->createView(), array(),
@@ -1418,7 +1418,7 @@ public function testPasswordSubmittedWithNotAlwaysEmpty()
14181418
public function testPasswordWithMaxLength()
14191419
{
14201420
$form = $this->factory->createNamed('name', 'password', 'foo&bar', array(
1421-
'max_length' => 123,
1421+
'attr' => array('maxlength' => 123),
14221422
));
14231423

14241424
$this->assertWidgetMatchesXpath($form->createView(), array(),
@@ -1489,7 +1489,7 @@ public function testRadioWithValue()
14891489
public function testTextarea()
14901490
{
14911491
$form = $this->factory->createNamed('name', 'textarea', 'foo&bar', array(
1492-
'pattern' => 'foo',
1492+
'attr' => array('pattern' => 'foo'),
14931493
));
14941494

14951495
$this->assertWidgetMatchesXpath($form->createView(), array(),
@@ -1518,7 +1518,7 @@ public function testText()
15181518
public function testTextWithMaxLength()
15191519
{
15201520
$form = $this->factory->createNamed('name', 'text', 'foo&bar', array(
1521-
'max_length' => 123,
1521+
'attr' => array('maxlength' => 123),
15221522
));
15231523

15241524
$this->assertWidgetMatchesXpath($form->createView(), array(),
@@ -1898,9 +1898,7 @@ public function testWidgetAttributes()
18981898
'required' => true,
18991899
'disabled' => true,
19001900
'read_only' => true,
1901-
'max_length' => 10,
1902-
'pattern' => '\d+',
1903-
'attr' => array('class' => 'foobar', 'data-foo' => 'bar'),
1901+
'attr' => array('maxlength' => 10, 'pattern' => '\d+', 'class' => 'foobar', 'data-foo' => 'bar'),
19041902
));
19051903

19061904
$html = $this->renderWidget($form->createView());

src/Symfony/Component/Form/Tests/Extension/Core/EventListener/ResizeFormListenerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ public function testPreSetDataResizesForm()
6969

7070
$this->factory->expects($this->at(0))
7171
->method('createNamed')
72-
->with(1, 'text', null, array('property_path' => '[1]', 'max_length' => 10, 'auto_initialize' => false))
72+
->with(1, 'text', null, array('property_path' => '[1]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
7373
->will($this->returnValue($this->getForm('1')));
7474
$this->factory->expects($this->at(1))
7575
->method('createNamed')
76-
->with(2, 'text', null, array('property_path' => '[2]', 'max_length' => 10, 'auto_initialize' => false))
76+
->with(2, 'text', null, array('property_path' => '[2]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
7777
->will($this->returnValue($this->getForm('2')));
7878

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

8484
$this->assertFalse($this->form->has('0'));
@@ -113,12 +113,12 @@ public function testPreSubmitResizesUpIfAllowAdd()
113113

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

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

124124
$this->assertTrue($this->form->has('0'));

src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testSetDataAdjustsSize()
3131
$form = $this->factory->create('collection', null, array(
3232
'type' => 'text',
3333
'options' => array(
34-
'max_length' => 20,
34+
'attr' => array('maxlength' => 20),
3535
),
3636
));
3737
$form->setData(array('foo@foo.com', 'foo@bar.com'));
@@ -41,15 +41,18 @@ public function testSetDataAdjustsSize()
4141
$this->assertCount(2, $form);
4242
$this->assertEquals('foo@foo.com', $form[0]->getData());
4343
$this->assertEquals('foo@bar.com', $form[1]->getData());
44-
$this->assertEquals(20, $form[0]->getConfig()->getOption('max_length'));
45-
$this->assertEquals(20, $form[1]->getConfig()->getOption('max_length'));
44+
$formAttrs0 = $form[0]->getConfig()->getOption('attr');
45+
$formAttrs1 = $form[1]->getConfig()->getOption('attr');
46+
$this->assertEquals(20, $formAttrs0['maxlength']);
47+
$this->assertEquals(20, $formAttrs1['maxlength']);
4648

4749
$form->setData(array('foo@baz.com'));
4850
$this->assertInstanceOf('Symfony\Component\Form\Form', $form[0]);
4951
$this->assertFalse(isset($form[1]));
5052
$this->assertCount(1, $form);
5153
$this->assertEquals('foo@baz.com', $form[0]->getData());
52-
$this->assertEquals(20, $form[0]->getConfig()->getOption('max_length'));
54+
$formAttrs0 = $form[0]->getConfig()->getOption('attr');
55+
$this->assertEquals(20, $formAttrs0['maxlength']);
5356
}
5457

5558
public function testThrowsExceptionIfObjectIsNotTraversable()

src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,19 @@ public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly()
131131
}
132132

133133
public function testPassMaxLengthToView()
134+
{
135+
$form = $this->factory->create('form', null, array('attr' => array('maxlength' => 10)));
136+
$view = $form->createView();
137+
138+
$this->assertSame(10, $view->vars['attr']['maxlength']);
139+
}
140+
141+
public function testPassMaxLengthBCToView()
134142
{
135143
$form = $this->factory->create('form', null, array('max_length' => 10));
136144
$view = $form->createView();
137145

138-
$this->assertSame(10, $view->vars['max_length']);
146+
$this->assertSame(10, $view->vars['attr']['maxlength']);
139147
}
140148

141149
public function testSubmitWithEmptyDataCreatesObjectIfClassAvailable()

src/Symfony/Component/Form/Tests/FormFactoryTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence()
399399
->with('Application\Author', 'firstName')
400400
->will($this->returnValue(new TypeGuess(
401401
'text',
402-
array('max_length' => 10),
402+
array('attr' => array('maxlength' => 10)),
403403
Guess::MEDIUM_CONFIDENCE
404404
)));
405405

@@ -408,15 +408,15 @@ public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence()
408408
->with('Application\Author', 'firstName')
409409
->will($this->returnValue(new TypeGuess(
410410
'password',
411-
array('max_length' => 7),
411+
array('attr' => array('maxlength' => 7)),
412412
Guess::HIGH_CONFIDENCE
413413
)));
414414

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

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

422422
$this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
@@ -450,22 +450,22 @@ public function testOptionsCanBeOverridden()
450450
->with('Application\Author', 'firstName')
451451
->will($this->returnValue(new TypeGuess(
452452
'text',
453-
array('max_length' => 10),
453+
array('attr' => array('maxlength' => 10)),
454454
Guess::MEDIUM_CONFIDENCE
455455
)));
456456

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

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

464464
$this->builder = $factory->createBuilderForProperty(
465465
'Application\Author',
466466
'firstName',
467467
null,
468-
array('max_length' => 11)
468+
array('attr' => array('maxlength' => 11))
469469
);
470470

471471
$this->assertEquals('builderInstance', $this->builder);
@@ -493,7 +493,7 @@ public function testCreateBuilderUsesMaxLengthIfFound()
493493

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

499499
$this->builder = $factory->createBuilderForProperty(
@@ -559,7 +559,7 @@ public function testCreateBuilderUsesPatternIfFound()
559559

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

565565
$this->builder = $factory->createBuilderForProperty(

0 commit comments

Comments
 (0)
0