8000 [Form] Add ability to disable choice in form by damour · Pull Request #7510 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Add ability to disable choice in form #7510

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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add ability to disable choice in form
  • Loading branch information
damour committed Mar 28, 2013
commit 957ff28221057c9dfc2c7211f9e2858b95729793
6 changes: 6 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function getTests()
{
return array(
'selectedchoice' => new \Twig_Test_Method($this, 'isSelectedChoice'),
'choicedisabled' => new \Twig_Test_Method($this, 'isChoiceDisabled'),
);
}

Expand Down Expand Up @@ -123,6 +124,11 @@ public function isSelectedChoice(ChoiceView $choice, $selectedValue)
return $choice->value === $selectedValue;
}

public function isChoiceDisabled(ChoiceView $choice)
Copy link
Member

Choose a reason for hiding this comment

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

This method is useless as you already have a boolean with the value, allowing to do choice.disabled in Twig, which is shorted than choice is choicedisabled

{
return $choice->disabled;
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}{% if choice is choicedisabled(value) %} disabled="disabled"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
Copy link
Member

Choose a reason for hiding this comment

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

you don't need the value to decide if it is disabled

{% endif %}
{% endfor %}
{% endspaceless %}
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/View/ChoiceView.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,26 @@ class ChoiceView
*/
public $label;

/**
* Is choice disabled.
*
* @var Boolean
*/
public $disabled;

/**
* Creates a new ChoiceView.
*
* @param mixed $data The original choice.
* @param string $value The view representation of the choice.
* @param string $label The label displayed to humans.
* @param string $disabled Is choice disabled.
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a Boolean there

*/
public function __construct($data, $value, $label)
public function __construct($data, $value, $label, $disabled = false)
{
$this->data = $data;
$this->value = $value;
$this->label = $label;
$this->disabled = $disabled;
}
}
0