How to make form_rest() not display a field with Symfony2?

前端 未结 4 461
轮回少年
轮回少年 2020-12-02 08:52

I\'ve started to use Symfony2 but I\'ve some problems. I wanted to render fields by hand but it doesn\'t work because my field yet rendered by me is displayed with the

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 09:10

    The situation in which you don't want to show some field suggests badly designed form. You could feed some argument(s) into it's __construct to make it conditional (say, include/exclude some fields) or you could just create separate Form classes (which is, in my opinion, a bit overkill).

    I had common case few months ago when form differed when user inserted/updated records. It was something like this:

    ...
    public function __construct($isUpdateForm){
        $this->isUpdateForm= $isUpdateForm;
    }
    
    public function buildForm(FormBuilder $builder, array $options){
        ....
        $builder->add('some_filed', 'text', ..... );
    
        if ( $this->isUpdateForm ){
            $builder->add(.....);
        }
        ....
    }
    ....
    

    If for some reasons you're not able to refactor form class you could still display unwanted fields but wrap them into

    which has CSS display:none attribute. That way "they are still there" (and by all means are processed normally) but are not visible to user.

    Hope this helps...

提交回复
热议问题