Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Created June 17, 2015 06:23
Show Gist options
  • Save tanakahisateru/953fb53e7fd66f241cae to your computer and use it in GitHub Desktop.
Save tanakahisateru/953fb53e7fd66f241cae to your computer and use it in GitHub Desktop.
YiiのActiveFormサブミットを確認ダイアログ付きにするjQueryプラグイン ref: http://qiita.com/tanakahisateru/items/277d684bb75119dfc68a
(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);
<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