Completed
Pull Request — master (#1979)
by Martin
02:01
created

SubmitButtonWidget::setActiveForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $activeForm of type object<yii\widgets\ActiveForm> is incompatible with the declared type object<luya\widgets\activeForm> of property $_activeForm.

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..

Loading history...
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) { buttonSelector.html('" . $this->pushed . "'); }                            
96
                }
97
                return true;});");
98
        } else {
99
            $js = [
100
                'this.disabled=true;',
101
            ];
102
103
            if ($this->pushed) {
104
                $js[] = "this.innerHTML='{$this->pushed}';";
105
            }
106
            $this->options = ArrayHelper::merge([
107
                'onclick' => new JsExpression(implode(" ", $js)),
108
                'encoding' => false,
109
            ], $this->options);
110
        }
111
112
        return Html::decode(Html::submitButton($this->label, $this->options));
113
    }
114
115
}
116