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

时间:2017-06-29 06:47:40

标签: php symfony symfony1 symfony-1.4 symfony-forms

我在Symfony 1.4应用程序中创建checbox时出现以下错误。 复选框已呈现,但使用"数组来串联对话"错误。

以下是我的代码。

表格

$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;
  }
}

1 个答案:

答案 0 :(得分:0)

有些晚了,但是由于我遇到了这个问题,可能有人需要它,所以我发现问题是我使用的sfWidgetFormChoice设置为多个,但是我没有告诉交给验证人。

解决方案就是将选项也传递给它。

最终遇到类似的情况

$this->validatorSchema['choices_name'] = new sfValidatorChoice([
    'required' => false,
    'multiple' => true,
    'choices'  => $choices
]);

我在Form类中使用它,这就是为什么看到$this->validatorSchema的原因,如果您在其他任何地方使用它,例如在OP情况下,只需添加'multiple' => true作为选项在选项数组中。