Created
June 17, 2015 06:23
-
-
Save tanakahisateru/953fb53e7fd66f241cae to your computer and use it in GitHub Desktop.
YiiのActiveFormサブミットを確認ダイアログ付きにするjQueryプラグイン ref: http://qiita.com/tanakahisateru/items/277d684bb75119dfc68a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.confirmBeforeSubmit = function(options) { | |
options = $.extend({ | |
message: "本当に実行しますか。" | |
}, options); | |
this.each(function() { | |
var $button = $(this); | |
var $form = $button.closest('form'); | |
var confirmed = false; | |
var message = options.message; | |
$button.on('click', function (event) { | |
confirmed = false; | |
}); | |
$form.on('beforeSubmit', function () { | |
if (!confirmed) { | |
var data = $form.data('yiiActiveForm'); | |
yii.confirm(message, function () { | |
setTimeout(function(){ | |
confirmed = true; | |
data.validated = true; | |
$form.submit(); | |
}, 1); | |
}); | |
//data.validated = false; | |
return false; | |
} else { | |
return true; | |
} | |
}); | |
}); | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="form-group"> | |
<?= Html::submitButton($model->isNewRecord ? '作成する' : '保存する', [ | |
'class' => $model->isNewRecord ? | |
'btn btn-success submit' : | |
'btn btn-primary submit', | |
]) ?> | |
</div> | |
<?php if ($model->isNewRecord) { | |
$this->registerJs( | |
"jQuery('.forum-form form .submit').confirmBeforeSubmit({ | |
message: 'なにか作成しようとしています。本当によろしいですか?' | |
});" | |
); | |
} ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment