如何通过URI发送表单验证错误消息?

时间:2017-11-03 07:14:42

标签: php codeigniter validation

我有一个页面可以在Codeigniter中更改用户密码。它位于URI case "change_password": echo '<div class="panel-body"> <center><h2 style="font-family:baloo bhaijaan">Ganti Password</h2> <form method="post" action="'.base_url('admin/change_password_action').'"> <div class="form-group"> <strong >Password lama</strong> <h6 style="font-size: 10px; margin-top: 10px; margin-bottom: 2px">'.form_error('password').'</h6> <input type="password" name="password" placeholder="**********" class="form-control" value="" required> </div> <div class="form-group"> <strong>Password baru</strong> <h6 style="font-size: 10px; margin-top: 10px; margin-bottom: 2px">'.form_error('password_baru').'</h6> <input type="password" name="password_baru" id="password2" placeholder="**********" class="form-control" required> </div> <div class="form-group"> <strong>Konfirmasi Password baru</strong> <h6 style="font-size: 10px; margin-top: 10px; margin-bottom: 2px">'.form_error('password_baru2').'</h6> <input type="password" name="password_baru2" id="password3" placeholder="**********" class="form-control" required> </div> <p> <button type="submit" style="font-family:baloo bhaijaan" name="submit" value="login" class="btn btn-succes">Ganti password</button> </form> </div>'; break; 中。如何发送错误消息?因为如果我使用重定向,则不会显示该消息。

我的观点在switch语句中。以下是我的观点:

   if ($this->form_validation->run() == FALSE) {   
       $this->session->set_flashdata('error', validation_errors());
       if (!empty($this->session->flashdata('error'))) {
           $data['error_data'] = $this->session->flashdata('error');
       }    
       redirect('admin/admin_page/ganti_password');
   }
   else

这是我的控制器:

   string pianist = "Johann Sebastian Bach";
    string[] asd = pianist.Split(" ");

2 个答案:

答案 0 :(得分:1)

只需将错误存储在Flash数据中并显示您重定向的位置。

发送错误消息,如:

 $this->session->set_flashdata('error', validation_errors());

打印错误(适用于查看)

if (!empty($this->session->flashdata('error'))) {

    echo $this->session->flashdata('error');
}

错误(对于控制器)

if (!empty($this->session->flashdata('error'))) {

    $data['error_data'] = $this->session->flashdata('error');
}

答案 1 :(得分:1)

//using flash data you display error message, no need to pass any thing through URI

/* controller side */
   $this->form_validation->set_rules('password', 'Password',  'trim|required');
   $this->form_validation->set_rules('password_baru', 'Password Baru',  'trim|required');
   $this->form_validation->set_rules('password_baru2', 'Password Baru2',  'trim|required');
   if ($this->form_validation->run() == FALSE) {

           $this->session->set_flashdata('error', validation_errors());
           redirect('admin/admin_page/change_password');

      } else { 
               /*your action*/
      }

/* Views */
In admin/admin_page/change_password views page any where put below code to display error message. it will appear when page redirect time only. if you refresh flash message will vanish automaticlly

echo $this->session->flashdata('error');
 echo validation_errors('<b>','</b>'); //this will display individual input errors