【发布时间】:2014-03-18 22:16:48
【问题描述】:
我将更改 codeigniter 中名为 accept 的特定规则的验证消息。它是一个复选框。我根据 codeigniter 的文档尝试了这段代码:
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);
}
}
但它不起作用,它会显示所需验证的默认消息!即使我不能通过回调函数手动设置消息只是为了这个规则有什么问题?
【问题讨论】:
-
什么是
accept?你加载$this->load->library('form_validation');了吗? -
@Phil 在我看来它是一个 checkbox 字段。是的,我做到了!除了这个之外,它在任何地方都可以使用!
-
我可以看到更多
php和html我这里没有太多事情要做
标签: php validation codeigniter-2 codeigniter