Search code examples
phpsymfonysymfony1symfony-1.4symfony-forms

Symfony 1.4 form checkbox array to string conversation


I have following error during creating checboxes in Symfony 1.4 app. Checkboxes are rendered but with "Array to string conversation" error.

Below is my code.

Form

$this->setWidget('emails', new sfWidgetFormChoice([
      'label'    => 'Emails',
      'expanded' => true,
      'multiple' => true,
      'choices'  => array('test', 'test2'),
    ]));

Render

<div class="container emails">
    <fieldset>
      <legend>
        <?php echo $form['emails']->renderLabel(); ?>
      </legend>
        <?php echo $form['emails']->render(); ?>
        <?php echo $form['emails']->renderError(); ?>
    </fieldset>
  </div>

Error

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

My Php version is 5.5.8 Symfony version 1.4.19

I know that the best way would be move app to newest symfony version, but this application is too large to rewrite.

Anyone have idea how to solve it?

//Edit

I noticed that if I change code to

$arr = [];
    array_push($arr, 'test');
    array_push($arr, 'test2');


    $this->setWidget('emails', new sfWidgetFormSelectCheckbox([
      'label'    => __('Adresy email'),
      'choices'  =>$arr,
    ]));

it returns any errors, but if I add one more value to array, errors shows again.

Whole class code

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

Solution

  • It is a bit late, but since I was having this issue and someone else may need it, I found that the issue was that I was using a sfWidgetFormChoice set to be multiple, but I didn't tell it to the validator.

    The solution is just to pass the option to it as well.

    End up having something like

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

    I'm using it in a Form class, that's why you see $this->validatorSchema, if you are using it anywhere else, like in the OP case, just add that 'multiple' => true as option in the options array.