我正在使用小部件

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

和验证器作为
sfValidatorChoice(array (
  'choices' => array_keys(CountryPeer::getAllCountry()),
))

我得到一个选择元素作为
<select id="country" name="reg_form[country]">
  <option value="1">India</option>
  <option value="2">Srilanka</option>
</select>

我想添加一个选项 --Select Countries-- 作为默认值:
<option value="">--Select Countries--</option>

如果我没有选择任何国家,它应该抛出一个必需的错误。

最佳答案

首先,将选项添加到选择数组中,但不添加到验证器中,因此会抛出错误:

$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()),
))

然后将“-1”选项设置为默认值:
 $your_form->setDefault('country', -1);

应该做。

关于php - 在 Symfony 的 Select 选项中设置默认值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1762728/

10-12 12:56
查看更多