如何显示输入字段旁边的每个错误,或者只显示所有字段的一个错误(例如"所有字段都需要"),如下所示?我更喜欢输入旁边的每个错误。
查看:我正在使用form_open和更高版本的form_input。 form_eror没有工作。
// INPUT FORM FOR PRODUCT INSERT
// Form open
echo form_open('insert_product',array('id' =>'form'));
//all form inputs
// Name
echo form_label('Name ','name');
$name = array(
'name' => 'productName',
'id' => 'productName',
'placeholder' => 'Enter product name',
);
echo form_input($name, set_value('name'));
echo form_error('name');
数组中的控制器验证规则:
// Define validation rules for form inputs
$validation_rules = array(
//Name
array(
'field' => 'productName',
'label' => 'Product name',
'rules' => 'required|min_length[3]'
),
验证检查控制器:
//Check validation
if ( $this->form_validation->run() == true ) {
//Preparing input data, keys of array are the same as fields in database table
$data = array(
'Name' => $this->input->post('productName'),
'Description' => $this->input->post('productDescription'),
'Production_date' => date('Y-m-d', strtotime( $this->input->post('productProduction_date' ))),
'Expiry_date' => date('Y-m-d', strtotime( $this->input->post('productExpiry_date' ))),
'Price' => $this->input->post('productPrice'),
'Currency' => $this->input->post('productCurrency'),
'EAN_code' => $this->input->post('productEAN_code'),
'Weight' => $this->input->post('productWeight'),
'Weight_unit' => $this->input->post('productWeight_unit')
);
//Transfer data to model and get message from model
$row = $this->ProductModel->insert_product($data);
//get answer
if ($row) {
echo "Product successfully added to database";
}else {
echo 'There was an error inserting data in database '.json_encode($row);
}
}
else {
echo validation_errors();
}