Completed
Push — master ( 3fe54f...10b459 )
by Basil
02:11
created

SubmitButtonWidget::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.7666
cc 2
nc 2
nop 0
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
11
/**
12
 * Generates a submit button for a form. This should be used when submiting payment forms
13
 * in order to ensure a request is not send twice.
14
 *
15
 * ```php
16
 * $form = ActiveForm::begin();
17
 * // form code
18
 *
19
 * SubmitButtonWidget::widget(['label' => 'Save', 'pushed' => 'Saving ...', 'options' => ['class' => 'btn btn-primary']]);
20
 * $form::end();
21
 * ```
22
 *
23
 * @author Basil Suter <[email protected]>
24
 * @since 1.0.21
25
 */
26
class SubmitButtonWidget extends Widget
27
{
28
    /**
29
     * @var string The label which should be displayed on button.
30
     */
31
    public $label;
32
    
33
    /**
34
     * @var string The label which should be visible when the button is pushed. for example `... sending`.
35
     */
36
    public $pushed;
37
38
    /**
39
     * @var array An array with Options which can be passed to the button, see {{luya\helpers\Html::submitButton}}.
40
     */
41
    public $options = [];
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function init()
47
    {
48
        parent::init();
49
50
        if (!$this->label) {
51
            throw new InvalidConfigException("The label property can not be empty.");
52
        }
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function run()
59
    {
60
        $js = [
61
            'this.disabled=true;',
62
        ];
63
64
        if ($this->pushed) {
65
            $js[] = "this.innerHTML='{$this->pushed}';";
66
        }
67
68
        return Html::decode(Html::submitButton($this->label, ArrayHelper::merge([
69
            'onclick' => new JsExpression(implode(" ", $js)),
70
            'encoding' => false,
71
        ], $this->options)));
72
    }
73
}