【问题标题】:What generates the .holder-required class in SilverStripe forms在 SilverStripe 表单中生成 .holder-required 类的原因
【发布时间】:2023-04-02 01:56:01
【问题描述】:

我正在 SilverStripe 中构建一个联系表单。

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

我已经搜索了整个项目文件夹,但没有一个文件甚至包含 holder-required

.holder-required 类从何而来?

【问题讨论】:

    标签: php forms validation silverstripe


    【解决方案1】:

    您找不到holder-required 的原因是因为它在技术上不存在于 SilverStripe 代码库中,它实际上是一个由两个字符串连接在一起的类。

    FormField 中,有一个名为"extraClass" which adds these classes to the field 的函数。

    下面是FormField类的代码sn-p:

    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(),该函数在表单字段上设置消息属性。

    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;
    }
    

    我对@9​​87654323@ 进行了更多浏览,它应该在呈现表单后清除错误。表单类有两个函数部分,clearMessageresetValidation

    通过forTemplate渲染表单模板时调用函数clearMessage。我没有看到在 SilverStripe CMS 或框架代码库中使用了 resetValidation 函数。

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

    【讨论】:

    • 非常感谢!!!你知道为什么即使我重新加载页面或清除缓存也不会清除 $Message 变量吗?这会在表单中保留错误消息和 .holder-required ,除非我清除缓存并重新打开浏览器。
    • 我已经扩展了我的答案,基本上消息是从会话中设置的。虽然它旨在清除自身,但您可能需要清除会话本身(我的回答涵盖如何),或者您可能需要清除自己的 cookie。这可能是您特定 SS 版本中的错误,您使用的是最新的 SS 3.1 版本(3.1.14)吗?
    • 感谢@Turnerj 的额外回答!抱歉,今天才看到你的回复。是的,我正在使用最新的 SS 3.1 版本。我发现只有在删除网站的 cookie 并刷新后,这些消息才会消失。我改用了validation.js,并忽略了所有.holder 所需的类......但这不是一个完美的解决方案,因为.holder 所需的类仍然存在。有时间我会试试 resetValidation 函数。
    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 2012-07-18
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 2011-12-02
    相关资源
    最近更新 更多