我对form_validation消息名称字段访问有什么问题?

时间:2017-08-03 00:32:50

标签: codeigniter

我想尝试使用我的确认密码验证确认密码匹配,但它不会显示我的错误,但是当我尝试设置我的用户名的消息然后告诉我它不能被它访问什么是我做错了?但我尝试更改用户名中的名称,但它不起作用。

new_user controller

public function new_user(){
   $first_name = $this->security->xss_clean(strip_tags($this->input->post('first_name')));
    $last_name = $this->security->xss_clean(strip_tags($this->input->post('last_name')));
    $profile_id = $this->security->xss_clean(strip_tags($this->input->post('profile_id')));
    $password = $this->security->xss_clean(strip_tags($this->input->post('password')));
    $username = $this->security->xss_clean(strip_tags($this->input->post('username')));
                    $data = array(
                        'first_name' => $first_name,
                        'last_name' => $last_name,
                        'profile_id' => $profile_id,
                        'password' => password_hash($password, PASSWORD_BCRYPT),
                        'username' => $username,
                    );

                    $this->form_validation->set_rules('first_name','first_name','trim|required');
                    $this->form_validation->set_rules('last_name','last_name','trim|required');
                    $this->form_validation->set_rules('username','username','trim|required|');
                    $this->form_validation->set_rules('password', 'password', 'trim|required');
                    $this->form_validation->set_rules('conf_password', 'conf_password', 'matches[password]|required');

                    $this->form_validation->set_message('first_name', 'Please enter First Name');
                    $this->form_validation->set_message('last_name', 'Please enter Last Name');
                    $this->form_validation->set_message('username', 'Please enter Username'); 
                    $this->form_validation->set_message('password', 'Please enter Password'); 

                    if ($this->form_validation->run() == FALSE) {
                      $this->json($this->form_validation->error_array());
                    }else{
                      $this->user->signup($data); 
                    }
                }

表格

<form id="signup" method="post" onsubmit="return false;">
                        <h2 class="sr-only">New User</h2>
                        <div class="illustration">
                            <i class="icon ion-ios-locked-outline" style="color: rgb(251, 244, 244);"></i>
                        </div>
                        <div class="form-group">
                            <input type="text" name="first_name" id="first_name" placeholder="First Name" class="form-control"
                            required="required" />
                        </div>
                        <div class="form-group">
                            <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="form-control"
                            required="required" />
                        </div>
                        <div class="form-group">
                            <select name="profile_id" id="profile_id" class="form-control" required="required">
                                <option value="">Select a Profile</option>
                                <?php foreach ($profile as $value): ?>
                                    <option value="<?php echo $value['id'];?>">
                                        <?php echo $value['profile']; ?>
                                    </option>
                                <?php endforeach ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <input type="text" name="username" id="username" placeholder="username" class="form-control"
                            required="required" />
                        </div>
                        <div class="form-group">
                            <input type="password" name="password" id="password" placeholder="***********" class="form-control"
                            required="required" />
                        </div>
                        <div class="form-group">
                            <input type="password" name="conf_password" id="conf_password" placeholder="***********" class="form-control"
                            required="required" />
                        </div>
                        <div class="form-group">
                            <button class="btn btn-primary btn-block" type="submit" style="background-color: rgb(41, 54, 78);">Ingresar</button>
                        </div>
                    </form>

输出错误

{username: "Unable to access an error message corresponding to your field name username.()"}
username
:
"Unable to access an error message corresponding to your field name username.()"

1 个答案:

答案 0 :(得分:1)

我认为你应该这样做 替换此

$this->form_validation->set_rules('username','username','trim|required|');

要:

$this->form_validation->set_rules('username','username','trim|required');