【问题标题】:Bootstrap Modal With Codeigniter Form Validation带有 Codeigniter 表单验证的引导模式
【发布时间】:2015-07-19 00:11:13
【问题描述】:

在我的带有 codeigniter 的引导模型表单上,如果输入的用户名不正确,它应该会显示消息。 但是当我点击按钮时,即使显示验证错误,引导模式也会消失

目标:我想做的是,如果任何表单验证为真,则不会关闭引导模式,但如果 form_validation 正常,那么让我提交。

如果出现任何表单验证错误,如何防止引导模式关闭。

我尝试过使用e.preventDefault(); 没有运气

模型视图

<?php if ($users) { ?>
<?php foreach ($users as $user) { ?>
<div class="modal fade" id="myModal-<?php echo $user['username']; ?>">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header clearfix">
            <div class="pull-left">
            <h2 style="font-size: 18px;">Are you absolutely sure?</h2></div>
            <div class="pull-right">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            </div>
            <div class="modal-body">
            <div class="alert alert-warning text-center">
                Unexpected bad things will happen if you don't read this! 
            </div>
            <p class="text-center">If you delete this user, this user will not be able to login to the admin,
            and all of his or her user information will be <b>Removed For Ever!</b>
            </p>
            <br/>
            <p>Please type in the username of the user to confirm. <?php echo validation_errors('<div class="text-danger validation">', '</div>'); ?></p>
            <form role="form" action="<?php echo $action;?>" method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <input type="text" id="input-user-<?php echo $user['username']; ?>" name="username" value="" class="form-control" />
                    <input type="hidden" name="user_id" value="<?php echo $user['user_id']; ?>" class="form-control" />
                </div>
                <div class="form-group text-center">
                    <button type="submit" id="button-delete-user-<?php echo $user['username']; ?>" class="btn btn-user-delete"><span class="text-danger">I understand the consequences, deleting this user</span></button>
                </div>
            </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- Bootstrap Model Form Validation Check --> 
<script type="text/javascript">
$(document).ready(function(){
$('#button-delete-user-<?php echo $user['username']; ?>').on('click', function(e) {
    e.preventDefault();
});
});
</script>

<!-- Enabled Submit Button If Text Is Entered In Input -->
<script>
$(document).ready(function(){
$('#button-delete-user-<?php echo $user['username']; ?>').attr('disabled',true);

    $('#input-user-<?php echo $user['username']; ?>').keyup(function(){
        if($(this).val().length !=0)
        $('#button-delete-user-<?php echo $user['username']; ?>').attr('disabled', false);            
        else
        $('#button-delete-user-<?php echo $user['username']; ?>').attr('disabled',true);
    });
});
</script>

<?php }?>
<?php }?>

控制器功能删除

public function delete() {
  // Note: Form Validation Library Auto Loaded

    $this->form_validation->set_rules('username', 'Username', 'required|callback_confirm_username_before_delete');
    $this->form_validation->set_rules('user_id', 'User ID Number', 'required|callback_validateForm');

    if ($this->form_validation->run($this) == FALSE) {

        $this->get_list();

    } else {

        $this->delete_user();

        $this->session->set_flashdata('info', 'Your user'.' '. $this->input->post('username') .' '.'was successfully deleted.');
        redirect('admin/user/users');
    }

  }

【问题讨论】:

  • “验证”css 声明后缺少右括号:&lt;?php echo validation_errors('&lt;div class="text-danger validation"&gt;', '&lt;/div&gt;'); ?&gt;
  • bootstrap modal 仍然没有,尽管即使是固定的

标签: javascript jquery codeigniter


【解决方案1】:

Error_validation 是 CI 提供的功能。

您只能在页面提交时使用error_validation的代码点火器,因此,由于页面正在提交,引导模式会自动关闭。

如果你想对模态框进行表单验证,最好使用AJAX

如果要在 form_open 上方显示错误信息, 只需制作一个空白 div,例如:

<div class='error_msg'>

</div>

并使用 jquery:

$(function(){

$('#form_id').submit(function(event){
event.preventDefault();
var custemail = $('#email_id').val();
var custname = $('#name').val();

$.ajax({
        type: 'POST',
        url: your controller path,
        data: {
         'name': custname,
         'email': custemail

        },
        dataType: 'html',
        success: function(results){
             if(something not as you expected){
              $('.error_msg').html('error msg');
              return false;
             }
        }
  });


});

});

【讨论】:

  • 你有例子吗,因为我希望 form_validation 消息仍然显示在打开的表单上方的表单上。
  • 我添加了我的控制器删除功能数据中的内容:在 ajax 中?
  • 只需将字段信息作为 json 传递并在您的 php 文件中验证它
  • 在包含表单的模式中显示错误时,我也遇到了同样的问题。
猜你喜欢
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-06
相关资源
最近更新 更多