问题描述:

 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']);

}

}

i want to set validation message dynamically from backend based on datatype but it takes same message for all fields..

How can i set different message for all fields based on datatype ?

网友答案:

create a callback functions for each datatype

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');
相关阅读:
Top