dongnai1876 2009-11-19 11:42
浏览 52
已采纳

在Symfony的Select选项中设置默认值?

I am using the widget

sfWidgetFormChoice(array (
  'choices' => CountryPeer::getAllCountry(),
))

and validator as

sfValidatorChoice(array (
  'choices' => array_keys(CountryPeer::getAllCountry()),
))

I get a select element as

<select id="country" name="reg_form[country]">
  <option value="1">India</option>
  <option value="2">Srilanka</option>
</select>

I want to add a option --Select Countries-- as default:

<option value="">--Select Countries--</option>

so that it should throw an required error, if i am not selecting any country.

  • 写回答

2条回答 默认 最新

  • doulai8128 2009-11-19 17:34
    关注

    First, add the option to the array of choices, but not into the validator, so it will throw the error:

    $def_val = array(-1=>"--Select Countries--");
    
    $sel_choices = array_merge($def_val,CountryPeer::getAllCountry());
    
    sfWidgetFormChoice(array (
      'choices' => $sel_choices,
    ))
    
    sfValidatorChoice(array (
      'choices' => array_keys(CountryPeer::getAllCountry()),
    ))
    

    And then set the "-1" option as the default value:

     $your_form->setDefault('country', -1);
    

    Should do it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置