Skip to content

[Form] setReadOnly and setAttribute on a Form after creation #2797

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
29 changes: 29 additions & 0 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ public function isReadOnly()
return true;
}

/**
* Set read only form status
*
* @param Boolean read only status
*
* @return Form The current form
*/
public function setReadOnly($readOnly = true)
{
$this->readOnly = (Boolean) $readOnly;

return $this;
}

/**
* Sets the parent form.
*
Expand Down Expand Up @@ -370,6 +384,21 @@ public function getAttribute($name)
return $this->attributes[$name];
}

/**
* Sets the value of the attributes with the given name.
*
* @param string $name The name of the attribute
* @param mixed $value The value of the attribute
*
* @return Form The current form
*/
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;

return $this;
}

/**
* Updates the field with default data.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Symfony/Tests/Component/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ public function testNotReadOnly()
$this->assertFalse($child->isReadOnly());
}

public function testSetReadOnlyAfterFormCreation()
{
$parent = $this->getBuilder()->setReadOnly(false)->getForm();

$parent->setReadOnly(true);

$this->assertTrue($parent->isReadOnly());
}

public function testSetNotReadOnlyAfterFormCreation()
{
$parent = $this->getBuilder()->setReadOnly(true)->getForm();

$parent->setReadOnly(false);

$this->assertFalse($parent->isReadOnly());
}

public function testCloneChildren()
{
$child = $this->getBuilder('child')->getForm();
Expand Down