THELIA Forum

Welcome to the THELIA support and discusssion forum

Announcement

Rejoignez la communauté sur le Discord Thelia : https://discord.gg/YgwpYEE3y3

Offline


Bonjour,

Lorsque j'essaye de sauvegarde mes paramètres sur mon template du module custom développé,
j'obtiens l'erreur suivant : Notice: Array to string conversion

Voici les codes de mes fichiers :

<?php

namespace Subscription\Form;

use Subscription\Subscription;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Thelia\Form\BaseForm;
use Thelia\Model\ConfigQuery;

class ConfigurationForm extends BaseForm
{
    protected function buildForm()
    {

        $bank_card = (Subscription::getConfigValue('bank_card'))? true : false;
        $single_euro_payments_area = (Subscription::getConfigValue('single_euro_payments_area'))? true : false;
        
        $this->formBuilder
        	->add(
        		'bank_card',
        		'checkbox',
        		[	
        			'value' => 'CB',
        			'required' => false,
        			'label' => $this->translator->trans('Bank Card', [], Subscription::DOMAIN_NAME),
        			'data' => $bank_card,

        		]
        	)
        	->add(
        		'single_euro_payments_area',
        		'checkbox',
        		[	
        			'value' => 'CB',
        			'required' => false,
        			'label' => $this->translator->trans('Single Euro Payments Area', [], Subscription::DOMAIN_NAME),
        			'data' => $single_euro_payments_area,

        		]
        	)
        	->add('day_of_month', 
        		'choice',
        		[
        			'choices' => $this->getChoicesList(),
                    'required' => true,
                    'multiple' => true,
                    'constraints' => array(
                        new NotBlank(),
                    ),
                    'label' => $this->translator->trans('Choose the date(s) available for bank transfer', [], Subscription::DOMAIN_NAME),
                    'data' => $this->computeDataList(),
        		]
            )
        ;
    }

    protected function computeDataList()
    {
        $values = json_decode(ConfigQuery::read("subscription_day_of_month", ""), TRUE);
        $return = array();
        for ($i = 1; $i <= 31; $i++) {
            $return["$i"] = false;
        }
        if ($values != NULL)
        {
            foreach ($values as $k => $v)
                $return["$v"] = true;
        }

        return $return;
    }

	protected function getChoicesList()
	{
	    $return = array();
	    for ($i = 1; $i <= 31; $i++) {
	        $return["$i"] = "$i";
	    }
	    return $return;
	}


    public function getName()
    {
        return 'subscription_configuration_form';
    }

}
namespace Subscription\Controller;

use Subscription\Subscription;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Tools\URL;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * Class Configuration
 * @package Subscription\Controller
 */
class ConfigurationForm extends BaseAdminController
{
    public function saveAction()
    {
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'Subscription', AccessManager::UPDATE)) {
            return $response;
        }

        $configurationForm = $this->createForm('subscription.form.configure');

        try {

            $form = $this->validateForm($configurationForm, "POST");

            // Get the form field values
            $data = $form->getData();

            foreach ($data as $name => $value) {
                Subscription::setConfigValue($name, $value);
            }

            if ($this->getRequest()->get('save_mode') == 'stay') {
                // If we have to stay on the same page, redisplay the configuration page/
                $url = '/admin/module/subscription';
            } else {
                // If we have to close the page, go back to the module back-office page.
                $url = '/admin/modules';
            }

            return $this->generateRedirect(URL::getInstance()->absoluteUrl($url));

        } catch (FormValidationException $ex) {
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
        } catch (\Exception $ex) {
            $error_msg = $ex->getMessage();
        }

        $this->setupFormErrorContext(
            'Subscription configuration',
            $error_msg,
            $configurationForm,
            $ex
        );

        return $this->generateRedirect(URL::getInstance()->absoluteUrl('/admin/module/subscription'));
    }
}

Offline


A quelle ligne se produit l'erreur ? On peut avoir la stack trace complète svp ?


OpenStudio Toulouse

Offline


L'erreur s'affiche côté front  qui est : Array to string conversion

Offline


Ton erreur vient du fait que tu as un array comme valeur de retour des cases à cocher et que t'essaie de sauvegarder un array

foreach ($data as $name => $value) {
                Subscription::setConfigValue($name, $value);
            }

Il te faudrait un traitement particulier pour le cas des checkbox afin de convertir $value en string (json_encode ou serialize)

Last edited by wisejack (15-03-2017 15:28:44)