1

我有一个使用 FormField 从 php 创建的表单,并且我有国家/地区字段,其中包含国家/地区列表。我的问题是如何设置要选择的国家/地区,从 php 而不是从 html 模板(因为它在 tpl 文件中创建为 {form_field field=$field} )。

这是我的代码:

$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']
        );
    }

如果我可以从 php 设置它会很棒,因为我不想更改核心文件或其他东西。提前致谢。

4

2 回答 2

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

或者

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']);
                }
   }
   ...
  }
}

现在,如果您打算设置一个相对所需的值,您需要在类中创建一个私有变量:

              $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']);
                    }

或设置不同的私有变量:

          $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']);
                }
于 2019-12-29T18:39:09.977 回答
-1

(new FormField)->setValue ($value)应该做的工作,检查classes/form/FormField.php

public function setValue($value)
于 2019-06-12T19:04:19.087 回答