Codeigniter form validation error messages with bootstrap

时间:2016-08-30 04:49:27

标签: php codeigniter bootstrap-modal

I am new to php codeigniter framework. I am using basic form submission to submit data through a form. There is a form validation error showing function in codeigniter framework. What I want to do is, when there is an error show it an a bootstrap popup or warning message. I tried so many ways but did not succeed. When I used bootstrap warning messages, I was unable to hide it when there was no error and in page load. Can someone please help me with this.

2 个答案:

答案 0 :(得分:3)

使用flashdata显示错误消息

 $this->session->set_flashdata('flashSuccess', 'Leave Applied successfully');
 $this->session->set_flashdata('flashError', 'Some error found');

在标题中添加一些代码以显示Flash数据

  <div class="container">
                <div class="row" id="flashMessage">
                    <?php
                    if ($this->session->flashdata('flashError')) {
                        ?>
                        <br/>
                        <div class="alert alert-danger alert-dismissable">
                            <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                            <h4>Error!</h4>
                            <p><?php echo $this->session->flashdata('flashError'); ?>.</p>
                        </div> 
                        <?php
                    }
                    if ($this->session->flashdata('flashSuccess')) {
                        ?>
                        <br/>
                        <div class="alert alert-success alert-dismissable">
                            <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                            <h4>Success!</h4>
                            <p><?php echo $this->session->flashdata('flashSuccess'); ?>.</p>
                        </div> 
                        <?php
                    }
                    ?>

                </div>
            </div>

答案 1 :(得分:3)

在提交表单时,在codeigniter中

,您可以验证表单元素,如下所示:

$this->form_validation->set_rules('your element', ' ', 'trim|required');
if ($this->form_validation->run() == FALSE) 
{
   $this->session->set_flashdata('message', 'Yor're data is not valid!');
}

你可以在视图中使用up session来获取这样的引导警报:

<div style="margin-top: 8px" id="message">
           <?php
           if($this->session->userdata('message') != null)
           {
               echo '<div class="alert alert-success" role="alert">';
               echo '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
               echo $this->session->userdata('message') <> '' ? $this->session->userdata('message') : '';
               echo '</div>';
           }
           ?>
</div>