/
AjaxFormWidget.php
97 lines (84 loc) · 2.44 KB
/
AjaxFormWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* @link https://github.com/jwaldock/yii2-ajaxform/
* @copyright Copyright (c) 2016 Joel Waldock
*/
namespace jwaldock\ajaxform;
use yii\base\Widget;
use yii\helpers\Json;
use yii\helpers\Url;
/**
* AjaxFormWidget is a widget that provides AJAX submission for [[\yii\widgets\ActiveForm]].
* TODO docs
*
* @author Joel Waldock
*/
class AjaxFormWidget extends Widget
{
/**
* @var array|string $action the form action URL. This parameter will be processed by [[\yii\helpers\Url::to()]].
* Defaults to form action if not set.
* @see method for specifying the HTTP method for this form.
*/
public $action;
/**
* @var boolean whether to reset the form on successful submission
*/
public $resetOnSuccess;
/**
* @var boolean whether form must be reset before it can be submitted again
*/
public $requireReset;
/**
* @var string
*/
public $failCallback;
/**
* @var \yii\widgets\ActiveForm the form that ajax saving is registered for.
*/
public $form;
/**
* @var array event => handler see yii.ajaxForm.js for events
*/
public $clientEvents;
/**
* @inheritDoc
*/
public function run()
{
$view = $this->getView();
$id = $this->form->id;
$options = Json::htmlEncode($this->getClientOptions());
AjaxFormAsset::register($view);
$view->registerJs("jQuery('#$id').ajaxForm($options);");
$this->registerClientEvents();
}
/**
* Returns the options for the ajax form JS widget.
* @return array the options
*/
protected function getClientOptions()
{
$options = [
'resetOnSuccess' => $this->resetOnSuccess,
'requireReset' => $this->requireReset,
'failCallback' => $this->failCallback,
'action' => Url::to($this->action),
];
return array_filter($options, function($value) { return $value !== null;});
}
/**
* Registers client events for the ajax form JS widget.
*/
protected function registerClientEvents()
{
if (!empty($this->clientEvents)) {
$id = $this->form->id;
$js = [];
foreach ($this->clientEvents as $event => $handler) {
$js[] = "jQuery('#$id').on('$event', $handler);";
}
$this->getView()->registerJs(implode("\n", $js));
}
}
}