1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\widgets; |
4
|
|
|
|
5
|
|
|
use luya\base\Widget; |
6
|
|
|
use luya\helpers\Html; |
7
|
|
|
use luya\helpers\ArrayHelper; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\web\JsExpression; |
10
|
|
|
use yii\widgets\ActiveForm; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generates a submit button for a form. This should be used when submiting payment forms |
14
|
|
|
* in order to ensure a request is not send twice. |
15
|
|
|
* |
16
|
|
|
* ```php |
17
|
|
|
* $form = ActiveForm::begin(); |
18
|
|
|
* // form code |
19
|
|
|
* |
20
|
|
|
* SubmitButtonWidget::widget(['label' => 'Save', 'pushed' => 'Saving ...', 'options' => ['class' => 'btn btn-primary']]); |
21
|
|
|
* $form::end(); |
22
|
|
|
* ``` |
23
|
|
|
* |
24
|
|
|
* @author Basil Suter <[email protected]> |
25
|
|
|
* @since 1.0.21 |
26
|
|
|
*/ |
27
|
|
|
class SubmitButtonWidget extends Widget |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string The label which should be displayed on button. |
31
|
|
|
*/ |
32
|
|
|
public $label; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string The label which should be visible when the button is pushed. for example `... sending`. |
36
|
|
|
*/ |
37
|
|
|
public $pushed; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array An array with Options which can be passed to the button, see {{luya\helpers\Html::submitButton}}. |
41
|
|
|
*/ |
42
|
|
|
public $options = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var activeForm Define activeForm context to use widget in context and only disable button when validation succeeded |
46
|
|
|
* @since 1.0.24 First time this was introduced |
47
|
|
|
*/ |
48
|
|
|
private $_activeForm; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $activeForm Set $activeForm and check for type "ActiveForm" |
52
|
|
|
* @since 1.0.24 |
53
|
|
|
*/ |
54
|
|
|
public function setActiveForm(ActiveForm $activeForm) |
55
|
|
|
{ |
56
|
|
|
$this->_activeForm = $activeForm; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return ActiveForm Return $activeForm |
61
|
|
|
* @since 1.0.24 |
62
|
|
|
*/ |
63
|
|
|
public function getActiveForm() |
64
|
|
|
{ |
65
|
|
|
return $this->_activeForm; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function init() |
72
|
|
|
{ |
73
|
|
|
parent::init(); |
74
|
|
|
|
75
|
|
|
if (!$this->label) { |
76
|
|
|
throw new InvalidConfigException("The label property can not be empty."); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add beforeSubmit handler which disables button and replaces button text |
82
|
|
|
* @return string |
83
|
|
|
* @since 1.0.24 Added "ActiveForm-Mode" which only disables button when given ActiveForm is validated successful |
84
|
|
|
*/ |
85
|
|
|
public function run() |
86
|
|
|
{ |
87
|
|
|
if ($this->_activeForm) { |
88
|
|
|
$this->view->registerJs(" |
89
|
|
|
$(document).on('beforeSubmit', function (e) { |
90
|
|
|
var buttonSelector = $(e.target).find(':submit'); |
91
|
|
|
var formSelector = $(e.target); |
92
|
|
|
if (formSelector.find('div." . $this->_activeForm->errorCssClass . "').length === 0) { |
93
|
|
|
buttonSelector.attr('disabled', true); |
94
|
|
|
var newButtonLabel = '" . $this->pushed . "'; |
95
|
|
|
if (newButtonLabel) { |
96
|
|
|
buttonSelector.html('" . $this->pushed . "'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return true; |
100
|
|
|
});"); |
101
|
|
|
} else { |
102
|
|
|
$js = [ |
103
|
|
|
'this.disabled=true;', |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
if ($this->pushed) { |
107
|
|
|
$js[] = "this.innerHTML='{$this->pushed}';"; |
108
|
|
|
} |
109
|
|
|
$this->options = ArrayHelper::merge([ |
110
|
|
|
'onclick' => new JsExpression(implode(" ", $js)), |
111
|
|
|
'encoding' => false, |
112
|
|
|
], $this->options); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return Html::decode(Html::submitButton($this->label, $this->options)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..