Daroath Daroath - 4 months ago 50
PHP Question

codeigniter rules group form validation

As the Codeigniter document said that we can create sets of rules inside form_validation.php config file. I think I have followed the instruction but the problem is that it shows empty error message rather than errors message that set inside the config arrays.
My form_validation.php config file

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


And here is my users controller


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


And here is the error message show up

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


Any help please appreciated!

Answer

I have done something similar to this, but I'm not using class/method name association in the config file. I have done like below:

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

Controller Class

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

NOTE: In autoload.php, I have added $autoload['libraries'] = array('form_validation'); to load form_validation config.

Hope this will works for you as well. Let me know if you facing any other issues.

Comments