不输出表单错误消息

时间:2012-02-11 23:06:52

标签: php codeigniter

我有一个表单没有输出任何错误消息我错过了什么?

型号:

function createUser($username = NULL ,$passwordHash = NULL ,$firstname = NULL ,$lastname = NULL ,$email = NULL,$group = NULL ,$active = NULL)
    {
        $data = array('userName' => $username, 'userFirstName' => $firstname, 'userLastName' => $lastname, 'userEmail' => $email, 'userPassword' => sha1($passwordHash), 'userGroup' => $group, 'userActive' => $active);

        $this->db->insert('users',$data);

        return TRUE;
    }

查看:

<h1><?php echo $companyName; echo nbs(1);?> - <?php echo $pageTitle; ?></h1>

<?php
if($success == TRUE) {
echo '<section id = "validation">Page Updated</section>';   
}
?>
    <p>Error: <?php echo validation_errors();?> </p>
    <div class="formContent">
        <form action="createUser" method="post">
            <fieldset class="control-group">
                <label for="userName">User Name: <input type="text" name="userName"  value="<?php echo set_value('userName'); ?>" placeholder="User Name"></label>
                <label for="userPassword">User Password: <input type="password" name="userPassword" value="<?php echo set_value('userPassword'); ?>" placeholder="User Password"></label>
                <label for="userFirstName">First Name: <input type="text" name="userFirstName" value="<?php echo set_value('userFirstName'); ?>" placeholder="First Name"></label>
                <label for="userLastName">Last Name: <input type="text" name="userLastName" value="<?php echo set_value('userLastName'); ?>" placeholder="Last Name"></label>
                <label for="userEmail">E-Mail: <input type="text" name="userEmail" value="<?php echo set_value('userEmail'); ?>" placeholder="Admin E-mail"></label>
                <label for="userGroup"> User Group:
                    <select name="userGroup" value="<?php echo set_value('userGroup'); ?>">
                        <option value="select">Please Select</option>
                        <option value="admin">Admin Group</option>
                        <option value="user">User Group</option>
                    </select>
                </label>
                <label for="userActive"> User Active:
                        <select name="userActive" value="<?php echo set_value('userActive'); ?>">
                            <option value="select">Please Select</option>
                            <option value="yes">Yes</option>
                            <option value="no">No</option>
                        </select>
                </label>
                <button type="submit" class="btn-primary">Create</button>
                </fieldset>
        </form>
    </div>

控制器:

    public function index()
    {
        $data['companyName'] = $this->core_model->companyName();
        $data['success'] ="";
        $data['pageTitle'] = "Create User";
        $this->load->view('admin/assets/header', $data);
        $this->load->view('admin/createUser', $data);
        $this->load->view('admin/assets/footer');

        if($this->input->post('submit'))
        {
                $this->form_validation->set_rules('userName', 'User Name', 'trim|required|xss_clean|callback_username_check');
                $this->form_validation->set_rules('userPassword', 'User Password', 'trim|required|xss_clean|sha1');
                $this->form_validation->set_rules('userFirstName', 'First Name', 'trim|required|xss_clean');
                $this->form_validation->set_rules('userLastName', 'Last Name', 'trim|required|xss_clean');
                $this->form_validation->set_rules('userEmail', 'E-Mail', 'trim|required|xss_clean');
                $this->form_validation->set_rules('userGroup', 'User Group', 'trim|required|xss_clean');
                $this->form_validation->set_rules('userActive', 'User Active', 'trim|required|xss_clean');

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

                $data['companyName'] = $this->core_model->companyName();
                $data['success'] ="";
                $data['pageTitle'] = "Create User";
                $this->load->view('admin/assets/header', $data);
                $this->load->view('admin/createUser', $data);
                $this->load->view('admin/assets/footer');
            }else{

            $username = $this->input->post('userName',TRUE);
            $password = $this->input->post('userPassword', TRUE);
            $firstname = $this->input->post('userFirstName', TRUE);
            $lastname = $this->input->post('userLastName',TRUE);
            $email = $this->input->post('userEmail',TRUE);
            $group = $this->input->post('userGroup',TRUE);
            $active = $this->input->post('userActive', TRUE);

            $this->db->escape($username);
            $this->db->escape($password);
            $this->db->escape($firstname);
            $this->db->escape($lastname);
            $this->db->escape($email);
            $this->db->escape($group);
            $this->db->escape($active);

            $passwordHash = $this->encrypt->sha1($password);

            if ($this->core_model->createUser($username,$passwordHash,$firstname,$lastname,$email,$group,$active)) 
            {
                $data['success'] = TRUE;
                $data['companyName'] = $this->core_model->companyName();
                $data['pageTitle'] = "Create User";
                $this->load->view('admin/assets/header', $data);
                $this->load->view('admin/createUser', $data);
                $this->load->view('admin/assets/footer');
            }else{
                $data['companyName'] = $this->core_model->companyName();
                $data['pageTitle'] = "Create User";
                $this->load->view('admin/assets/header', $data);
                $this->load->view('admin/createUser', $data);
                $this->load->view('admin/assets/footer');
            }
        }   
        }
    }

        function __username_check($userName){
        {
            if ($userName == $user->$userName) {

                $this->form_validation->set_message('username_check','Sorry the chosen username %s is taken!');

                return false;
            }else{
                return true;
            }

        }

    }

}

/* End of file login.php */
/* Location: ./application/controllers/admin/createUser.php */

1 个答案:

答案 0 :(得分:2)

您需要将<input> s 放在<{1}}标记之外!这是主要问题。

此外:

  1. 没有名为“submit”的输入:您的提交按钮实际上没有名称属性。而且,顺便说一下,因为你已经在使用form_validation类,所以检查(<label> </label>)是多余的;
  2. 我看到的另一个多余的事情是将TRUE(即,将其xss_cleaned)传递给input-&gt; post方法:你已经有了大量的xss_clean验证规则,所以为什么要再次传递 在验证检查期间已经通过它的昂贵的额外处理?
  3. 旁注,如果您使用Active Record或查询绑定,则不必转义变量,因此我也会删除该部分:)
  4. 我相信你对if input->post('submit')的调用将会失败:函数,就像“callback_”验证规则所关注的那样,是“username_check”;除了双下划线通常用于PHP中的“魔术方法”;您可以安全地删除这两者,或者如果您真的想要在函数名称上加下划线(只有一个),您可能需要调用“__username_check()”。
  5. 你加载相同的视图三次,为什么?我相信你可以像这样重写整个索引方法:

    callback__check_username
  6. <强>更新

    至于用户名检查,因为CodeIgniter的v.2.0,你在验证规则中具有这种能力:如果放置is_unique规则,事实上,它会自动查询数据库以检查它。语法是:

    function index()
    {
      $this->form_validation->set_rules('userName', 'User Name', 'trim|required|xss_clean|callback_username_check');
        $this->form_validation->set_rules('userPassword', 'User Password', 'trim|required|xss_clean|sha1');
        $this->form_validation->set_rules('userFirstName', 'First Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('userLastName', 'Last Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('userEmail', 'E-Mail', 'trim|required|xss_clean');
        $this->form_validation->set_rules('userGroup', 'User Group', 'trim|required|xss_clean');
        $this->form_validation->set_rules('userActive', 'User Active', 'trim|required|xss_clean');
    
        if ($this->form_validation->run() == FALSE) {
    
            $data['success'] ="";
    
        }else{
    
            $username = $this->input->post('userName');
            $password = $this->input->post('userPassword');
            $firstname = $this->input->post('userFirstName');
            $lastname = $this->input->post('userLastName');
            $email = $this->input->post('userEmail');
            $group = $this->input->post('userGroup');
            $active = $this->input->post('userActive');
            $passwordHash = $this->encrypt->sha1($password);
    
            if ($this->core_model->createUser($username,$passwordHash,$firstname,$lastname,$email,$group,$active)) 
            {
                $data['success'] = TRUE;
    
            }      
        }
        $data['companyName'] = $this->core_model->companyName();
        $data['pageTitle'] = "Create User";
        $this->load->view('admin/assets/header', $data);
        $this->load->view('admin/createUser', $data);
        $this->load->view('admin/assets/footer');
    }
    

    在你的情况下,可能是

    is_unique[table.field]