【问题标题】:How to disable radio buttons if one is seleced in Sonata Admin如果在 Sonata Admin 中选择了单选按钮,如何禁用单选按钮
【发布时间】:2018-08-28 04:58:44
【问题描述】:

例如,我有一个实体字段,该字段以 null 开头,并将在管理页面中显示单选按钮,一旦选择了单选按钮并将其保存到实体中,那么这些单选按钮需要“禁用”,仍然可见但不难处理。

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));
}

【问题讨论】:

    标签: php symfony sonata-admin sonata


    【解决方案1】:

    您可以在表单中添加一个条件来检查某个字段是否已填写。 (假设方法名为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,
         ));
    }
    

    更多信息:

    https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html

    【讨论】:

      【解决方案2】:

      按照你的想法去做。

      检查其中一个选项是否存在,如果存在则执行不同的代码。

      如果存在单选按钮,我还建议删除它,并替换为文本。这将阻止一些聪明人编辑 DOM 并更改选择。

      【讨论】:

        猜你喜欢
        • 2017-09-02
        • 1970-01-01
        • 2011-11-05
        • 1970-01-01
        • 2017-06-30
        • 1970-01-01
        • 2013-01-19
        • 2011-04-17
        • 2011-07-12
        相关资源
        最近更新 更多