Jump to content

[RÉSOLU] Insérer de nouveaux champs au milieu du formulaire


sococa

Recommended Posts

Bonjour tout le monde,

J'ai créé un module pour ajouter des champs au formulaire d'inscription (customerform) en utilisant le hook additionalCustomerFormFields.
Le truc, c'est que j'aimerais que les nouveaux champs apparaissent au milieu du customerform, après le champ siret. Pas à la fin.
J'ai vu que c'était possible en créant un hook et en appelant ce hook directement dans le customerForm, mais cela me renvoi systématiquement l'erreur " ( ! ) Warning: Array to string conversion in /var/www/prestashop/classes/Hook.php on line 936".
Voici le code en question
 

public function hookDisplayTest($params)
    {
        $countries = Country::getCountries($this->context->language->id, true);

        $additionalFields = [];

        $selectedDefaultCountry = $this->context->country->id;
        $idCountryField = (new FormField)
            ->setName('id_country')
            ->setType('select')
            ->setLabel($this->l('Pays'))
            ->setRequired(true);
        
        $additionalFields[] = $idCountryField;
        foreach ($countries as $country) {
            $idCountryField->addAvailableValue(
                $country['id_country'],
                $country['name']
            );
        }
        
        $additionalFields[] = (new FormField)
            ->setName('vat_number')
            ->setType('text')
            ->setLabel($this->l('N° TVA'))
            ->setMaxLength(12)
            ->setRequired(true)
            ->addConstraint('isVatNumber');

        $additionalFields[] = (new FormField)
            ->setName('activity')
            ->setType('select')
            ->setLabel($this->l('Activité'))
            ->setRequired(true)
            ->setAvailableValues([
                'Commerce' => $this->l('Commerce'),
                'Industrie' => $this->l('Industrie'),
                'Services' => $this->l('Services'),
                'Autre' => $this->l('Autre'),
            ]);
            
        $additionalFields[] = (new FormField)
            ->setName('phone')
            ->setType('tel')
            ->setLabel($this->l('Téléphone'))
            ->setRequired(true)
            ->addConstraint('isPhoneNumber');


        
        $additionalFields[] = (new FormField)
            ->setName('address1')
            ->setType('text')
            ->setLabel($this->l('Adresse'))
            ->setRequired(true)
            ->addConstraint('isAddress');

        $additionalFields[] = (new FormField)
            ->setName('address2')
            ->setType('text')
            ->setLabel($this->l('Complément d\'adresse'))
            ->addConstraint('isAddress');


        $additionalFields[] = (new FormField)
            ->setName('postcode')
            ->setType('text')
            ->setLabel($this->l('Code postal'))
            ->setRequired(true)
            ->addConstraint('isPostCode');

        $additionalFields[] = (new FormField)
            ->setName('city')
            ->setType('text')
            ->setLabel($this->l('Ville'))
            ->setRequired(true)
            ->addConstraint('isCityName');

        return $additionalFields;
    }

<form action="{block name='customer_form_actionurl'}{$action}{/block}" id="customer-form" class="js-customer-form" method="post">
  <div>
    {block "form_fields"}
      {foreach from=$formFields item="field"}
        {block "form_field"}
          {if $field.type === "password"}
            <div class="field-password-policy">
              {form_field field=$field}
            </div>
          {else}
            {form_field field=$field}
            {if $field.name == "siret"}
              <p><a href="/nous-contacter" target="_blank">Pas encore de SIRET? Contactez-nous</a></p>
              {hook h='displayTest'}
            {/if}
          {/if}
        {/block}

      {/foreach}

      {$hook_create_account_form nofilter}
    {/block}
    
  </div>

J'ai aussi essayé avec le hook actionCustomerFormBuilderModifier, mais n'ai pas eu plus de résultats. Il ne se passait rien.

j'espère que quelqu'un pourra m'aider.

Merci d'avance :) 

ps_customregistrationfields.zip

Edited by sococa (see edit history)
Link to comment
Share on other sites

Bonjour @Prestashop Addict et merci. 
Pour réorganiser formfield, si j'ai  bien compris, ce n'est pas nécessaire puisque je ne veux pas modifier l'ordre des champs du formulaire. Je veux simplement en ajouter des nouveaux, au milieu. Concernant CustomerRegistrationFormatter, je n'arrive pas à l'implémenter. Peux-tu m'en dire plus s'il-te-plait ?
 

Link to comment
Share on other sites

Bonjour.

Exemple d'ajout de champs après le nom de famille :

/*$additionalFields[] = (new FormField)
        ->setName('other_activity')
        ->setType('text')
        ->setLabel($this->l('Préciser votre activité'))
        ->setRequired(false)
        ->setAvailableValues([]);*/

        $position = (int) array_search('lastname', array_keys($params['fields']), null) + 1;
        $fieldcount = count($params['fields']);
        $result = array_merge(
            array_slice($params['fields'], 0, $position),
            $additionalFields,
            array_slice($params['fields'], $position - $fieldcount)
        );
        $params['fields'] = $result;
        //return $additionalFields;

 

image.thumb.png.75e3ed690cff6e479f135ee1e03077db.png

  • Like 1
Link to comment
Share on other sites

Exemple d'ajout de champs pour n'importe quelle partie du formulaire :

public function hookAdditionalCustomerFormFields($params)
{
    $countries = Country::getCountries($this->context->language->id, true);

    $additionalFields = [];

    $fields = $params['fields'];
    foreach ($fields as $key => $f) {
        if ($key == 'lastname') {
            $selectedDefaultCountry = $this->context->country->id;
            $idCountryField = (new FormField)
                ->setName('id_country')
                ->setType('select')
                ->setLabel($this->l('Pays'))
                ->setRequired(true);
    
            $additionalFields[] = $idCountryField;
            foreach ($countries as $country) {
                $idCountryField->addAvailableValue(
                    $country['id_country'],
                    $country['name']
                );
            }
            $position = (int) array_search('lastname', array_keys($params['fields']), null) + 1;
            $result = array_merge(
                array_slice($params['fields'], 0, $position),
                $additionalFields,
                array_slice($params['fields'], $position +1)
            );

            $params['fields'] = $result;
        }

        if ($key == 'email') {
            $additionalFields[] = (new FormField)
                ->setName('vat_number')
                ->setType('text')
                ->setLabel($this->l('N° TVA'))
                ->setMaxLength(12)
                ->setRequired(true)
                ->addConstraint('isVatNumber');
            $position = (int) array_search('email', array_keys($params['fields']), null) + 1;
            $result = array_merge(
                array_slice($params['fields'], 0, $position),
                $additionalFields,
                array_slice($params['fields'], $position +1)
            );

            $params['fields'] = $result;
        }
    }
}

 

image.png.d1f35989d934e52cbf3688f7fef6297e.png

 

J'ai fait exprès une petite erreur pour que tu réfléchisses aussi 😉

Edited by ps8modules (see edit history)
  • Like 1
Link to comment
Share on other sites

Merci beaucoup @ps8modules ca marche nickel.
Par contre je n'ai pas vu l'erreur que tu as glissé ^^'
Chez moi tout fonctionne nickel, l'inscription, la vérif des champs etc.

 

    public function hookAdditionalCustomerFormFields($params)
    {
        $countries = Country::getCountries($this->context->language->id, true);

        $additionalFields = [];

        $selectedDefaultCountry = $this->context->country->id;
        $idCountryField = (new FormField)
            ->setName('id_country')
            ->setType('select')
            ->setLabel($this->l('Pays'))
            ->setRequired(true);

        foreach ($countries as $country) {
            $idCountryField->addAvailableValue(
                $country['id_country'],
                $country['name']
            );
        }

        $additionalFields[] = $idCountryField;

        $additionalFields[] = (new FormField)
            ->setName('vat_number')
            ->setType('text')
            ->setLabel($this->l('N° TVA'))
            ->setMaxLength(12)
            ->setRequired(true)
            ->addConstraint('isVatNumber');

        $additionalFields[] = (new FormField)
            ->setName('activity')
            ->setType('select')
            ->setLabel($this->l('Activité'))
            ->setRequired(true)
            ->setAvailableValues([
                'Commerce' => $this->l('Commerce'),
                'Industrie' => $this->l('Industrie'),
                'Services' => $this->l('Services'),
                'Autre' => $this->l('Autre'),
            ])
            ->setValue('Commerce'); // Par défaut, sélectionne "Commerce"

        $additionalFields[] = (new FormField)
            ->setName('phone')
            ->setType('tel')
            ->setLabel($this->l('Téléphone'))
            ->setRequired(true)
            ->addConstraint('isPhoneNumber');

        $additionalFields[] = (new FormField)
            ->setName('address1')
            ->setType('text')
            ->setLabel($this->l('Adresse'))
            ->setRequired(true)
            ->addConstraint('isAddress');

        $additionalFields[] = (new FormField)
            ->setName('address2')
            ->setType('text')
            ->setLabel($this->l('Complément d\'adresse'))
            ->addConstraint('isAddress');

        $additionalFields[] = (new FormField)
            ->setName('postcode')
            ->setType('text')
            ->setLabel($this->l('Code postal'))
            ->setRequired(true)
            ->addConstraint('isPostCode');

        $additionalFields[] = (new FormField)
            ->setName('city')
            ->setType('text')
            ->setLabel($this->l('Ville'))
            ->setRequired(true)
            ->addConstraint('isCityName');

        // Récupérer les champs existants
        $existingFields = $params['fields'];

        $position = (int) array_search('siret', array_keys($params['fields']), null) + 1;
        $fieldcount = count($params['fields']);
        $result = array_merge(
            array_slice($params['fields'], 0, $position),
            $additionalFields,
            array_slice($params['fields'], $position - $fieldcount)
        );
        $params['fields'] = $result;
    }

 

  • Like 1
Link to comment
Share on other sites

  • sococa changed the title to [RÉSOLU] Insérer de nouveaux champs au milieu du formulaire

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...