Blame view

frontend/modules/forms/Module.php 2.31 KB
c7f97c88   alex   Google recaptcha ...
1
2
3
4
5
6
7
8
  <?php
  
  namespace frontend\modules\forms;
  
  
  use yii\helpers\Html;
  use yii\web\View;
  use yii\widgets\ActiveForm;
2097a1ea   alex   fix1
9
  use yii\base\Module as YiiModule;
c7f97c88   alex   Google recaptcha ...
10
11
12
13
14
15
16
17
18
19
  
  
  /**
   * Class ContactModule
   * @package frontend\forms
   *
   *
   * Класс, который немного видоизменяет вывод формы контактов для того, чтобы в ней появилась гугловая рекапча
   *
   */
2097a1ea   alex   fix1
20
  class Module extends YiiModule
c7f97c88   alex   Google recaptcha ...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  {
  
  
  	public function renderForm(View $view)
  	{
  		if ($this->ajax) {
  			$js = <<<JS
  $(document).on('submit', '#{$this->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,
2097a1ea   alex   fix1
45
  				'action' => ($this->ajax) ? '/' . $this->id . '/save/ajax' : '/' . $this->id . '/save/no-ajax',
c7f97c88   alex   Google recaptcha ...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  				'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();
  	}
  }