Codeigniter 3:将“field is required”替换为“field not empty”

时间:2018-04-28 06:05:10

标签: codeigniter validation validationrules

我正在使用Codeigniter 3.1.8中的基本博客应用程序

我有创建帖子表单和更新帖子表单。他们都有验证规则。对于更新表单的无效字段“警告”,我想用“字段不能为空”替换表达式“field is required”。

这是代码(帖子控制器):

public function edit($id) {
    $data = $this->Static_model->get_static_data();
    $data['post'] = $this->Posts_model->get_post($id);
    $data['tagline'] = 'Edit the post "' . $data['post']->title . '"';
    $this->load->view('partials/header', $data);
    $this->load->view('edit');
    $this->load->view('partials/footer');
}

public function update() {
    // Form data validation rules
    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('desc', 'Short description', 'required');
    $this->form_validation->set_rules('body', 'Body', 'required');
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>');

    $id = $this->input->post('id');
    if ($this->form_validation->run()) {
        $this->Posts_model->update_post($id, $data);
        redirect('posts/post/' . $id);
    } else {
        $this->edit($id);
    }
}

如果标题字段为空,我希望警告为:“标题字段不能为空。”

我应该在更新方法中添加/更改什么?

2 个答案:

答案 0 :(得分:1)

你可以这样做:

在您的更新方法设置消息中,如下所示:

$this->form_validation->set_rules('title', 'Title', 'required',
                        array('required' => 'The Title field can not be empty')
                );
$this->form_validation->set_rules('desc', 'Short description', 'required',
                        array('required' => 'Short description can not be empty')
               );

/* use same for other fields*/

了解更多:for example this website

答案 1 :(得分:0)

我设法以这种方式获得所需的无效字段警告:

String selected[] = request.getParameter("item").split(":");
String selectedValue = selected[0];
String selectedText = selected[1];