dsfdsf21312 2016-09-07 10:35
浏览 41

如果在symfony中的表单类型中选中了复选框,请添加输入标记

I am a new symfony user. I am generating a form using a form type. I have generated a set of check boxes and would like to have an input field display if one of the check boxes has been selected.

So i am adding to my form builder a marketing rule like this

  ->add('marketingRules', 'entity', array(
                'required' =>'true',
                'class' => 'RecomendationsBundle:MarketingRule',
                'choices' => $builder->getData()->getProject()->getPlan()->getMarketingRules(),
                'property' => 'name',
                'expanded' => true,
                'multiple' => true,
                'by_reference' => false,
            ))

This works as expected. But I would like to add an input field when one of the Marketing rules have been selected to be able to add parameters to the chosen Marketing rule.

I can do this manually by checking the form with JavaScript and adding the necessary inputs, but I am sure there must be a much better option.

Please let me know if I can make my case clearer. In my head sounds ok :(

Using: Symfony 2.5 (Is an old project)

Thanks a lot for your time :)

  • 写回答

1条回答 默认 最新

  • dongma7796 2016-09-07 11:29
    关注

    FOr make the basic checkbox in a form you can use the current method :

    use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
    // ...
    
    $builder->add('public', CheckboxType::class, array(
        'label'    => 'Show this entry publicly?',
        'required' => false,
    ));
    

    After this you can add ""paramaters option" -> like an other add if checked". The checkbox use like value true and false. But i haven't use these one.

    The better solutions stay Jquery or also you can do this with css too. i explain :

    For CSS using : add to your view a input like this :

    <input class="reveal-if-active" name="TheNameYouWant" type="TheTypeYouNeed"/>
    

    and css :

    .reveal-if-active {
      opacity: 0;
      max-height: 0;
      overflow: hidden;
    }
    input[type="radio"]:checked ~ .reveal-if-active,
    input[type="checkbox"]:checked ~ .reveal-if-active {
      opacity: 1;
      max-height: 100px; /* little bit of a magic number :( */
      overflow: visible;
    }
    

    For your controller you can handle by hand the request :

    $myvar = $request->request->get('TheNameYouWant');
    

    And in the if($form is valid) you add this, before to persist :

    $myObject->setValueNeeded($myvar);
    

    It's a good way to use checkbox, if you're optionnal field is not required.

    The best Way stay to use Jquery, for this one i have see that you already use it so don't necessary to said you how do that.

    I'm not sure that you have solutions with Symfony except one that you can use with css method i have give you (for the controller and css keep the same method, just use this special parameter for the extra field :

    ->add('extra', null, array('mapped' => false)),
    

    This gonna make your field optionnal and invisible on your view if you don't call it. (for this one i refer to this post -> https://stackoverflow.com/a/12766325/6795425)

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画