├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── src ├── Asset.php └── js │ └── disable-submit-buttons.js └── tests ├── _app ├── assets │ └── .gitignore ├── config │ ├── .gitignore │ ├── common.php │ ├── console.php │ ├── test.php │ └── web.php ├── controllers │ └── SiteController.php ├── index.php ├── models │ └── NameForm.php ├── views │ ├── layouts │ │ └── main.php │ └── site │ │ └── index.php └── yii.php ├── _bootstrap.php ├── _data └── .gitkeep ├── _output └── .gitignore ├── _support ├── .gitignore ├── ApiTester.php └── UnitTester.php ├── unit.suite.yml └── unit └── _bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | # Folders to ignore 2 | vendor 3 | 4 | # autogenerated files 5 | composer.lock 6 | tests/_support/_generated 7 | tests/_app/runtime 8 | 9 | # OS or Editor files and folders 10 | .buildpath 11 | .cache 12 | .DS_Store 13 | .idea 14 | .project 15 | .settings 16 | .tmproj 17 | desktop.ini 18 | nbproject 19 | Thumbs.db 20 | *.sublime-project 21 | *.sublime-workspace 22 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Yii2 Disable Submit Button Asset 2 | ================================ 3 | 4 | 2.0.0 5 | ----- 6 | 7 | - [BRK] PHP Requirement upgraded to 5.6 (Faryshta) 8 | - [BRK] Class `faryshta\assets\ActiveFormDisableSubmitButtonsAsset` replaced by 9 | `faryshta\disableSubmitButtons\Asset` (Faryshta) 10 | - [Enh] Added jQuery methods `disableSubmitButtons()` and 11 | `enableSubmitButtons()` (Faryshta) 12 | - [Enh] Live Demo with instructions on [README.md] file 13 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The Faryshta Yii2 JqueryTags extension is free software. It is released under the terms of the following BSD License. 2 | 3 | Copyright © 2015 by Faryshta All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 6 | 7 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | Neither the name of Yii Software LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Faryshta Yii2 Disable Buttons 2 | ========================= 3 | 4 | [![Latest Stable Version](https://poser.pugx.org/faryshta/yii2-disable-submit-buttons/v/stable)](https://packagist.org/packages/faryshta/yii2-disable-submit-buttons) [![Total Downloads](https://poser.pugx.org/faryshta/yii2-disable-submit-buttons/downloads)](https://packagist.org/packages/faryshta/yii2-disable-submit-buttons) [![Latest Unstable Version](https://poser.pugx.org/faryshta/yii2-disable-submit-buttons/v/unstable)](https://packagist.org/packages/faryshta/yii2-disable-submit-buttons) [![License](https://poser.pugx.org/faryshta/yii2-disable-submit-buttons/license)](https://packagist.org/packages/faryshta/yii2-disable-submit-buttons) 5 | 6 | Yii2 asset to automatically disable submit buttons on Yii2 [ActiveForm](http://www.yiiframework.com/doc-2.0/yii-widgets-activeform.html). 7 | 8 | ## Installation 9 | 10 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 11 | 12 | Either run 13 | 14 | ```bash 15 | composer require "faryshta/yii2-disable-submit-buttons:~2.0.0" 16 | ``` 17 | 18 | or add 19 | 20 | ``` 21 | "faryshta/yii2-disable-submit-buttons": "~2.0.0" 22 | ``` 23 | 24 | to the `require` section of your `composer.json` file. 25 | 26 | ## Register Asset 27 | 28 | Register the `faryshta\disableSubmitButtons\Asset`, preferably on your 29 | `AppAsset` dependencies 30 | 31 | ```php 32 | 33 | use faryshta\disableSubmitButtons\Asset as DisableSubmitButtonAsset; 34 | 35 | class AppAsset extends yii\web\AssetBundle 36 | { 37 | public $depends = [ 38 | DisableSubmitButtonsAsset::class, 39 | // other dependencies 40 | ]; 41 | } 42 | ``` 43 | 44 | ## Usage 45 | 46 | Add css class `disable-submit-buttons` to your [ActiveForm] 47 | (http://www.yiiframework.com/doc-2.0/yii-widgets-activeform.html) widget. 48 | 49 | Optionally if you want to change the text on a button, add the attribute 50 | `data-disabled-text` with the text to be changed. 51 | 52 | ```php 53 | $form = ActiveForm::begin([ 54 | 'options' => ['class' => 'disable-submit-buttons'], 55 | // other configurations 56 | ]); 57 | 58 | // inputs 59 | 60 | Html::submitButton('Submit', [ 61 | // optional, will show the value of `data-disabled-text` attribute 62 | // while handling the validation and submit 63 | 'data' => ['disabled-text' => 'Please Wait'] 64 | ]) 65 | 66 | $form->end(); 67 | ``` 68 | 69 | With this the `:input[type="submit"]` buttons will be disabled and if the 70 | validation fails the buttons will be enabled again. 71 | 72 | ## Javascript 73 | 74 | This asset adds 2 methods on jQuery `disableSubmitButtons()` and 75 | `enableSubmitButtons()` which can be called from a `
` tag generated by 76 | ActiveForm widget. 77 | 78 | ### Ajax Example 79 | 80 | The following example submits the form using ajax and enables the submit 81 | buttons so the ur ser can send another form after completing the ajax request. 82 | 83 | ```php 84 | 85 | $form = ActiveForm::begin([ 86 | 'id' => 'ajax-form', 87 | 'options' => ['class' => 'disable-submit-buttons'], 88 | ]); 89 | echo $form->field($model, 'name'); 90 | 91 | echo Html::submitButton('Submit', [ 92 | 'data' => ['disabled-text' => 'Validating'], 93 | ]); 94 | 95 | $form->end(); 96 | 97 | $this->registerJsFile('ajax-form.js'); 98 | ``` 99 | 100 | Then on `ajax-form.js` 101 | 102 | ```javascript 103 | $('#ajax-form').on('beforeSubmit', function () { 104 | var $yiiform = $(this); 105 | $.ajax( 106 | { 107 | type: $yiiform.attr('method'), 108 | url: $yiiform.attr('action'), 109 | data: $yiiform.serializeArray(), 110 | } 111 | ).done(function(data) { 112 | // success actions 113 | $yiiform.enableSubmitButtons(); // enable the submit buttons 114 | }); 115 | 116 | return false; // prevent default form submission 117 | }); 118 | ``` 119 | 120 | ## Live Demo 121 | 122 | You can run a live demo of this library with the following composer commands 123 | 124 | ```bash 125 | 126 | composer deploy-tests 127 | composer yii serve 128 | ``` 129 | 130 | ## License 131 | 132 | The BSD License (BSD). Please see [License File](LICENSE.md) for more information. 133 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "faryshta/yii2-disable-submit-buttons", 3 | "description": "Yii2 asset to automatically disable submit buttons on Yii2 ActiveForm.", 4 | "keywords": [ 5 | "yii", 6 | "yii2", 7 | "activeform", 8 | "jquery", 9 | "disable" 10 | ], 11 | "type": "yii2-extension", 12 | "license": "BSD-3-Clause", 13 | "homepage": "https://github.com/Faryshta/yii2-jquery-tagsinput", 14 | "authors": [ 15 | { 16 | "name": "Angel (Faryshta) Guevara", 17 | "email": "angeldelcaos@gmail.com", 18 | "homepage": "https://github.com/Faryshta", 19 | "role": "Developer" 20 | } 21 | ], 22 | "require": { 23 | "php": ">=5.6", 24 | "yiisoft/yii2": "~2.0.0" 25 | }, 26 | "require-dev": { 27 | "yiisoft/yii2-debug": "*" 28 | }, 29 | "config": { 30 | "process-timeout": 1800, 31 | "fxp-asset": { 32 | "enabled": false 33 | } 34 | }, 35 | "repositories": [ 36 | { 37 | "type": "composer", 38 | "url": "https://asset-packagist.org" 39 | } 40 | ], 41 | "autoload": { 42 | "psr-4": { 43 | "faryshta\\disableSubmitButtons\\": "src" 44 | } 45 | }, 46 | "scripts": { 47 | "yii": "@php tests/_app/yii.php", 48 | "deploy-tests": "@composer update --prefer-stable" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Asset.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class Asset extends AssetBundle 12 | { 13 | /** 14 | * @inheritdoc 15 | */ 16 | public $sourcePath = __DIR__ . '/js'; 17 | 18 | /** 19 | * @inheritdoc 20 | */ 21 | public $js = ['disable-submit-buttons.js',]; 22 | 23 | /** 24 | * @inheritdoc 25 | */ 26 | public $depends = [JqueryAsset::class]; 27 | } 28 | -------------------------------------------------------------------------------- /src/js/disable-submit-buttons.js: -------------------------------------------------------------------------------- 1 | $.fn.extend({ 2 | _disableSubmitButtonSelector : ':input[type="submit"]', 3 | 4 | disableSubmitButtons : function() 5 | { 6 | $(this).find(this._disableSubmitButtonSelector) 7 | .attr('disabled', 'disabled'); 8 | 9 | $(this).find(this._disableSubmitButtonSelector + '[data-disabled-text]') 10 | .each(function () { 11 | var $this = $(this) 12 | if ($this.prop('tagName').toLowerCase() === 'input') { 13 | $this.data('enabled-text', $this.val()); 14 | $this.val($this.data('disabled-text')); 15 | } else { 16 | $this.data('enabled-text', $this.html()); 17 | $this.html($this.data('disabled-text')); 18 | } 19 | }); 20 | }, 21 | 22 | enableSubmitButtons: function () 23 | { 24 | $(this).find(this._disableSubmitButtonSelector) 25 | .removeAttr('disabled'); 26 | $(this).find(this._disableSubmitButtonSelector + '[data-disabled-text]') 27 | .each(function () { 28 | var $this = $(this) 29 | if ($this.prop('tagName').toLowerCase() === 'input') { 30 | $this.val($this.data('enabled-text')); 31 | } else { 32 | $this.html($this.data('enabled-text')); 33 | } 34 | }); 35 | } 36 | }); 37 | 38 | $('form.disable-submit-buttons').on('beforeValidate', function (event) { 39 | $(this).disableSubmitButtons(); 40 | }).on('afterValidate', function (event, messages, errors) { 41 | if (errors.length == 0) { 42 | return; 43 | } 44 | 45 | $(this).enableSubmitButtons(); 46 | }).on('ajaxComplete', function () { 47 | $(this).enableSubmitButtons(); 48 | }); 49 | -------------------------------------------------------------------------------- /tests/_app/assets/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /tests/_app/config/.gitignore: -------------------------------------------------------------------------------- 1 | db.local.php 2 | -------------------------------------------------------------------------------- /tests/_app/config/common.php: -------------------------------------------------------------------------------- 1 | dirname(__DIR__), 5 | 'language' => 'en-US', 6 | 'aliases' => [ 7 | '@tests' => dirname(dirname(__DIR__)), 8 | '@faryshta/disableSubmitButtons' => dirname(dirname(dirname(__DIR__))) . '/src', 9 | ], 10 | ]; 11 | -------------------------------------------------------------------------------- /tests/_app/config/console.php: -------------------------------------------------------------------------------- 1 | 'yii2-test-console', 9 | 'components' => [ 10 | 'log' => null, 11 | 'cache' => null, 12 | ], 13 | 'controllerMap' => [ 14 | 'serve' => [ 15 | 'class' => controllers\ServeController::class, 16 | 'docroot' => '@app', 17 | ], 18 | ], 19 | ] 20 | ); 21 | -------------------------------------------------------------------------------- /tests/_app/config/test.php: -------------------------------------------------------------------------------- 1 | 'yii2-disable-submit-buttons-tests', 13 | 'components' => [ 14 | 'mailer' => [ 15 | 'useFileTransport' => true, 16 | ], 17 | 'request' => [ 18 | 'cookieValidationKey' => 'test', 19 | 'enableCsrfValidation' => false, 20 | ], 21 | ], 22 | 'params' => [], 23 | ] 24 | ); 25 | -------------------------------------------------------------------------------- /tests/_app/config/web.php: -------------------------------------------------------------------------------- 1 | 'yii2-disable-submit-buttons-demo', 7 | 'bootstrap' => ['debug'], 8 | 'aliases' => [ 9 | '@vendor' => VENDOR_DIR, 10 | '@bower' => VENDOR_DIR . '/bower-asset', 11 | ], 12 | 'modules' => [ 13 | 'debug' => [ 14 | 'class' => yii\debug\Module::class, 15 | ], 16 | ], 17 | 'components' => [ 18 | 'assetManager' => [ 19 | 'basePath' => dirname(__DIR__) . '/assets', 20 | ], 21 | ], 22 | ] 23 | ); 24 | -------------------------------------------------------------------------------- /tests/_app/controllers/SiteController.php: -------------------------------------------------------------------------------- 1 | load(Yii::$app->request->post()) && $model->validate(); 15 | 16 | return $this->render('index', ['model' => $model]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/_app/index.php: -------------------------------------------------------------------------------- 1 | run(); 6 | -------------------------------------------------------------------------------- /tests/_app/models/NameForm.php: -------------------------------------------------------------------------------- 1 | 4], 18 | ]; 19 | } 20 | /** 21 | * @inheritdoc 22 | */ 23 | public function attributeLabels() 24 | { 25 | return [ 26 | 'name' => 'Name', 27 | ]; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/_app/views/layouts/main.php: -------------------------------------------------------------------------------- 1 | title) { 9 | $this->title = 'Demo Disable Submit Buttons'; 10 | } 11 | 12 | $this->beginPage(); 13 | 14 | ?> 15 | 16 | 17 | <?= Html::encode($this->title) ?> 18 | head(); 20 | $this->beginBody(); 21 | ?> 22 |

title ?>

23 | 24 | endBody(); 26 | $this->endPage(); 27 | ?> 28 | -------------------------------------------------------------------------------- /tests/_app/views/site/index.php: -------------------------------------------------------------------------------- 1 | name && !$model->hasErrors()) { 10 | echo Html::tag('h2', 'Hello ' . $model->name); 11 | } 12 | 13 | echo Html::tag('p', 'Normal Active Form'); 14 | 15 | $form = ActiveForm::begin([ 16 | 'options' => ['class' => 'disable-submit-buttons'], 17 | ]); 18 | echo $form->field($model, 'name'); 19 | 20 | echo Html::submitButton('Submit', [ 21 | 'data' => ['disabled-text' => 'Validating'], 22 | ]); 23 | 24 | $form->end(); 25 | 26 | echo Html::tag('p', 'Ajax Active Form'); 27 | 28 | $form = ActiveForm::begin([ 29 | 'id' => 'ajax-form', 30 | 'options' => ['class' => 'disable-submit-buttons'], 31 | ]); 32 | echo $form->field($model, 'name'); 33 | 34 | echo Html::submitButton('Submit', [ 35 | 'data' => ['disabled-text' => 'Validating'], 36 | ]); 37 | 38 | $form->end(); 39 | 40 | $js = <<<'JAVASCRIPT' 41 | $('#ajax-form').on('beforeSubmit', function () { 42 | var $yiiform = $(this); 43 | $.ajax( 44 | { 45 | type: $yiiform.attr('method'), 46 | url: $yiiform.attr('action'), 47 | data: $yiiform.serializeArray(), 48 | } 49 | ).done(function(data) { 50 | alert('Information Received'); 51 | $yiiform.enableSubmitButtons(); 52 | }); 53 | 54 | return false; // prevent default form submission 55 | }); 56 | JAVASCRIPT; 57 | 58 | $this->registerJS($js); 59 | -------------------------------------------------------------------------------- /tests/_app/yii.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | run(); 12 | exit($exitCode); 13 | -------------------------------------------------------------------------------- /tests/_bootstrap.php: -------------------------------------------------------------------------------- 1 |