Esempio n. 1
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     if (property_exists($owner, Module::MODEL_MULTILANG_PROPERTY_NAME)) {
         foreach ((array) $owner->attributes_multilang as $attribute_multilang) {
             foreach (ModelHelper::getMultiLangAttributes($owner, $attribute_multilang) as $attribute) {
                 $this->rules[] = [[$attribute], 'safe', 'on' => 'default'];
             }
         }
     }
     parent::attach($owner);
 }
Esempio n. 2
0
 /**
  * Create ActiveField object.
  *
  * @param \yii\widgets\ActiveForm $form
  * @param \yii\db\ActiveRecord $model Model
  * @param string $attribute Model attribute
  * @param array $options Attribute options
  * @param string $type ActiveField type
  * @return \yii\widgets\ActiveField ActiveField object
  * @throws InvalidConfigException
  */
 protected function createField($form, $model, $attribute, $options, $type = 'textInput')
 {
     $options = $this->getAttributeOptions($attribute, $options);
     $fieldOptions = [];
     if (isset($options['fieldOptions'])) {
         $fieldOptions = $options['fieldOptions'];
         unset($options['fieldOptions']);
     }
     $field = $form->field($model, $attribute, $fieldOptions);
     if (isset($options['hint'])) {
         $hintOptions = [];
         if (isset($options['hintOptions'])) {
             $hintOptions = $options['hintOptions'];
             unset($options['hintOptions']);
         }
         $field->hint($options['hint'], $hintOptions);
         unset($options['hint']);
     }
     if (isset($options['label'])) {
         $labelOptions = [];
         if (isset($options['labelOptions'])) {
             $labelOptions = $options['labelOptions'];
             unset($options['labelOptions']);
         }
         $field->label($options['label'], $labelOptions);
         unset($options['label']);
     }
     if (isset($options['input'])) {
         $input = $options['input'];
         unset($options['input']);
         $field = $field->input($input, $options);
     } else {
         if ($type == 'dropDownList' || $type == 'listBox' || $type == 'checkboxList' || $type == 'radioList') {
             $items = $this->getAttributeChoices($model, $attribute);
             $field->{$type}($items, $options);
         } elseif ($type == 'select') {
             if (isset($options['data'])) {
                 $options['data'] = $options['data'] + $this->getAttributeChoices($model, $attribute);
             } else {
                 $options['data'] = $this->getAttributeChoices($model, $attribute);
             }
             $field->widget(Select2::className(), $options);
         } elseif ($type == 'widget') {
             if (isset($options['widgetClass'])) {
                 $class = $options['widgetClass'];
                 unset($options['widgetClass']);
             } else {
                 throw new InvalidConfigException('Widget class missing from configuration.');
             }
             if (property_exists($class, 'form')) {
                 $options['form'] = $form;
             }
             $field->widget($class, $options);
         } else {
             $field->{$type}($options);
         }
     }
     /**
      * Multilanguage DB fields support
      */
     if ($attributes_multilang = ModelHelper::getMultiLangAttributes($model, $attribute)) {
         $field->addon = ['prepend' => ['content' => strtoupper(substr(\Yii::$app->sourceLanguage, 0, 2))]];
         $field .= implode('', array_map(function ($attribute_multilang) use($form, $model, $attribute, $options, $type) {
             $options['label'] = false;
             $field = $this->createField($form, $model, $attribute_multilang, $options, $type);
             $field->addon = ['prepend' => ['content' => strtoupper(trim(str_replace($attribute, '', $attribute_multilang), '-_'))]];
             return $field;
         }, $attributes_multilang));
     }
     return $field;
 }