在表单验证中设置 CodeIgniter "human name" 样式

Style CodeIgniter "human name" in form validation

我想将自定义通用样式应用于表单验证的 CodeIgniter set_rules 方法中的所有 "human names" class:

$this->form_validation->set_rules(<name of field>, <human name>, <rules>);

说我想把所有"human names"都写成粗体,我知道我能做到:

$this->form_validation->set_rules(<name of field>, <strong>human name</strong>, <rules>);

然而,一旦要设置样式的字段数量变大,这就很难管理了。

有没有办法使用类似 set_error_delimeters 的方式访问它?

$this->form_validation->set_error_delimeters('<div class="error">', '</div>');

我可以在 set_rules 方法中为此 "human name" 或第二个参数设置分隔符吗?

目前没有(至少 明确 讨论过)从 documentation.

做到这一点的方法

您可以更改set_rules form_validation 的功能 library.You 需要更改此功能的标签参数。只需查看此库中的 set_rules 函数即可。改变这一行 $label = ($label === '') ? $字段:$标签;

包围在 ? 之后的 $field 和 $label由你分隔。

您可以覆盖它或修改核心库函数或在验证库中创建新函数来设置人名分隔符。

您想更改人名的错误分隔符吗?

您希望在 extended form_validation class 中进行此更改,这样您就不必在所有验证中再次声明它 类?

如果两者都是,Chris has your back with this great solution!

class MY_Form_validation extends CI_Form_validation {

    public function __construct()
    {
        parent::__construct();

        $this->_error_prefix = '<p class="error">';
        $this->_error_suffix = '</p>';
    }
}

这样就不用了protected $CI magic thing所以我们这里很好