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

Dim*_*Sum 4 php forms validation silverstripe

我正在SilverStripe中建立联系表单.

测试验证时,如果我将所需字段留空并点击提交,那么这些输入字段将添加一个.holder-required类.即使我重新加载页面,它们也不会消失.(实际上错误消息*** is required将在重新加载后保留在那里.我只是停止显示的消息).

我搜索了整个项目文件夹,但是里面没有文件holder-required.

.holder-required班级来自哪里?

Tur*_*erj 5

你无法找到的原因holder-required是因为它在SilverStripe代码库中技术上不存在,它实际上是一个从两个字符串连接在一起的类.

在a中FormField,有一个名为"extraClass"的函数,它将这些类添加到字段中.

以下是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);
}
Run Code Online (Sandbox Code Playgroud)

这告诉我们的是,对于为字段显示的消息,它将holder-{Whatever_Your_Message_Type_Is}作为额外的类附加.


$this->Message()在页面重新加载之后仍然设置的原因是错误信息实际上保存到该表单的会话中.

下面是该类的一个片段,Form它调用了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;
}
Run Code Online (Sandbox Code Playgroud)

我已经对Form代码进行了一些浏览,它应该在渲染表单后清除错误.表单类有两个函数部分,clearMessageresetValidation.

通过clearMessage渲染表单模板时调用该函数forTemplate.我没有resetValidation在SilverStripe CMS或Framework代码库中看到该函数的任何用法.

如果在您的情况下消息未清除,您可能需要在代码中调用其中一个.