微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Symfony - 为这个 FORM

如何解决Symfony - 为这个 FORM

我想通过点击一个按钮(button: 'current_date',见下面的代码

此按钮位于相同形式的选择字段(月份和年份)内。

我不想从控制器处理这个动作,但是!我希望这一切都在此 FormType 内部完成! :-)

我知道我可以通过使用 javascript 来实现结果,但这不是我想要的,也不是我需要的,因为如果将此 FormType 文件移动到另一个项目中,我不想在其中编写一些东西(如 javascript)另一个文件

我认为可能的选项,但我不知道它们是否可以实现:

表单类型:

<?PHP

namespace App\Form;

use ...

class LicenseSearchType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder,array $options)
    {
        $builder
            ->add('name',null,[
                'required' => false
            ])
            ->add('filter',CheckBoxType::class,[
                'label' => 'Use filter ?','mapped' => false,// means that this checkBox in not related with the entity
                'required' => false,])
            ->add('month',ChoiceType::class,[
                'choices' => $this->getMonths(),'data' => date('n'),// set the current month
            ])
            ->add('year',[
                'choices' => $this->getYears(2000),'data' => date('Y'),// set the current Year
            ])
            ->add('current_date',ButtonType::class,[
                'label' => 'current date',// -------------------------------------------
                // ------------  add something here   --------
                // -------------------------------------------
            ])
        ;
    }

    protected function getMonths(){...}

    protected function getYears($min,$max='current'){...}

    // -------------------------------------------
    // ---- or ----  add something here   --------
    // -------------------------------------------

    // public function configureOptions(OptionsResolver $resolver){...}
    
}

这个表单的渲染:

the rendering of this Form

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。