当我总是让我无法访问相应的错误消息时,我的代码是什么?

时间:2017-06-25 05:53:00

标签: php codeigniter-3

我正在尝试为手机制作自己的自定义回调功能,但是当我想将其调用到$this->form_validation->set_rules('phone', 'phone', 'required|callback_phone');然后总是回复我'Unable to access an error message corresponding to your field name 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'));
                }
            }
        }

callback_function

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>

1 个答案:

答案 0 :(得分:0)

由于您添加了自定义规则,因此您必须为自定义规则添加消息。您可以使用表单验证库的set_message功能添加消息。另一种方法是在set_rules中定义消息。请参阅Setting Error Messages文档。

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