将自定义错误消息添加到 CI validation_errors()


Add custom error messages to CI validation_errors()

有一种方法可以将自定义错误消息添加到CodeIgniter validation_errors();

例如,如果我想要一个具有123456值的字段,并且用户输入12345我想设置一条消息说:

数字 6 是必需的!

以及我可能想要添加的任何其他自定义规则。比如特定的模式或任何其他东西。

对不起我的英语。

是的,这是可能的。

使用回调设置规则,例如,

$this->form_validation->set_rules('field_name', 'Number', 'callback_custom_validation');

并在同一控制器中定义回调,例如,

public function custom_validation($str)
{
    if ($str != '123456')
    {
        $this->form_validation->set_message('field_name', 'The %s field requires 123456');
        return false;
    }
    else
    {
        return true;
    }
}

在视图中显示错误<?php echo form_error('field_name')?>

有关回调的更多信息,请单击此处。