i am going to change the validation message for a specific rule called accept in codeigniter. it is a checkbox. i tried this code according to codeigniter's documentation:
function register()
{
//validate form input
$this->form_validation->set_rules('sex', $this->lang->line('create_user_validation_sex_label'), 'required|trim|xss_clean');
$this->form_validation->set_rules('first_name', $this->lang->line('create_user_validation_fname_label'), 'required|trim|xss_clean');
$this->form_validation->set_rules('last_name', $this->lang->line('create_user_validation_lname_label'), 'required|trim|xss_clean');
$this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email|trim|is_unique[users.email]');
$this->form_validation->set_rules('phone', $this->lang->line('create_user_validation_phone_label'), 'required|trim|xss_clean');
$this->form_validation->set_rules('mob', $this->lang->line('create_user_validation_mob_label'), 'required|trim|xss_clean');
$this->form_validation->set_rules('address', $this->lang->line('create_user_validation_address_label'), 'required|trim|xss_clean');
$this->form_validation->set_rules('company', $this->lang->line('create_user_validation_company_label'), 'required|trim|xss_clean');
$this->form_validation->set_rules('agentcode', $this->lang->line('create_user_validation_agentcode_label'), 'required|trim|xss_clean', 'callback_ins');
function callback_ins ($ins)
{
if ($ins == 0) {
return false;
}
else {
return true;
}
}
$this->form_validation->set_rules('username', $this->lang->line('create_user_validation_username_label'), 'required|trim|xss_clean|is_unique[users.username]');
$this->form_validation->set_rules('password', $this->lang->line('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', $this->lang->line('create_user_validation_password_confirm_label'), 'required');
$this->form_validation->set_rules('accept', 'Accept', 'callback_accept_check');
function accept_check($str)
{
if ($str == '') {
$this->form_validation->set_message('accept_check', 'برای ایجاد یک حساب کاربری، باید قوانین سایت را مطالعه کرده و موافقت خود را با آن اعلام نمایید.');
return FALSE;
} else {
return TRUE;
}
}
if ($this->form_validation->run() == TRUE)
{
$username = strtolower($this->input->post('username'));
$email = strtolower($this->input->post('email'));
$password = $this->input->post('password');
$additional_data = array(
'sex' => $this->input->post('sex'),
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'phone' => $this->input->post('phone'),
'mob' => $this->input->post('mob'),
'address' => $this->input->post('address'),
'company' => $this->input->post('company'),
'agentcode' => $this->input->post('agentcode')
);
}
if ($this->form_validation->run() == true && $this->ion_auth->register($username, $password, $email, $additional_data))
{
//check to see if we are creating the user
//redirect them back to the admin page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect("main/users", 'refresh');
}
else
{
//display the create user form
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
$this->_render_page('registration', $this->data);
}
}
but it doesn't work and it shows the default message for required validation! even i cant set the message manually by callback function just for this rule whats the problem?
According to CI user guide, you should pass the rule
as the first parameter to the set_message()
method, NOT the input name:
$this->form_validation->set_message('rule', 'Error Message');
In your case, it should be:
$this->form_validation->set_message('required', 'برای ایجاد یک حساب کاربری، باید قوانین سایت را مطالعه کرده و موافقت خود را با آن اعلام نمایید.');
but just i want to set message for accept field.
Then you need to use a callback function as the validation rule, as follows:
$this->form_validation->set_rules('accept', 'Accept', 'callback_accept_check');
Then within the Controller add a method called accept_check
. For instance:
public function accept_check($str)
{
if ($str == '') {
$this->form_validation->set_message('accept_check', 'برای ایجاد یک حساب کاربری، باید قوانین سایت را مطالعه کرده و موافقت خود را با آن اعلام نمایید.');
return FALSE;
} else {
return TRUE;
}
}