form_error数组不起作用

时间:2018-01-23 06:36:28

标签: php forms codeigniter validation

我正在我的表单中进行错误验证,但由于某些原因在每个输入框上验证并在其下显示它不起作用!但是对于validation_errors()它确实有效。但我想更多地使用form_error。你能救我吗?

这是我的set_rules放在我的控制器上:

$this->form_validation->set_rules("sections", "Section", "trim|required");              
$this->form_validation->set_rules('grade_name', 'Grade name', 'trim|required');         
$this->form_validation->set_error_delimiters('<div style="color:red;">', '</div>');

这就是我在观看时的方式:

$grade_name = array(
    'name'          => 'grade_name',
    'id'            => 'grade_name',
    'value'         => set_value('grade_name'),
    'maxlength'     => '100',
    'size'          => '50',
    'style'         => 'width:100%',
    'class'         => 'form-control m-input m-input--air',          
);

echo form_label('Grade:', 'grade_name');
echo form_input($grade_name);
echo form_error($grade_name);   

它不工作! :(请帮忙......

2 个答案:

答案 0 :(得分:0)

form_error()中你传递一个数组。只需添加输入名称

即可
echo form_error('grade_name');  

'name' => 'grade_name',名称字段应该来到那里

并设置字段type

$grade_name = array(
    'name'          => 'grade_name',
    'type'          => 'text', # add this
    'id'            => 'grade_name',
    'value'         => 'grade_name', # remove set_value() from here
    'maxlength'     => '100',
    'size'          => '50',
    'style'         => 'width:100%',
    'class'         => 'form-control m-input m-input--air',          
);

详细了解Showing Errors Individually in CodeIgniter.com

答案 1 :(得分:0)

您可以针对您的问题尝试此解决方案:

<强> Controller.php这样

<?php 
public function add() {
    $this->template->load('view_file_name');
}
public function save() {
        $data = array();
        $this->load->library('form_validation');
        $this->form_validation->set_rules("sections", "Section", "trim|required");              
        $this->form_validation->set_rules('grade_name', 'Grade name', 'trim|required');         
        $this->form_validation->set_error_delimiters('<div style="color:red;">', '</div>');
        if($this->form_validation->run()) {
            $this->db->trans_begin();
            // Add Here your save code or other stuff
            if ($this->db->trans_status() === FALSE) {
                flash_data(array('message_type'=>'error','message'=>lang('error')));
                $this->db->trans_rollback();
            } else {
                flash_data(array('message_type'=>'success','message'=>lang('success')));
                $this->db->trans_commit();
                redirect('redirect_page');
            }            
        } else {
            $this->template->load('view_file_name',(!empty($data))?$data:NULL);
        }
    }
?>

<强> view_file_name.php

<?php 
    echo form_open('controller/save',array('id'=>'add_form'));
    $grade_name = array(
        'name'          => 'grade_name',
        'id'            => 'grade_name',
        'value'         => set_value('grade_name'),
        'maxlength'     => '100',
        'size'          => '50',
        'style'         => 'width:100%',
        'class'         => 'form-control m-input m-input--air',          
    );

    echo form_label('Grade:', 'grade_name');
    echo form_input($grade_name);
    echo form_error('grade_name');  

    echo form_button('add',lang('save')); 

    echo form_close(); 
?>

我希望它会有所帮助。