'btn']; /** * content for submit button * * @var string */ public $buttonContent = 'Save'; /** * @var string */ public $classForm = 'form'; public $id = 'forms'; public $buttonTemplate = '{button}'; public $locale = 'app'; /** * @inheritdoc * @throws \yii\base\InvalidConfigException */ public function init() { parent::init(); if ($this->activeRecord !== null) { $this->model = \Yii::createObject($this->activeRecord); $this->model->scenario = $this->scenario; } else { $this->model = new DynamicModel($this->attributes); $this->model->attributeLabels = $this->labels; foreach ($this->rules as $rule) { if (is_array($rule) && isset($rule[0], $rule[1])) { $attributes = $rule[0]; $validator = $rule[1]; unset($rule[0], $rule[1]); $this->model->addRule($attributes, $validator, $rule); } else { throw new InvalidConfigException( 'Invalid validation rule: a rule must specify both attribute names and validator type.' ); } } } } /** * render html form with model * * @param \yii\web\View $view */ public function renderForm(View $view) { if ($this->ajax) { $js = <<formId}', function(e) { e.preventDefault(); e.stopImmediatePropagation(); $.post( $(this).attr("action"), $(this).serialize(), {$this->successCallback}).fail({$this->errorCallback}); }); JS; $view->registerJs($js, View::POS_READY); } /** * @var ActiveForm $form */ $form = ActiveForm::begin( [ 'id' => $this->formId, 'action' => ($this->ajax) ? '/' . $this->id . '/save/ajax' : '/' . $this->id . '/save/no-ajax', 'class' => $this->classForm, ] ); $content = ''; foreach ($this->attributes as $field) { # если мы присвоили этому полю тип, то он его отрисовывает if (isset($this->inputOptions[$field]['type'])) { $function = $this->inputOptions[$field]['type']; $formStr = $form->field($this->model, $field) ->$function( isset($this->inputOptions[$field]['options']) ? $this->inputOptions[$field]['options'] : [] ); } # а если нет, то просто добавляет поле else { $formStr = $form->field($this->model, $field); } if (isset($this->labels[$field])) { $formStr->label(\Yii::t($this->locale, $this->labels[$field])); } if (isset($this->inputOptions[$field]['template'])) { $formStr = str_replace('{input}', $formStr, $this->inputOptions[$field]['template']); } $content .= $formStr; } $content .= $form->field($this->model, 'reCaptcha')->widget(\sashsvamir\yii2\recaptcha\ReCaptcha::className())->label(false); $content .= str_replace('{button}', Html::submitButton(\Yii::t($this->locale, $this->buttonContent), $this->buttonOptions), $this->buttonTemplate); $content = str_replace('{form}', $content, $this->templateForm); echo $content; ActiveForm::end(); } }