Prestashop php formfield在选择下拉列表中设置值

时间:2017-11-16 08:29:01

标签: php forms drop-down-menu prestashop formfield

我有一个使用FormField从php创建的表单,我有country字段,其中包含一个国家/地区列表。我的问题是如何设置一个国家/地区,从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设置它会很棒,因为我不想更改核心文件或其他东西。提前谢谢。

2 个答案:

答案 0 :(得分: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']);
                }

答案 1 :(得分:-1)

<input type="text" placeholder="What to remind" value={this.state.todo} onChange={this.handleChange}/> 应该做得到,请检查(new FormField)->setValue ($value)

classes/form/FormField.php