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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测