Symfony 1.4表单复选框数组到字符串转换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Symfony 1.4表单复选框数组到字符串转换相关的知识,希望对你有一定的参考价值。
在Symfony 1.4应用程序中创建复选框时出现以下错误。复选框已呈现,但“阵列到字符串会话”错误。
以下是我的代码。
形成
$this->setWidget('emails', new sfWidgetFormChoice([
'label' => 'Emails',
'expanded' => true,
'multiple' => true,
'choices' => array('test', 'test2'),
]));
给予
<div class="container emails">
<fieldset>
<legend>
<?php echo $form['emails']->renderLabel(); ?>
</legend>
<?php echo $form['emails']->render(); ?>
<?php echo $form['emails']->renderError(); ?>
</fieldset>
</div>
错误
Notice: Array to string conversion in /var/www/html/symfony/1_4_1/lib/widget/sfWidgetFormSelectCheckbox.class.php on line 103
Notice: Array to string conversion in /var/www/html/symfony/1_4_1/lib/widget/sfWidgetFormSelectCheckbox.class.php on line 10
我的Php版本是5.5.8 Symfony版本1.4.19
我知道最好的方法是将应用程序移动到最新的symfony版本,但是这个应用程序太大而无法重写。
任何人都知道如何解决它?
//编辑
我注意到如果我改变代码
$arr = [];
array_push($arr, 'test');
array_push($arr, 'test2');
$this->setWidget('emails', new sfWidgetFormSelectCheckbox([
'label' => __('Adresy email'),
'choices' =>$arr,
]));
它返回任何错误,但如果我再向数组添加一个值,则会再次显示错误。
全班代码
class EmailFooterGeneratorForm extends BaseForm
{
/**
* configure...
*
* @param mixed $data
*/
public function configure()
{
sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
$this->setWidget('regards', new sfWidgetFormInputText([
'label' => __('Treść pozdrowień'),
'default' => 'Pozdrowienia/Best regards',
], [
'size' => 45
]));
$this->setWidget('emails', new sfWidgetFormChoice([
'label' => __('Adresy email'),
'choices' => $this->getDefault('phones'),
]));
$this->setWidget('phones', new sfWidgetFormChoice([
'label' => __('Numery telefonu'),
'expanded' => true,
'multiple' => true,
'choices' => $this->getDefault('phones'),
]));
$this->setWidget('employment', new sfWidgetFormSelectRadio([
'label' => __('Zatrudnienie'),
'choices' => $this->buildEmployment($this->getDefault('employment'))
]));
$this->setWidget('certyfications', new sfWidgetFormChoice([
'label' => __('Certyfikaty'),
'multiple' => true,
'expanded' => true,
'choices' => ['AEO', 'TÜV Rheinland', 'FSC']
]));
$templateChoices = [
'<a href="/images/sp/emailFooterGenerator/t1.png" target="_blank">' . __('Szablon') . ' 1 </a>',
'<a href="/images/sp/emailFooterGenerator/t2.png" target="_blank">' . __('Szablon') . ' 2 </a>'
];
$this->setWidget('templates', new sfWidgetFormSelectRadio([
'label' => __('Szablony'),
'choices' => $templateChoices
]));
$this->setWidget('www', new sfWidgetFormInputText([
'label' => __('Strona wwww'),
'default' => 'www.fakro.com'
]));
$this->setValidators([
'regards' => new sfValidatorString(
['max_length' => 50, 'min_length' => 12],
['required' => __('Wymagane'),
'min_length' => __('Treść pozdrowień musi mieć przynajmniej %min_length% znaków.'),
'max_length' => __('Treść pozdrowień może mieć maksymalnie %max_length% znaków.')
]
),
'emails' => new sfValidatorChoice(
['choices' => array_keys($this->getDefault('emails')), 'multiple' => true],
['required' => __('Wymagane')]
),
'employment' => new sfValidatorChoice(
['choices' => array_keys($this->getDefault('employment'))],
['required' => __('Wymagane')]
),
'templates' => new sfValidatorChoice(
['choices' => array_keys($templateChoices)],
['required' => __('Wymagane')]
),
'www' => new sfValidatorRegex(
['pattern' => '/^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$/'],
['invalid' => __('Niepoprawny adres strony wwww')]
)
]);
$this->validatorSchema['phones'] = new sfValidatorString(['required' => false]);
$this->validatorSchema['certyfications'] = new sfValidatorString(['required' => false]);
}
private function buildEmployment($employment)
{
$arr = [];
foreach ($employment as $key) {
$str =
"<div style='margin-left: 30px'>" .
__('Firma') . ": " . $key['company_name'] . "<br>" .
__('NIP') . ": " . $key['nip'] . "<br>" .
__('REGON') . ": " . $key['regon'] . "<br>" .
__('Miasto') . ": " . $key['city'] . "<br>" .
__('Ulica') . ": " . $key['street'] . "<br>" .
__('Kod') . ": " . $key['postal'] . "<br>" .
__('Kraj') . ": " . $key['country'] . "<br>" .
__('Wydział') . ": " . $key['depertment_name'] . "<br>" .
__('Stanowisko') . ": " . $key['job_name'] . "<br>
</div>"
;
$arr[] = $str;
}
return $arr;
}
}
答案
这有点晚了,但是由于我遇到了这个问题并且其他人可能需要它,我发现问题是我使用sfWidgetFormChoice
设置为多个,但我没有告诉验证器。
解决方案就是将选项传递给它。
最终有类似的东西
$this->validatorSchema['choices_name'] = new sfValidatorChoice([
'required' => false,
'multiple' => true,
'choices' => $choices
]);
我在Form类中使用它,这就是为什么你看到$this->validatorSchema
,如果你在其他任何地方使用它,就像在OP的情况下,只需在选项数组中添加'multiple' => true
作为选项。
以上是关于Symfony 1.4表单复选框数组到字符串转换的主要内容,如果未能解决你的问题,请参考以下文章