From 51db39f2d6e65843f232f0c39f11dc8cdf47056a Mon Sep 17 00:00:00 2001 From: dozer111 Date: Thu, 26 Jul 2018 09:26:52 +0300 Subject: [PATCH] fix2 --- frontend/modules/forms/Module.php | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 178 insertions(+), 9 deletions(-) diff --git a/frontend/modules/forms/Module.php b/frontend/modules/forms/Module.php index ddb2de9..271d483 100755 --- a/frontend/modules/forms/Module.php +++ b/frontend/modules/forms/Module.php @@ -2,25 +2,194 @@ namespace frontend\modules\forms; - +use yii\base\InvalidConfigException; use yii\helpers\Html; use yii\web\View; use yii\widgets\ActiveForm; -use yii\base\Module as YiiModule; /** - * Class ContactModule - * @package frontend\forms - * - * - * Класс, который немного видоизменяет вывод формы контактов для того, чтобы в ней появилась гугловая рекапча + * Module for generation forms adn saving data from frontend * + * @package artbox\core\forms */ -class Module extends YiiModule +class Module extends \yii\base\Module { + /** + * ActiveRecord for insert to db + * + * @var null + */ + public $templateForm = '{form}'; + + public $activeRecord = null; + + /** + * attributes which render in form + * + * @var array + */ + public $attributes = []; + + /** + * validation rules for DynamicModel + * + * @var array + */ + public $rules = []; + + /** + * scenario for ActiveRecord + * + * @var string + */ + public $scenario = 'default'; + + /** + * Input types for attributes. textinput default + * + * @var array + */ + public $inputOptions = []; + + /** + * Labels by form (if ActiveRecord is null) + * + * @var array + */ + public $labels = []; + + /** + * Send or not email + * + * @var bool + */ + public $sendEmail = true; + + /** + * Email address + * + * @var string + */ + public $email = 'admin@example.com'; + + /** + * Email template + * + * @var string + */ + public $emailFile = 'mail.php'; + + /** + * Email subject + * + * @var string + */ + public $subject = ''; + + /** + * mailer which send email + * + * @var null + */ + public $mailer = null; + + /** + * @var \yii\db\ActiveRecord $model ; + */ + public $model = null; + + /** + * send form with ajax + * + * @var bool + */ + public $ajax = false; + + /** + * callback function for ajax script of send form + * + * @var string + */ + public $successCallback = 'function (data) { + alert("ok"); + }'; + /** + * error function for ajax script of send form + * + * @var string + */ + public $errorCallback = 'function (data) { + alert("error"); + }'; + /** + * id form + * + * @var string + */ + public $formId = 'dynamic-form'; + + /** + * options for submit button + * + * @var array + */ + public $buttonOptions = ['class' => '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) { @@ -46,6 +215,7 @@ JS; 'class' => $this->classForm, ] ); + $content = ''; foreach ($this->attributes as $field) { @@ -71,7 +241,6 @@ JS; $content .= $formStr; } - $content .= $form->field($this->model, 'reCaptcha')->widget(\sashsvamir\yii2\recaptcha\ReCaptcha::className())->label(false); -- libgit2 0.21.4