1

I have a form created from php with FormField and i have the country field, which contains a list of countries. My question is how can i set a country to be selected, from php and not from html templates ( cause it's created as {form_field field=$field} in the tpl file ).

Here is my code:

$countries = Country::getCountries($this->context->language->id);
    $format['country'] = (new FormField)
        ->setName('country')
        ->setType('countrySelect')
        ->setLabel(
            $this->translator->trans(
                'Country', [], 'Shop.Forms.Labels'
            )
        )
        ->setRequired(true)
    ;

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

If i could set it from php it would be awesome, cause i don't want to change core files or something. Thanks in advance.

Ciubakka
  • 33
  • 2
  • 8

2 Answers2

0
class XXXFormatter extends XXXCore
{
private $country = 666;// 666 = preselected country by id
...
}

or

class XXXFormatter extends XXXCore
{
private $country = (int) Tools::getValue('id_country');
...
  public function getFormat()
  {
   ...
   if($this->country == 666)
   {
    $format['country'] = (new FormField())
        ->setName('id_country')
        ->setType('hidden')
        ->setValue($this->country);
   }
   else
   {
          $countries = Country::getCountries($this->language->id,true);
            $format['country'] = (new FormField())
                ->setName('country')
                ->setType('countrySelect')
                ->setLabel($this->translator->trans('Country', [], 'Shop.Forms.Labels'))
                ->setRequired($this->country_is_required)
                ->setValue($this->country);
                foreach ($countries as $country) {
                    $format['country']->addAvailableValue(
                        $country['id_country'],
                        $country['name']);
                }
   }
   ...
  }
}

Now if you plan to set a relative required value, you need to make a private variable inside the class:

              $countries = Country::getCountries($this->language->id,true);
                $format['country'] = (new FormField())
                    ->setName('country')
                    ->setType('countrySelect')
                    ->setLabel($this->translator->trans('Country', [], 'Shop.Forms.Labels'))
                    ->setRequired($this->country_is_required)
                    ->setValue($this->country);
                    foreach ($countries as $country) {
                        $format['country']->addAvailableValue(
                            $country['id_country'],
                            $country['name']);
                    }

or set different private variable:

          $countries = Country::getCountries($this->language->id,true);
            $format['country'] = (new FormField())
                ->setName('country')
                ->setType('countrySelect')
                ->setLabel($this->translator->trans('Country', [], 'Shop.Forms.Labels'))
                ->setRequired($this->password_is_required)
                ->setValue($this->country);
                foreach ($countries as $country) {
                    $format['country']->addAvailableValue(
                        $country['id_country'],
                        $country['name']);
                }
-1

(new FormField)->setValue ($value) should do the job, check out classes/form/FormField.php

public function setValue($value)
erwanpia
  • 1
  • 1