8000 [Form] read_only and disabled attributes (closes #1974) by helmer · Pull Request #3193 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] read_only and disabled attributes (closes #1974) #3193

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

Merged
merged 1 commit into from
Feb 2, 2012
Merged
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 CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c

### Form

* [BC BREAK] ``read_only`` field attribute now renders as ``readonly="readonly"``, use ``disabled`` instead
* [BC BREAK] child forms now aren't validated anymore by default
* made validation of form children configurable (new option: cascade_validation)
* added support for validation groups as callbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@

{% block widget_attributes %}
{% spaceless %}
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 %}
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 %}
{% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
id="<?php echo $view->escape($id) ?>"
name="<?php echo $view->escape($full_name) ?>"
<?php if ($read_only): ?>disabled="disabled" <?php endif ?>
<?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 ?>
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public function buildForm(FormBuilder $builder, array $options)

$builder
->setRequired($options['required'])
->setReadOnly($options['read_only'])
->setDisabled($options['disabled'])
->setErrorBubbling($options['error_bubbling'])
->setEmptyData($options['empty_data'])
->setAttribute('read_only', $options['read_only'])
->setAttribute('by_reference', $options['by_reference'])
->setAttribute('property_path', $options['property_path'])
->setAttribute('error_mapping', $options['error_mapping'])
Expand Down Expand Up @@ -102,7 +103,8 @@ public function buildView(FormView $view, FormInterface $form)
->set('full_name', $fullName)
->set('errors', $form->getErrors())
->set('value', $form->getClientData())
->set('read_only', $form->isReadOnly())
->set('read_only', $form->getAttribute('read_only'))
->set('disabled', $form->isDisabled())
->set('required', $form->isRequired())
->set('max_length', $form->getAttribute('max_length'))
->set('pattern', $form->getAttribute('pattern'))
Expand All @@ -126,6 +128,7 @@ public function getDefaultOptions(array $options)
'trim' => true,
'required' => true,
'read_only' => false,
'disabled' => false,
'max_length' => null,
'pattern' => null,
'property_path' => null,
Expand Down
31 changes: 9 additions & 22 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class Form implements \IteratorAggregate, FormInterface
* Whether this form may only be read, but not bound
* @var Boolean
*/
private $readOnly = false;
private $disabled = false;

/**
* The dispatcher for distributing events of this form
Expand All @@ -190,7 +190,7 @@ public function __construct($name, EventDispatcherInterface $dispatcher,
array $types = array(), array $clientTransformers = array(),
array $normTransformers = array(),
DataMapperInterface $dataMapper = null, array $validators = array(),
$required = false, $readOnly = false, $errorBubbling = false,
$required = false, $disabled = false, $errorBubbling = false,
$emptyData = null, array $attributes = array())
{
foreach ($clientTransformers as $transformer) {
Expand Down Expand Up @@ -219,7 +219,7 @@ public function __construct($name, EventDispatcherInterface $dispatcher,
$this->dataMapper = $dataMapper;
$this->validators = $validators;
$this->required = (Boolean) $required;
$this->readOnly = (Boolean) $readOnly;
$this->disabled = (Boolean) $disabled;
$this->errorBubbling = (Boolean) $errorBubbling;
$this->emptyData = $emptyData;
$this->attributes = $attributes;
Expand Down Expand Up @@ -266,29 +266,19 @@ public function getTypes()
public function isRequired()
{
if (null === $this->parent || $this->parent->isRequired()) {

return $this->required;
}

return false;
}

/**
* Returns whether this form is read only.
*
* The content of a read-only form is displayed, but not allowed to be
* modified. The validation of modified read-only forms should fail.
*
* Fields whose parents are read-only are considered read-only regardless of
* their own state.
*
* @return Boolean
* {@inheritDoc}
*/
public function isReadOnly()
public function isDisabled()
{
if (null === $this->parent || !$this->parent->isReadOnly()) {

return $this->readOnly;
if (null === $this->parent || !$this->parent->isDisabled()) {
return $this->disabled;
}

return true;
Expand Down Expand Up @@ -457,7 +447,7 @@ public function getExtraData()
*/
public function bind($clientData)
{
if ($this->readOnly) {
if ($this->isDisabled()) {
$this->bound = true;

return $this;
Expand Down Expand Up @@ -674,7 +664,6 @@ public function isEmpty()
{
foreach ($this->children as $child) {
if (!$child->isEmpty()) {

return false;
}
}
Expand All @@ -697,10 +686,9 @@ public function isValid()
return false;
}

if (!$this->readOnly) {
if (!$this->isDisabled()) {
foreach ($this->children as $child) {
if (!$child->isValid()) {

return false;
}
}
Expand Down Expand Up @@ -865,7 +853,6 @@ public function has($name)
public function get($name)
{
if (isset($this->children[$name])) {

return $this->children[$name];
}

Expand Down
20 changes: 10 additions & 10 deletions src/Symfony/Component/Form/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FormBuilder
/**
* @var Boolean
*/
private $readOnly;
private $disabled;

/**
* @var Boolean
Expand Down Expand Up @@ -174,27 +174,27 @@ public function getData()
}

/**
* Set whether the form is read only
* Set whether the form is disabled
Copy link
Contributor

Choose a reason for hiding this comment

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

missing dot

*
* @param Boolean $readOnly Whether the form is read only
* @param Boolean $disabled Whether the form is disabled
*
* @return FormBuilder The current builder
*/
public function setReadOnly($readOnly)
public function setDisabled($disabled)
{
$this->readOnly = (Boolean) $readOnly;
$this->disabled = (Boolean) $disabled;

return $this;
}

/**
* Returns whether the form is read only.
* Returns whether the form is disabled.
*
* @return Boolean Whether the form is read only
* @return Boolean Whether the form is disabled
*/
public function getReadOnly()
public function getDisabled()
{
return $this->readOnly;
return $this->disabled;
}

/**
Expand Down Expand Up @@ -645,7 +645,7 @@ public function getForm()
$this->getDataMapper(),
$this->getValidators(),
$this->getRequired(),
$this->getReadOnly(),
$this->getDisabled(),
$this->getErrorBubbling(),
$this->getEmptyData(),
$this->getAttributes()
Expand Down
14 changes: 7 additions & 7 deletions src/Symfony/Component/Form/FormInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ function add(FormInterface $child);

/**
* Returns the child with the given name.
*
*
* @param string $name The name of the child
*
*
* @return FormInterface The child form
*/
function get($name);
Expand Down Expand Up @@ -178,17 +178,17 @@ function isValid();
function isRequired();

/**
* Returns whether this form can be read only.
* Returns whether this form is disabled.
*
* The content of a read-only form is displayed, but not allowed to be
* modified. The validation of modified read-only forms should fail.
* The content of a disabled form is displayed, but not allowed to be
* modified. The validation of modified disabled forms should fail.
*
* Fields whose parents are read-only are considered read-only regardless of
* Fields whose parents are disabled are considered disabled regardless of
* their own state.
*
* @return Boolean
*/
function isReadOnly();
function isDisabled();

/**
* Returns whether the form is empty.
Expand Down
30 changes: 30 additions & 0 deletions tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,36 @@ public function testHidden()
);
}

public function testReadOnly()
{
$form = $this->factory->createNamed('text', 'name', null, array(
'read_only' => true,
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="text"]
[@name="name"]
[@readonly="readonly"]
'
);
}

public function testDisabled()
{
$form = $this->factory->createNamed('text', 'name', null, array(
'disabled' => true,
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="text"]
[@name="name"]
[@disabled="disabled"]
'
);
}

public function testInteger()
{
$form = $this->factory->createNamed('integer', 'na&me', 123, array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public function testPassRequiredAsOption()
$this->assertTrue($form->isRequired());
}

public function testPassReadOnlyAsOption()
public function testPassDisabledAsOption()
{
$form = $this->factory->create('field', null, array('read_only' => true));
$form = $this->factory->create('field', null, array('disabled' => true));

$this->assertTrue($form->isReadOnly());
$this->assertTrue($form->isDisabled());
}

public function testBoundDataIsTrimmedBeforeTransforming()
Expand Down
4 changes: 2 additions & 2 deletions tests/Symfony/Tests/Component/Form/FormFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public function testUnknownOptions()

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

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