如何在codeigniter中使用FormValidation

发布于 2024-11-09 01:58:07 字数 2229 浏览 0 评论 0原文

是否有可能如何显示控制器中的字符串以在 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;
        }
    }

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

Is it possible how to display string from controller to view in codeigniter?
im doing a form validation to check if a certain record exists in the database.

here's the code for my controller

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'));
        }

here's the code for my model

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;
        }
    }

what i wanted to happen is; how will i show an expected error message something like this "Username already exists" and i want to display it in my view. is this possible? or should i try to do another approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半衬遮猫 2024-11-16 01:58:07

您可以在此处进行学习。但为了让您更好地理解,我将解释整个事情。您的控制器应如下所示 这

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(); ?>

如果您仍然无法理解,请让我知道。

You can study here. But for your better understanding, I will explain the whole thing. Your Controller should look like this

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;
    }
}

Here's the Code for your Model

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

        return true;
    }
    else{
        return false;
    }
}

Your View should contain the following line:

<?php echo validation_errors(); ?>

If you still have problem to understand please let me know.

时光暖心i 2024-11-16 01:58:07

使用

您可以在控制器上 :
$this->session->set_flashdata('message', '

YES!

');

然后只需调用您的视图即可: session->flashdata('message'); ?>

希望有帮助

You can use

On controller:
$this->session->set_flashdata('message', '<div class="message">YES!</div>');

Then just call on your view: <?php echo $this->session->flashdata('message'); ?>

Hope that helps

飘落散花 2024-11-16 01:58:07

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

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

Since you are using Form Validation class, in view the error messages are present. All you need to call the error message using form_error(). For more details please visit http://codeigniter.com/user_guide/libraries/form_validation.html.

Also to pass a variable to view, you need to pass it when you call a view to display
$this->load->view('YOUR_VIEW', array('VARIABLE_NAME' => "VARIABLE_VALUE"));

强辩 2024-11-16 01:58:07

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

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>

这是一个有用的方法。

You can define a variable in controller class.

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>

It is an usefull method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文