foreach($all_fields as $row){
$datatype = $this->sys_model->get_table_data_by_datatype_id('datatype',$row['datatype_id']);
$msg = $this->sys_model->get_validation_message_by_datatype_id('validation_message',$row['datatype_id']);
if($datatype[0]['datatype'] == 'email'){
$this->form_validation->set_rules($row['field_name'],$row['field_name'], 'required|trim|xss_clean|valid_email');
$this->form_validation->set_message('required',$msg[0]['validation_msg']);
}
elseif($datatype[0]['datatype'] == 'number'){
$this->form_validation->set_rules($row['field_name'],$row['field_name'], 'required|numeric');
$this->form_validation->set_message('required',$msg[0]['validation_msg']);
}else{
$this->form_validation->set_rules($row['field_name'],$row['field_name'], 'required');
$this->form_validation->set_message('required',$msg[0]['validation_msg']);
}
}
我想根据数据类型从后端动态设置验证消息,但它对所有字段都采用相同的消息。 如何根据数据类型为所有字段设置不同的消息?
答案 0 :(得分:0)
为每种数据类型
创建一个回调函数function validate_string($str)
{
if(condition) )
{
return true;
}
else
{
$this->form_validation->set_message('validate_string', 'The %s your custom error message');
return false;
}
}
$this->form_validation->set_rules('field1', 'label1', 'validate_string');
$this->form_validation->set_rules('field2', 'label2', 'validate_string');
$this->form_validation->set_rules('field3', 'label3', 'validate_string');
$this->form_validation->set_rules('field4', 'label4', 'validate_string');