Php 当总是返回我无法访问相应的错误消息时,我的代码发生了什么?

Php 当总是返回我无法访问相应的错误消息时,我的代码发生了什么?,php,codeigniter-3,Php,Codeigniter 3,我试图为手机创建自己的自定义回调函数,但当我想将其调用到$this->form_validation->set_rules('phone'、'phone'、'required | callback_phone')然后始终返回我“无法访问与您的字段名phone对应的错误消息。(phone)我不知道为什么如果我都很好,对此有什么想法吗 $this->form_validation->set_rules('first_name', 'first_name', 'trim|alpha|req

我试图为手机创建自己的自定义回调函数,但当我想将其调用到
$this->form_validation->set_rules('phone'、'phone'、'required | callback_phone')
然后始终返回我
“无法访问与您的字段名phone对应的错误消息。(phone)
我不知道为什么如果我都很好,对此有什么想法吗

$this->form_validation->set_rules('first_name', 'first_name', 'trim|alpha|required');
        $this->form_validation->set_rules('last_name', 'last_name', 'trim|alpha|required');
        $this->form_validation->set_rules('email', 'email', 'trim|valid_email|required');
        $this->form_validation->set_rules('phone', 'phone', 'required|callback_phone');
        $this->form_validation->set_rules('address', 'address', 'trim|alpha_numeric_spaces|required');

        if ($this->form_validation->run() == FALSE) {
            $this->json($this->form_validation->error_array());
        } else {
            if (empty($data)) {
                $this->json(array('msg' => 'vacio'));
            } else {
                if (!$this->providers->isExistsProvider($data)) {
                    $this->providers->addProvider($data);
                    $this->json(array('msg' => 'successfully added'));
                } else {
                    $this->json(array('msg' => 'The email is used by another provider'));
                }
            }
        }
回调函数

public function phone($phone) { 
        if ( preg_match( '/^[+]?([\d]{0,3})?[\(\.\-\s]?([\d]{3})[\)\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$/', $phone ) ) { 
            return TRUE; 
        } else {
            return FALSE; 
        } 
    }
html表单

<form class="form-horizontal" id="provider">
    <div class="form-group row">
        <div class="col-md-6">
            <label for="wprice" class="">First Name: </label>
            <input type="text" name="first_name" id="first_name" class="form-control input-sm" placeholder="First Name" title="First Name" required>
        </div>
        <div class="col-md-6">
            <label for="cost_price" class="">Last Name: </label>
            <input type="text" name="last_name" id="last_name" class="form-control input-sm" placeholder="Last Name" title="Last Name" required>
        </div>
    </div>
    <div class="form-group">
        <label for="description" class="col-sm-2 control-label">Email: </label>
        <div class="col-sm-10">
            <input type="email" name="email" id="email" class="form-control input-sm" placeholder="Email" title="Invalid Email" required>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-md-6">
            <label for="wprice">Phone: </label>
            <input type="text" name="phone" id="phone" class="form-control input-sm" placeholder="Phone" title="Phone" required>
        </div>
        <div class="col-md-6">
            <label for="cost_price">Address: </label>
            <input type="text" name="address" id="address" class="form-control input-sm" placeholder="Address" title="Address" required>
        </div>
    </div>
</form>

名字:
姓氏:
电邮:
电话:
地址:

因为您添加了自定义规则,所以必须为自定义规则添加消息。您可以使用表单验证库的
set\u message
功能添加消息。另一种方法是在
set\u rules
中定义消息。见文件

$this->form_validation->set_rules('first_name', 'first_name', 'trim|alpha|required');
$this->form_validation->set_rules('last_name', 'last_name', 'trim|alpha|required');
$this->form_validation->set_rules('email', 'email', 'trim|valid_email|required');
$this->form_validation->set_rules('phone', 'phone', 'required|callback_phone');
$this->form_validation->set_rules('address', 'address', 'trim|alpha_numeric_spaces|required');

$this->form_validation->set_message('phone', 'Phone number is invalid!');

if ($this->form_validation->run() == FALSE) {
    $this->json($this->form_validation->error_array());
} else {
    if (empty($data)) {
        $this->json(array('msg' => 'vacio'));
    } else {
        if (!$this->providers->isExistsProvider($data)) {
            $this->providers->addProvider($data);
            $this->json(array('msg' => 'successfully added'));
        } else {
            $this->json(array('msg' => 'The email is used by another provider'));
        }
    }
}

事情不是这样的。我必须将消息设置为字段,而不是回调。我更新了答案。在没有回拨的情况下尝试它。\ucode>$this->form\u validation->set\u message('phone','phone number is invalid!')@AronImperial