【问题标题】:Prestashop php formfield set value in select dropdownPrestashop php formfield在选择下拉列表中设置值
【发布时间】:2018-04-29 17:00:50
【问题描述】:

我有一个使用 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 设置它会很棒,因为我不想更改核心文件或其他东西。提前致谢。

【问题讨论】:

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


    【解决方案1】:

    (new FormField)->setValue ($value) 应该可以完成这项工作,请查看classes/form/FormField.php

    public function setValue($value)
    

    【讨论】:

      【解决方案2】:
      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']);
                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多