Completed
Pull Request — master (#1979)
by Martin
03:20 queued 25s
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
     * @since 1.0.24 First time this was introduced
46
     *
47
     * @var activeForm Define activeForm context to use widget in context and only disable button when validation succeeded
48
     */
49
    protected $activeForm;
50
51
    /**
52
     * @since 1.0.24 First time this was introduced
53
     *
54
     * @param $activeForm Set $activeForm and check for type "ActiveForm"
55
     */
56
    public function setActiveForm(ActiveForm $activeForm)
57
    {
58
        $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...
59
    }
60
61
    /**
62
     * @since 1.0.24 First time this was introduced
63
     *
64
     * @return ActiveForm Return $activeForm
65
     */
66
    public function getActiveForm()
67
    {
68
        return $this->activeForm;
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function init()
75
    {
76
        parent::init();
77
78
        if (!$this->label) {
79
            throw new InvalidConfigException("The label property can not be empty.");
80
        }
81
    }
82
83
    /**
84
     * * @since 1.0.24 Added "ActiveForm-Mode" which only disables button when given ActiveForm is validated successful
85
     *
86
     * Add beforeSubmit handler which disables button and replaces button text
87
     * @return string
88
     */
89
    public function run()
90
    {
91
        if ($this->activeForm) {
92
            $this->view->registerJs("            
93
            $(document).on('beforeSubmit', function (e) {                
94
                var buttonSelector = $(e.target).find(':submit');
95
                var formSelector = $(e.target);
96
                if (formSelector.find('div." . $this->activeForm->errorCssClass . "').length === 0) {
97
                    buttonSelector.attr('disabled', true);
98
                    var newButtonLabel = '" . $this->pushed . "';
99
                    if (newButtonLabel) {
100
                        buttonSelector.html('" . $this->pushed . "');
101
                    }                            
102
                }
103
                return true;
104
            });");
105
        } else {
106
            $js = [
107
                'this.disabled=true;',
108
            ];
109
110
            if ($this->pushed) {
111
                $js[] = "this.innerHTML='{$this->pushed}';";
112
            }
113
            $this->options = ArrayHelper::merge([
114
                'onclick' => new JsExpression(implode(" ", $js)),
115
                'encoding' => false,
116
            ], $this->options);
117
        }
118
119
        return Html::decode(Html::submitButton($this->label, $this->options));
120
    }
121
122
}
123