在模型用户中,我有这个字段。
$this->addField('login')->mandatory(true)->caption('Login')->length(10);
但是当我输入超过 10 个字符的文本时,验证永远不会发生,并且永远不会在字段下方显示红色消息:“文本太长”
如何在敏捷工具包 4.2.4 中做到这一点?我想念一些基本的东西???
谢谢指教!!
在模型用户中,我有这个字段。
$this->addField('login')->mandatory(true)->caption('Login')->length(10);
但是当我输入超过 10 个字符的文本时,验证永远不会发生,并且永远不会在字段下方显示红色消息:“文本太长”
如何在敏捷工具包 4.2.4 中做到这一点?我想念一些基本的东西???
谢谢指教!!
正如 Field 类 length() 方法描述中所说:
This will provide a HTML settings on a field for maximum field size.
The length of a field will not be enforced by this setting.
...
Field->length($n) 本身不进行任何验证。它只是用于表单字段显示目的,您也可以在自己的验证类中的某处使用此值,如下所示:
// In model class file init method
$model->addHook('beforeSave', array($this, 'customValidation'));
// In model class file
function customValidation() {
foreach ($this->getActualFields() as $f) {
$field = $this->getField($f);
if ($field->length && strlen($this[$f]) > $field->length) {
throw $this->exception('Field value to long', 'Exception_ValidityCheck')
->addMoreInfo('field', $f)
->addMoreInfo('value', $this[$f])
->addMoreInfo('limit', $field->length);
}
}
}
上面的代码完全未经测试 - 只是为了给你一个想法。您还可以在表单提交挂钩上验证字段值长度,但这并不完全正确。更好的是在模型中执行此操作。
另请查看 Romans https://github.com/romaninsh/validation制作的验证插件。它必须非常强大。