Symfony1 Symfony格式的默认值

Symfony1 Symfony格式的默认值,symfony1,symfony-1.4,symfony-forms,Symfony1,Symfony 1.4,Symfony Forms,要在我的表单中设置select的dafault值,我喜欢: $def_volume = array(-1=>"Please select a volume"); $def_issue = array(-1=>"Please select issue"); $volumes = array_merge($def_volume,IssuePeer::getAllVolumesForListChoices()); $issues = array_merge($de

要在我的表单中设置select的dafault值,我喜欢:

   $def_volume = array(-1=>"Please select a volume");
   $def_issue = array(-1=>"Please select issue");

   $volumes = array_merge($def_volume,IssuePeer::getAllVolumesForListChoices());
   $issues = array_merge($def_issue,IssuePeer::getAllIssuesForListChoices());

   $this->setWidgets(array(
  'keyword'    => new sfWidgetFormInput(),
  'volume' => new sfWidgetFormSelect(array('choices' => $volumes)),
  'issue' => new sfWidgetFormSelect(array('choices' => $issues)),
));

   $this->setValidators(array(
  'keyword'    => new sfValidatorString(array('required' => true)),
  'volume' => new sfValidatorChoice(array('choices' => array_keys(IssuePeer::getAllVolumesForListChoices()))),
  'issue' => new sfValidatorChoice(array('choices' => array_keys(IssuePeer::getAllIssuesForListChoices()))),
));
输出:

<select id="filterSearch_volume" name="filterSearch[volume]">
<option value="0">Please select a volume</option>
....
</select>

请选择一个卷
....
但我希望有这样的输出:

<select id="filterSearch_volume" name="filterSearch[volume]">
<option value="">Please select a volume</option>
....
</select>

请选择一个卷
....
如果您注意到第一个值的值是
value=“0”
,但我希望像这样返回我“null”
value=“

您尝试过:

$def_volume = array('' => "Please select a volume");

这似乎对我有用。

$this->widgetSchema['employee\u id']->setOption('add\u empty','Please choose a employee')

为什么它不起作用?我试过SF1.4.20,效果不错。表单发送了一个空值。是的,浏览器中表单的输出给我“请选择一个卷”,但当我单击“提交”时,表单无效,代码停在这里“如果($this->form->isValid()”),这意味着表单无效?Michal的技术是正确的。如果表格无效,那是因为其他原因。但是Michal的答案是如何向choice小部件添加默认值。不能在表单中发送空值。NULL由PHP解释。你不应该否决那些试图帮助你Nll的人。你将发送一个空值(当然不是空值,但可以用类似的方式使用)@Nll表单未通过验证,因为在验证程序中您不允许空值(请注意,发送空值并不等于什么都不发送!)。@antony您也错了,因为我并不愚蠢,第一次不测试此解决方案,任何人都会尝试,但是它不起作用,它给出了无效的表单错误,我确信我表单的代码是正确的。因为我发送了一个索引为空的数组,这意味着索引为null的数组!另外,当我用-1更改null时,我的表单工作正常。。。