【问题标题】:set_message in codeigniter doesn't workcodeigniter 中的 set_message 不起作用
【发布时间】: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 字段。是的,我做到了!除了这个之外,它在任何地方都可以使用!
  • 我可以看到更多phphtml 我这里没有太多事情要做

标签: php validation codeigniter-2 codeigniter


【解决方案1】:

根据CI user guide,您应该将rule作为第一个参数传递给set_message()方法,不是输入名称:

$this->form_validation->set_message('rule', 'Error Message');

在你的情况下,应该是:

$this->form_validation->set_message('required', 'برای ایجاد یک حساب کاربری، باید قوانین سایت را مطالعه کرده و موافقت خود را با آن اعلام نمایید.');

但我只想为接受字段设置消息。

那么你需要使用一个callback function作为验证规则,如下:

$this->form_validation->set_rules('accept', 'Accept', 'callback_accept_check');

然后在 Controller 中添加一个名为 accept_check 的方法。例如:

public function accept_check($str)
{
    if ($str == '') {
        $this->form_validation->set_message('accept_check', 'برای ایجاد یک حساب کاربری، باید قوانین سایت را مطالعه کرده و موافقت خود را با آن اعلام نمایید.');
        return FALSE;
    } else {
        return TRUE;
    }
}

【讨论】:

  • 但我只想为接受字段设置消息。此代码将更改所有必需的验证消息
  • @Hashem Qolami 我也试过这个解决方案,但它不起作用:$this->form_validation->set_rules('accept', 'قوانین', 'callback_accept_check'); function callback_accept_check ($str) { if(!isset($str) OR empty($str)) { $this->form_validation->set_message('accept_check', 'برای ایجاد یک حساب کاربری، باید قوانین سایت را مطالعه کرده و موافقت خود را با آن اعلام نمایید.'); return false; } }
  • 你能扩展it doesn't work吗?您是否按照用户指南中准备的示例进行操作?你有什么错误吗?
  • 您能否通过添加您迄今为止尝试过的当前代码来更新问题?我的意思是来自控制器的相关代码。
  • :( 问题是表单的其他部分工作正常,没有出现任何错误。
猜你喜欢
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多