如果在 Sonata Admin 中选择了一个,如何禁用单选按钮
How to disable radio buttons if one is seleced in Sonata Admin
例如,我有一个以 null 开头的实体字段,它将在管理页面中显示单选按钮,一旦选择了单选按钮并将其保存到实体中,那么这些单选按钮就需要 'disabled' , 仍然可见但不难处理。
protected function configureFormFields(FormMapper $form)
{
$form->add('radio_buttons', ChoiceType::class,
array('choices' => array(
"choice 1" => 'input1',
"choice 2" => 'input2'),
'choices_as_values' => true, 'multiple'=>false, 'expanded'=>true, 'disabled' => false));
}
按照你的看法去做。
检查是否存在其中一个选项,如果存在则执行不同的代码。
我还建议删除单选按钮(如果存在)并替换为文本。这将防止某些自作聪明的人编辑 DOM 并更改选择。
您可以在表单中添加一个条件来检查某个字段是否已填写。
(假设该方法名为 getRadioButton())
if ($this->getSubject()->getRadioButton() != null) {
$form->add(here tell than you need disabled buttons)
} else {
$form->add(here tell than you need buttons)
}
此外,在表单字段中,您可以添加 "html" 属性来执行此操作:
->add('radio_buttons', ChoiceType::class,array(
'what you want'=>'ok',
'attr'=>array("disabled" => true))
所以最后它会给出类似
的东西
if ($this->getSubject()->getRadioButton() != null) {
$form->add('radio_buttons', ChoiceType::class,
array('choices' => array(
"choice 1" => 'input1',
"choice 2" => 'input2'),
'choices_as_values' => true,
'multiple'=>false,
'expanded'=>true,
'attr' => array('disabled'=>true),
));
} else {
$form->add('radio_buttons', ChoiceType::class,
array('choices' => array(
"choice 1" => 'input1',
"choice 2" => 'input2'),
'choices_as_values' => true,
'multiple'=>false,
'expanded'=>true,
));
}
更多信息:
例如,我有一个以 null 开头的实体字段,它将在管理页面中显示单选按钮,一旦选择了单选按钮并将其保存到实体中,那么这些单选按钮就需要 'disabled' , 仍然可见但不难处理。
protected function configureFormFields(FormMapper $form)
{
$form->add('radio_buttons', ChoiceType::class,
array('choices' => array(
"choice 1" => 'input1',
"choice 2" => 'input2'),
'choices_as_values' => true, 'multiple'=>false, 'expanded'=>true, 'disabled' => false));
}
按照你的看法去做。
检查是否存在其中一个选项,如果存在则执行不同的代码。
我还建议删除单选按钮(如果存在)并替换为文本。这将防止某些自作聪明的人编辑 DOM 并更改选择。
您可以在表单中添加一个条件来检查某个字段是否已填写。 (假设该方法名为 getRadioButton())
if ($this->getSubject()->getRadioButton() != null) {
$form->add(here tell than you need disabled buttons)
} else {
$form->add(here tell than you need buttons)
}
此外,在表单字段中,您可以添加 "html" 属性来执行此操作:
->add('radio_buttons', ChoiceType::class,array(
'what you want'=>'ok',
'attr'=>array("disabled" => true))
所以最后它会给出类似
的东西if ($this->getSubject()->getRadioButton() != null) {
$form->add('radio_buttons', ChoiceType::class,
array('choices' => array(
"choice 1" => 'input1',
"choice 2" => 'input2'),
'choices_as_values' => true,
'multiple'=>false,
'expanded'=>true,
'attr' => array('disabled'=>true),
));
} else {
$form->add('radio_buttons', ChoiceType::class,
array('choices' => array(
"choice 1" => 'input1',
"choice 2" => 'input2'),
'choices_as_values' => true,
'multiple'=>false,
'expanded'=>true,
));
}
更多信息: