sfValidatorChoice 的问题

问题描述 投票:0回答:1

我在 sfValidatorChoice 方面遇到一些问题。这是可供选择的数组:

$branches = array(123 => 'Value 1', 456 => 'Value 2', 789 => 'Value 3')

其中数字是给定的 ID。为了获得默认值,我这样做:

$choices_branches = array_merge(array('default' => 'Bitte wählen'), $branches);

在表格课上我这样做:

$this->setWidgets(array(
    'branches'    => new sfWidgetFormChoice(array(
                        'choices'  => $choices_branches,
                        'expanded' => false,
                        )),
...))

这是验证器:

$this->setValidators(array(
  'branches'    => new sfValidatorChoice(array('choices' => array_keys($branches))),
  ...
))

但是当我选择值 1 并尝试提交表单时,我收到无效错误。

有人可以帮忙吗?

php forms symfony-1.4
1个回答
2
投票

来自 php.net

不要忘记数字键将被重新编号!

您应该执行以下操作:

$choices =  array(123 => 'Value 1', 456 => 'Value 2', 789 => 'Value 3');
$this->setWidget('branches', new sfWidgetFormChoice(array(
    'choices'  => array('' => 'Bitte wählen') + $choices,
    'expanded' => false,
));
$this->setValidator('branches', new sfValidatorChoice(array(
    'choices' => array_keys($choices),
));
© www.soinside.com 2019 - 2024. All rights reserved.