Symfony 1.4 表单复选框数组到字符串对话

Symfony 1.4 form checkbox array to string conversation

我在 Symfony 1.4 应用程序中创建复选框时出现以下错误。 复选框已呈现,但出现 "Array to string conversation" 错误。

下面是我的代码。

表格

$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,
    ]));

它 returns 任何错误,但如果我向数组添加一个值,错误再次显示。

整个class代码

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
]);

我在表单 class 中使用它,这就是为什么你看到 $this->validatorSchema,如果你在其他任何地方使用它,比如在 OP 案例中,只需添加'multiple' => true 作为选项数组中的选项。