php 中required,php – 什么在SilverStripe表单中生成.holder-required类

我正在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值