8000 [Form] read_only and disabled attributes · symfony/symfony@de253dd · GitHub
[go: up one dir, main page]

Skip to content

Commit de253dd

Browse files
committed
[Form] read_only and disabled attributes
1 parent 4b19034 commit de253dd

File tree

11 files changed

+88
-66
lines changed

11 files changed

+88
-66
lines changed

CHANGELOG-2.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
158158

159159
### Form
160160

161+
* [BC BREAK] ``read_only`` field attribute now renders as ``readonly="readonly"``, use ``disabled`` instead
161162
* [BC BREAK] child forms now aren't validated anymore by default
162163
* made validation of form children configurable (new option: cascade_validation)
163164
* added support for validation groups as callbacks

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@
280280

281281
{% block widget_attributes %}
282282
{% spaceless %}
283-
id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
283+
id="{{ id }}" name="{{ full_name }}"{% 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 %}
284284
{% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}
285285
{% endspaceless %}
286286
{% endblock widget_attributes %}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
id="<?php echo $view->escape($id) ?>"
22
name="<?php echo $view->escape($full_name) ?>"
3-
<?php if ($read_only): ?>disabled="disabled" <?php endif ?>
3+
<?php if ($read_only): ?>readonly="readonly" <?php endif ?>
4+
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
45
<?php if ($required): ?>required="required" <?php endif ?>
56
<?php if ($max_length): ?>maxlength="<?php echo $view->escape($max_length) ?>" <?php endif ?>
67
<?php if ($pattern): ?>pattern="<?php echo $view->escape($pattern) ?>" <?php endif ?>

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ public function buildForm(FormBuilder $builder, array $options)
4444

4545
$builder
4646
->setRequired($options['required'])
47-
->setReadOnly($options['read_only'])
47+
->setDisabled($options['disabled'])
4848
->setErrorBubbling($options['error_bubbling'])
4949
->setEmptyData($options['empty_data'])
50+
->setAttribute('read_only', $options['read_only'])
5051
->setAttribute('by_reference', $options['by_reference'])
5152
->setAttribute('property_path', $options['property_path'])
5253
->setAttribute('error_mapping', $options['error_mapping'])
@@ -102,7 +103,8 @@ public function buildView(FormView $view, FormInterface $form)
102103
->set('full_name', $fullName)
103104
->set('errors', $form->getErrors())
104105
->set('value', $form->getClientData())
105-
->set('read_only', $form->isReadOnly())
106+
->set('read_only', $form->getAttribute('read_only'))
107+
->set('disabled', $form->isDisabled())
106108
->set('required', $form->isRequired())
107109
->set('max_length', $form->getAttribute('max_length'))
108110
->set('pattern', $form->getAttribute('pattern'))
@@ -126,6 +128,7 @@ public function getDefaultOptions(array $options)
126128
'trim' => true,
127129
'required' => true,
128130
'read_only' => false,
131+
'disabled' => false,
129132
'max_length' => null,
130133
'pattern' => null,
131134
'property_path' => null,

src/Symfony/Component/Form/Form.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class Form implements \IteratorAggregate, FormInterface
166166
* Whether this form may only be read, but not bound
167167
* @var Boolean
168168
*/
169-
private $readOnly = false;
169+
private $disabled = false;
170170

171171
/**
172172
* The dispatcher for distributing events of this form
@@ -190,7 +190,7 @@ public function __construct($name, EventDispatcherInterface $dispatcher,
190190
array $types = array(), array $clientTransformers = array(),
191191
array $normTransformers = array(),
192192
DataMapperInterface $dataMapper = null, array $validators = array(),
193-
$required = false, $readOnly = false, $errorBubbling = false,
193+
$required = false, $disabled = false, $errorBubbling = false,
194194
$emptyData = null, array $attributes = array())
195195
{
196196
foreach ($clientTransformers as $transformer) {
@@ -219,7 +219,7 @@ public function __construct($name, EventDispatcherInterface $dispatcher,
219219
$this->dataMapper = $dataMapper;
220220
$this->validators = $validators;
221221
$this->required = (Boolean) $required;
222-
$this->readOnly = (Boolean) $readOnly;
222+
$this->disabled = (Boolean) $disabled;
223223
$this->errorBubbling = (Boolean) $errorBubbling;
224224
$this->emptyData = $emptyData;
225225
$this->attributes = $attributes;
@@ -266,29 +266,19 @@ public function getTypes()
266266
public function isRequired()
267267
{
268268
if (null === $this->parent || $this->parent->isRequired()) {
269-
270269
return $this->required;
271270
}
272271

273272
return false;
274273
}
275274

276275
/**
277-
* Returns whether this form is read only.
278-
*
279-
* The content of a read-only form is displayed, but not allowed to be
280-
* modified. The validation of modified read-only forms should fail.
281-
*
282-
* Fields whose parents are read-only are considered read-only regardless of
283-
* their own state.
284-
*
285-
* @return Boolean
276+
* {@inheritDoc}
286277
*/
287-
public function isReadOnly()
278+
public function isDisabled()
288279
{
289-
if (null === $this->parent || !$this->parent->isReadOnly()) {
290-
291-
return $this->readOnly;
280+
if (null === $this->parent || !$this->parent->isDisabled()) {
281+
return $this->disabled;
292282
}
293283

294284
return true;
@@ -457,7 +447,7 @@ public function getExtraData()
457447
*/
458448
public function bind($clientData)
459449
{
460-
if ($this->readOnly) {
450+
if ($this->isDisabled()) {
461451
$this->bound = true;
462452

463453
return $this;
@@ -674,7 +664,6 @@ public function isEmpty()
674664
{
675665
foreach ($this->children as $child) {
676666
if (!$child->isEmpty()) {
677-
678667
return false;
679668
}
680669
}
@@ -697,10 +686,9 @@ public function isValid()
697686
return false;
698687
}
699688

700-
if (!$this->readOnly) {
689+
if (!$this->isDisabled()) {
701690
foreach ($this->children as $child) {
702691
if (!$child->isValid()) {
703-
704692
return false;
705693
}
706694
}
@@ -865,7 +853,6 @@ public function has($name)
865853
public function get($name)
866854
{
867855
if (isset($this->children[$name])) {
868-
869856
return $this->children[$name];
870857
}
871858

src/Symfony/Component/Form/FormBuilder.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class FormBuilder
4646
/**
4747
* @var Boolean
4848
*/
49-
private $readOnly;
49+
private $disabled;
5050

5151
/**
5252
* @var Boolean
@@ -174,27 +174,27 @@ public function getData()
174174
}
175175

176176
/**
177-
* Set whether the form is read only
177+
* Set whether the form is disabled
178178
*
179-
* @param Boolean $readOnly Whether the form is read only
179+
* @param Boolean $disabled Whether the form is disabled
180180
*
181181
* @return FormBuilder The current builder
182182
*/
183-
public function setReadOnly($readOnly)
183+
public function setDisabled($disabled)
184184
{
185-
$this->readOnly = (Boolean) $readOnly;
185+
$this->disabled = (Boolean) $disabled;
186186

187187
return $this;
188188
}
189189

190190
/**
191-
* Returns whether the form is read only.
191+
* Returns whether the form is disabled.
192192
*
193-
* @return Boolean Whether the form is read only
193+
* @return Boolean Whether the form is disabled
194194
*/
195-
public function getReadOnly()
195+
public function getDisabled()
196196
{
197-
return $this->readOnly;
197+
return $this->disabled;
198198
}
199199

200200
/**
@@ -645,7 +645,7 @@ public function getForm()
645645
$this->getDataMapper(),
646646
$this->getValidators(),
647647
$this->getRequired(),
648-
$this->getReadOnly(),
648+
$this->getDisabled(),
649649
$this->getErrorBubbling(),
650650
$this->getEmptyData(),
651651
$this->getAttributes()

src/Symfony/Component/Form/FormInterface.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ function add(FormInterface $child);
4848

4949
/**
5050
* Returns the child with the given name.
51-
*
51+
*
5252
* @param string $name The name of the child
53-
*
53+
*
5454
* @return FormInterface The child form
5555
*/
5656
function get($name);
@@ -178,17 +178,17 @@ function isValid();
178178
function isRequired();
179179

180180
/**
181-
* Returns whether this form can be read only.
181+
* Returns whether this form is disabled.
182182
*
183-
* The content of a read-only form is displayed, but not allowed to be
184-
* modified. The validation of modified read-only forms should fail.
183+
* The content of a disabled form is displayed, but not allowed to be
184+
* modified. The validation of modified disabled forms should fail.
185185
*
186-
* Fields whose parents are read-only are considered read-only regardless of
186+
* Fields whose parents are disabled are considered disabled regardless of
187187
* their own state.
188188
*
189189
* @return Boolean
190190
*/
191-
function isReadOnly();
191+
function isDisabled();
192192

193193
/**
194194
* Returns whether the form is empty.

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,36 @@ public function testHidden()
12991299
);
13001300
}
13011301

1302+
public function testReadOnly()
1303+
{
1304+
$form = $this->factory->createNamed('text', 'name', null, array(
1305+
'read_only' => true,
1306+
));
1307+
1308+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1309+
'/input
1310+
[@type="text"]
1311+
[@name="name"]
1312+
[@readonly="readonly"]
1313+
'
1314+
);
1315+
}
1316+
1317+
public function testDisabled()
1318+
{
1319+
$form = $this->factory->createNamed('text', 'name', null, array(
1320+
'disabled' => true,
1321+
));
1322+
1323+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1324+
'/input
1325+
[@type="text"]
1326+
[@name="name"]
1327+
[@disabled="disabled"]
1328+
'
1329+
);
1330+
}
1331+
13021332
public function testInteger()
13031333
{
13041334
$form = $this->factory->createNamed('integer', 'na&me', 123, array(

tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ public function testPassRequiredAsOption()
7070
$this->assertTrue($form->isRequired());
7171
}
7272

73-
public function testPassReadOnlyAsOption()
73+
public function testPassDisabledAsOption()
7474
{
75-
$fo BD94 rm = $this->factory->create('field', null, array('read_only' => true));
75+
$form = $this->factory->create('field', null, array('disabled' => true));
7676

77-
$this->assertTrue($form->isReadOnly());
77+
$this->assertTrue($form->isDisabled());
7878
}
7979

8080
public function testBoundDataIsTrimmedBeforeTransforming()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public function testUnknownOptions()
492492

493493
$this->setExpectedException('Symfony\Component\Form\Exception\CreationException',
494494
'The options "invalid", "unknown" do not exist. Known options are: "data", "data_class", ' .
495-
'"trim", "required", "read_only", "max_length", "pattern", "property_path", "by_reference", ' .
495+
'"trim", "required", "read_only", "disabled", "max_length", "pattern", "property_path", "by_reference", ' .
496496
'"error_bubbling", "error_mapping", "label", "attr", "invalid_message", "invalid_message_parameters", ' .
497497
'"translation_domain", "empty_data"'
498498
);
@@ -507,7 +507,7 @@ public function testUnknownOption()
507507

508508
$this->setExpectedException('Symfony\Component\Form\Exception\CreationException',
509509
'The option "unknown" does not exist. Known options are: "data", "data_class", ' .
510-
'"trim", "required", "read_only", "max_length", "pattern", "property_path", "by_reference", ' .
510+
'"trim", "required", "read_only", "disabled", "max_length", "pattern", "property_path", "by_reference", ' .
511511
'"error_bubbling", "error_mapping", "label", "attr", "invalid_message", "invalid_message_parameters", ' .
512512
'"translation_domain", "empty_data"'
513513
);

0 commit comments

Comments
 (0)
0