Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php codeigniter规则组表单验证_Php_Codeigniter_Validation - Fatal编程技术网

Php codeigniter规则组表单验证

Php codeigniter规则组表单验证,php,codeigniter,validation,Php,Codeigniter,Validation,正如Codeigniter文档所说,我们可以在form_validation.php配置文件中创建规则集。我想我已经遵循了指令,但问题是它显示的是空的错误消息,而不是在配置数组中设置的错误消息。 我的form_validation.php配置文件 $config = array( 'users/register' => array( array( 'field' => 'user_type', 'label' =

正如Codeigniter文档所说,我们可以在form_validation.php配置文件中创建规则集。我想我已经遵循了指令,但问题是它显示的是空的错误消息,而不是在配置数组中设置的错误消息。 我的form_validation.php配置文件

$config = array(
    'users/register' => array(
        array(
            'field' => 'user_type',
            'label' => 'User Type',
            'rules' => 'required|in_list[2,3]',
            'errors' => array(
                'in_list' => '%s Accept only agents or owners!'
            )
        ),
        array(
            'field' => 'first_name',
            'label' => 'First Name',
            'rules' => 'trim|required|alpha_numeric_spaces',
            'errors' => array(
                'required' => 'Required field.',
                'alpha_numeric_spaces' => 'Only letters, number and space are allowed for %s.'
            )
        ),
        array(
            'field' => 'last_name',
            'label' => 'Last Name',
            'rules' => 'trim|required|alpha_numeric_spaces',
            'errors' => array(
                'required' => 'Fields with red asterisk is required!',
                'alpha_numeric_spaces' => 'Only letters, number and space are allowed for %s.'
            )
        ),
        array(
            'field' => 'sex',
            'label' => 'Gender',
            'rules' => 'trim|required|in_list[male,female]',
            'errors' => array(
                'in_list' => 'Optional, %s field must be male or female.'
            )
        ),
        array(
            'field' => 'password',
            'label' => 'Password',
            'rules' =>'trim|required|min_length[6]|max_length[25]',
            'errors' => array(
                'required' => 'Required field.',
                'min_length' => '%s must be between 6-25 characters long.',
                'max_length' => '%s must be between 6-25 characters long.'
            )
        ),
        array(
            'field' => 'confirm_password',
            'label' => 'Password confirmed',
            'rules' => 'trim|required|matches[password]',
            'errors' => array(
                'required' => 'Required field.',
                'matches' => '%s doesn\'t match with password field.'
            )
        ),              
        array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'trim|required|is_unique[users.email]|valid_email',
            'errors' => array(
                'required' => 'Required field.',
                'is_unique' => '%s is already taken.',
                'valid_email' => '%s must be valid. For example, johndoe@example.com'
            )
        )
    ),
    'users/update_address' => array(
        array(
            'field' => 'house_number',
            'label' => 'House Number',
            'rules' => 'trim|alpha_numeric_spaces',
            'errors' => array(
                'alpha_numeric_spaces' => 'Only letters, space and number are allowed for %s'
            )
        ),
        array(
            'field' => 'street_number',
            'label' => 'Street Number',
            'rules' => 'trim|alpha_numeric_spaces',
            'errors' => array(
                'alpha_numeric_spaces' => 'Only letters, space and number are allowed for %s'
            )
        ),
        array(
            'field' => 'city_id',
            'label' => 'City or Province',
            'rules' => 'trim|is_natural',
            'errors' => array(
                'is_natural' => 'Only number is allowed for %s'
            )
        ),
        array(
            'field' => 'district_id',
            'label' => 'District or Khan',
            'rules' => 'trim|is_natural',
            'errors' => array(
                'is_natural' => 'Only number is allowed for %s'
            )
        ),
        array(
            'field' => 'commune_id',
            'label' => 'Commune or sangkat',
            'rules' => 'trim|is_natural',
            'errors' => array(
                'is_natural' => 'Only number is allowed for %s'
            )
        )
    )
);
这是我的用户控制器
class Users extends AL_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper(['url','form']);
        $this->load->model('user');
        $this->load->model('ion_auth_model');
        $this->load->library('form_validation');
    }

public function update_address() {
        header('Content-Type: application/x-json; charset=utf-8');
        if(!$this->ion_auth->logged_in()) {
            redirect(base_url(). 'users/login', 'refresh');
        } else {
            if($this->form_validation->run('update_address') == FALSE) {
                $data['errors'] = validation_errors();
                echo json_encode(array('status' => 'ERROR', 'msg' => 'Your form contains error(s). Please fix it.', 'err' => $data));
            } else {
                $user_id = $this->ion_auth->get_user_id();
                if($this->user->edit_user_address($user_id)) {
                    echo json_encode(array('status' => 'OK', 'msg' => 'Your address has been updated!'));
                } else {
                    echo json_encode(array('status' => 'ERROR', 'msg' => 'Unable to update your address. Please refresh your page and try again.'));
                }
            }
        }
    }
}
下面是显示的错误消息

{"status":"ERROR","msg":"Your form contains error(s). Please fix it.","err":{"errors":""}}

application/config/form_validation.php

您已经在那里定义了表单验证规则

所以你必须加载

$this->config->load('form_validation');
并尝试按以下方式访问配置规则:

$this->form_validation->set_rules($this->form_validation->set_rules($this->config->item('users/update_address'));

看看这是否有助于我最终找到答案。Codeigniter文档没有明确说明类/方法名关联。当我从

$this->form_validation->run('update_address') == FALSE

但是,下面显示的消息很难确定哪个错误对应哪个字段。如果有更好的答案,我们将不胜感激

{"status":"ERROR","msg":"Your form contains error(s). Please fix it.","err":{"errors":"<p>Only letters, space and number are allowed for House Number<\/p>\n<p>Only letters, space and number are allowed for Street Number<\/p>\n"}}
{“status”:“ERROR”,“msg”:“您的表单包含错误。请修复它。”,“err”:{“errors”:“门牌号只允许字母、空格和数字\n街道号只允许字母、空格和数字\n”}

我做了类似的事情,但我没有在配置文件中使用类/方法名关联。我做了如下工作:

form_validation.php

$config = array(
    'register' => array(
        array(
            'field' => 'user_type',
            'label' => 'User Type',
            'rules' => 'required|in_list[2,3]',
            'errors' => array(
                'in_list' => '%s Accept only agents or owners!'
            )
        ),
        array(
            'field' => 'first_name',
            'label' => 'First Name',
            'rules' => 'trim|required|alpha_numeric_spaces',
            'errors' => array(
                'required' => 'Required field.',
                'alpha_numeric_spaces' => 'Only letters, number and space are allowed for %s.'
            )
        ),
        array(
            'field' => 'last_name',
            'label' => 'Last Name',
            'rules' => 'trim|required|alpha_numeric_spaces',
            'errors' => array(
                'required' => 'Fields with red asterisk is required!',
                'alpha_numeric_spaces' => 'Only letters, number and space are allowed for %s.'
            )
        ),
        array(
            'field' => 'sex',
            'label' => 'Gender',
            'rules' => 'trim|required|in_list[male,female]',
            'errors' => array(
                'in_list' => 'Optional, %s field must be male or female.'
            )
        ),
        array(
            'field' => 'password',
            'label' => 'Password',
            'rules' =>'trim|required|min_length[6]|max_length[25]',
            'errors' => array(
                'required' => 'Required field.',
                'min_length' => '%s must be between 6-25 characters long.',
                'max_length' => '%s must be between 6-25 characters long.'
            )
        ),
        array(
            'field' => 'confirm_password',
            'label' => 'Password confirmed',
            'rules' => 'trim|required|matches[password]',
            'errors' => array(
                'required' => 'Required field.',
                'matches' => '%s doesn\'t match with password field.'
            )
        ),              
        array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'trim|required|is_unique[users.email]|valid_email',
            'errors' => array(
                'required' => 'Required field.',
                'is_unique' => '%s is already taken.',
                'valid_email' => '%s must be valid. For example, johndoe@example.com'
            )
        )
    ),
    'update_address' => array(
        array(
            'field' => 'house_number',
            'label' => 'House Number',
            'rules' => 'trim|alpha_numeric_spaces',
            'errors' => array(
                'alpha_numeric_spaces' => 'Only letters, space and number are allowed for %s'
            )
        ),
        array(
            'field' => 'street_number',
            'label' => 'Street Number',
            'rules' => 'trim|alpha_numeric_spaces',
            'errors' => array(
                'alpha_numeric_spaces' => 'Only letters, space and number are allowed for %s'
            )
        ),
        array(
            'field' => 'city_id',
            'label' => 'City or Province',
            'rules' => 'trim|is_natural',
            'errors' => array(
                'is_natural' => 'Only number is allowed for %s'
            )
        ),
        array(
            'field' => 'district_id',
            'label' => 'District or Khan',
            'rules' => 'trim|is_natural',
            'errors' => array(
                'is_natural' => 'Only number is allowed for %s'
            )
        ),
        array(
            'field' => 'commune_id',
            'label' => 'Commune or sangkat',
            'rules' => 'trim|is_natural',
            'errors' => array(
                'is_natural' => 'Only number is allowed for %s'
            )
        )
    )
);
控制器类

class Users extends AL_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper(['url','form']);
        $this->load->model('user');
        $this->load->model('ion_auth_model');
        $this->load->library('form_validation');
    }

public function update_address() {
        header('Content-Type: application/x-json; charset=utf-8');
        if(!$this->ion_auth->logged_in()) {
            redirect(base_url(). 'users/login', 'refresh');
        } else {
            if($this->form_validation->run('update_address') == FALSE) {
                $data['errors'] = validation_errors();
                echo json_encode(array('status' => 'ERROR', 'msg' => 'Your form contains error(s). Please fix it.', 'err' => $data));
            } else {
                $user_id = $this->ion_auth->get_user_id();
                if($this->user->edit_user_address($user_id)) {
                    echo json_encode(array('status' => 'OK', 'msg' => 'Your address has been updated!'));
                } else {
                    echo json_encode(array('status' => 'ERROR', 'msg' => 'Unable to update your address. Please refresh your page and try again.'));
                }
            }
        }
    }
}
注意:
autoload.php
中,我添加了
$autoload['libraries']=array('form_validation')加载表单验证配置


希望这对你也有用。如果您面临任何其他问题,请告诉我。

尝试将var_dump($data)
放在
echo json_encode(数组('status'=>'ERROR','msg'=>'您的表单包含错误。请修复它。','err'=>$data))查看什么是结果数组(1){[“错误”]=>字符串(0)”}查看没有消息显示其为空Y确实仍然为空字符串。我认为这是Codeigniter bug?你已经设置了这个
$this->form\u valiation->set\u rule($config)不工作的家伙。项目('$config['users/update_address']);变量不能为单变量quote@Daroath键入
$this->form\u validation->set\u rules($this->config->item('users/update\u address'))查看此工作根据您的经验,我们是否可以显示如下错误消息{“状态”:“错误”,“消息”:“您的表单包含错误。请修复它。”,“错误”:{“错误”:{“门牌号”:“门牌号},{“街道号”只允许字母、空格和数字”:”街道编号“}}只允许使用字母、空格和数字在我的应用程序中,我以不同的方式处理它。我在一个变量中存储
validation\u errors
,然后我编写了一个方法,该方法接受该变量数据并循环遍历每个项目,并在模式弹出窗口中显示错误消息。根据您的示例,对于line
echo json\u encode(数组),您将获得哪些数据('status'=>'ERROR','msg'=>'您的表单包含错误。请修复它。','err'=>$data));
?您可以将它粘贴到这里吗?顺便说一句,
{“status”:“ERROR”,“msg”:“您的表单包含错误。请修复它。”,“err”:{“errors”:{“house\u number”:“house number},{“street\u number:”只允许字母、空格和数字“街道编号只允许字母、空格和数字”}}
是一个json文档。所有最终用户都没有理解它的经验。您必须为用户显示正确的验证消息。如果
json\u编码(数组('status'=>'ERROR','msg'=>'您的表单包含错误。请修复它。','err'=>$data))
line根据您的期望返回正确的数据,然后您能告诉我接下来需要什么吗?我想要特定字段的错误,例如,如果house_number字段不正确,则返回{“house_number”:“house number只允许字母、空格和数字}”类似这样的键值对将完全修复我的问题echo json_encode(数组('status'=>'ERROR','msg'=>'您的表单包含错误。请修复它','errors'=>$this->form_validation->ERROR_array());感谢@Deepak Biswal的帮助。按预期给我错误{“status”:“ERROR”,“msg”:“您的表单包含错误”。请修复它。“,”错误“:{“房屋号”:“房屋号只允许字母、空格和数字”,“街道号”:“街道号只允许字母、空格和数字”}
class Users extends AL_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper(['url','form']);
        $this->load->model('user');
        $this->load->model('ion_auth_model');
        $this->load->library('form_validation');
    }

public function update_address() {
        header('Content-Type: application/x-json; charset=utf-8');
        if(!$this->ion_auth->logged_in()) {
            redirect(base_url(). 'users/login', 'refresh');
        } else {
            if($this->form_validation->run('update_address') == FALSE) {
                $data['errors'] = validation_errors();
                echo json_encode(array('status' => 'ERROR', 'msg' => 'Your form contains error(s). Please fix it.', 'err' => $data));
            } else {
                $user_id = $this->ion_auth->get_user_id();
                if($this->user->edit_user_address($user_id)) {
                    echo json_encode(array('status' => 'OK', 'msg' => 'Your address has been updated!'));
                } else {
                    echo json_encode(array('status' => 'ERROR', 'msg' => 'Unable to update your address. Please refresh your page and try again.'));
                }
            }
        }
    }
}