【问题标题】:how to use FormValidation in codeigniter如何在 codeigniter 中使用表单验证
【发布时间】:2011-05-23 03:11:33
【问题描述】:

是否有可能如何从控制器显示字符串以在 codeigniter 中查看? 我正在做一个表单验证来检查数据库中是否存在某个记录。

这是我的控制器的代码

function create_customer()
        {
            // field name, error message, validation rules
            $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|callback_username_exists');
            $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
            $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');

            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('admin/users/admin_form');
            }

            else
            {           
                if($query = $this->usermodel->create_customer())
                {
                        redirect('userslist');
                }
                else
                {
                    $this->load->view('admin/users/admin_form.php');            
                }
            }

        }

            function username_exists($key)
        {
            $this->usermodel->username_exists($this->input->post('username'));
        }

这是我的模型的代码

function create_customer()
    {
            $new_member_insert_data = array(
                'username' => $this->input->post('username'),
                'email' => $this->input->post('email'),
                'password ' => md5($this->input->post('password '))     
            );
            $insert = $this->db->insert('users', $new_member_insert_data);
            return $insert;
    }

    function username_exists($key)
    {
        $this->db->where('username',$this->input->post('username'));
        $query = $this->db->get('users');
        if ($query->num_rows() > 0){

            return true;
        }
        else{
            return false;
        }
    }

我想要发生的是;我将如何显示类似“用户名已存在”的预期错误消息,我想在我的视图中显示它。这可能吗?还是我应该尝试另一种方法?

【问题讨论】:

    标签: php codeigniter validation


    【解决方案1】:

    你可以学习here。但是为了您更好地理解,我将解释整个事情。你的控制器应该是这样的

    function create_customer()
    {
        // field name, error message, validation rules
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|callback_username_exists');
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
    
        if($this->form_validation->run() == FALSE)
        {
           $this->load->view('admin/users/admin_form');
        }
        else
        {           
           if($query = $this->usermodel->create_customer())
           {
               redirect('userslist');
           }
           else
           {
               $this->load->view('admin/users/admin_form.php');            
            }
        }
    }
    
    function username_exists($key)
    {
        if($this->usermodel->username_exists($key))
        {
            $this->form_validation->set_message('username_exists', 'User Name already Exists');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
    

    这是您的模型

    的代码
    function username_exists($key)
    {
        $this->db->where('username',$key);
        $query = $this->db->get('users');
        if ($query->num_rows() > 0){
    
            return true;
        }
        else{
            return false;
        }
    }
    

    您的视图应包含以下行:

    <?php echo validation_errors(); ?>
    

    如果您仍有问题需要了解,请告诉我。

    【讨论】:

      【解决方案2】:

      你可以使用

      在控制器上: $this-&gt;session-&gt;set_flashdata('message', '&lt;div class="message"&gt;YES!&lt;/div&gt;');

      那么就直接调用你的观点吧:&lt;?php echo $this-&gt;session-&gt;flashdata('message'); ?&gt;

      希望有帮助

      【讨论】:

        【解决方案3】:

        由于您使用的是表单验证类,因此存在错误消息。您只需使用form_error() 调用错误消息即可。更多详情请访问http://codeigniter.com/user_guide/libraries/form_validation.html

        同样要传递一个变量给view,需要在调用view的时候传递才能显示 $this-&gt;load-&gt;view('YOUR_VIEW', array('VARIABLE_NAME' =&gt; "VARIABLE_VALUE"));

        【讨论】:

          【解决方案4】:

          您可以在控制器类中定义一个变量。

          class Home extends CI_Controller
          {
              public function __construct()
              {
                  parent::__construct();
              }
          
              public function home()
              {
                  //This variable will be automaticly passed to the view.
                  $this->data = "some data";
          
                  $this->load->view('home_main');
              }
          

          home_main.php:

          <html>
          <head>
              <title>My Page!</title>
          </head>
          <body>
              <?php echo $this->data ?>
          </body>
          </html>
          

          这是一个有用的方法。

          【讨论】:

          • 我认为你错过了正确理解问题。
          猜你喜欢
          • 1970-01-01
          • 2015-07-02
          • 2012-12-06
          • 2012-12-19
          • 1970-01-01
          • 1970-01-01
          • 2020-06-07
          • 1970-01-01
          相关资源
          最近更新 更多