我正在SilverStripe中建立联系表单.
测试验证时,如果我将所需字段留空并点击提交,那么这些输入字段将添加一个.holder-required类.即使我重新加载页面,它们也不会消失. (实际上,错误消息***是必需的,在重新加载后也会保留在那里.我只是停止了显示的消息).
我搜索了整个项目文件夹,但是没有文件甚至包含持有者所需的文件.
.holder-required类来自哪里?
解决方法:
你无法找到holder-required的原因是因为它在SilverStripe代码库中技术上不存在,它实际上是一个从两个字符串连接在一起的类.
下面是FormField类的代码片段:
public function extraClass() {
$classes = array();
$classes[] = $this->Type();
if($this->extraClasses) {
$classes = array_merge(
$classes,
array_values($this->extraClasses)
);
}
if(!$this->Title()) {
$classes[] = 'nolabel';
}
// Allow custom styling of any element in the container based on validation errors,
// e.g. red borders on input tags.
//
// CSS class needs to be different from the one rendered through {@link FieldHolder()}.
if($this->Message()) {
$classes[] .= 'holder-' . $this->MessageType();
}
return implode(' ', $classes);
}
这告诉我们的是,对于为字段显示的消息,它会将holder- {Whatever_Your_Message_Type_Is}附加为额外的类.
在页面重新加载后仍然设置$this-> Message()的原因是错误信息实际上已保存到该表单的会话中.
下面是a snippet from the Form class,它调用FormField :: setError(),该函数在表单字段上设置message属性.
public function setupFormErrors() {
$errorInfo = Session::get("FormInfo.{$this->FormName()}");
if(isset($errorInfo['errors']) && is_array($errorInfo['errors'])) {
foreach($errorInfo['errors'] as $error) {
$field = $this->fields->dataFieldByName($error['fieldName']);
if(!$field) {
$errorInfo['message'] = $error['message'];
$errorInfo['type'] = $error['messageType'];
} else {
$field->setError($error['message'], $error['messageType']);
}
}
// load data in from previous submission upon error
if(isset($errorInfo['data'])) $this->loadDataFrom($errorInfo['data']);
}
if(isset($errorInfo['message']) && isset($errorInfo['type'])) {
$this->setMessage($errorInfo['message'], $errorInfo['type']);
}
return $this;
}
我已经对Form code进行了更多的浏览,它应该在渲染表单后清除错误.表单类有两个函数部分,clearMessage和resetValidation.
通过forTemplate呈现表单模板时,将调用函数clearMessage.我没有看到在SilverStripe CMS或Framework代码库中使用resetValidation函数.
如果在您的情况下消息未清除,您可能需要在代码中调用其中一个.
标签:php,forms,validation,silverstripe
来源: https://codeday.me/bug/20190609/1203211.html