【问题标题】:Silverstripe: Custom Form Template - Validation IssueSilverstripe:自定义表单模板 - 验证问题
【发布时间】:2016-08-18 22:16:08
【问题描述】:

我在 Silverstripe 中创建了一个前端表单。它在记录数据的意义上起作用,如果有任何错误,它会重定向回来并且不保存数据。但是我面临的问题是验证消息显示不正确。当我使用$Form 变量在前端显示表单时,验证和一切正常。我想要做的是使用<% control Form %> 控制表单的布局。这是因为表单的设计方式。

这是我的代码:

(模板.ss)

<% control Form %>
    <form class="wrap" $FormAttributes>
        <% if $Message %>
            <p id="{$FormName}_error" class="message $MessageType">$Message</p>
        <% else %>
            <p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
            <% end_if %>
            <fieldset>
                <div class="member-details col lg-mobile-12 tablet-6 sm-desktop-6 md-desktop-6">
                    <% if ModTest == 'false' %>
                        <div class="field wrap">
                            <% control $Fields.dataFieldByName(ClientName) %>
                                <label class="title">$Title</label>$Field
                                    <% end_control %>
                                </div>
                            <% end_if %>
                            <div class="field wrap">
                                <% control $Fields.dataFieldByName(FirstName) %>
                                    <label class="title">$Title</label>$Field
                                <% end_control %>
                            </div>
                            <div class="field wrap">
                                <% control $Fields.dataFieldByName(Surname) %>
                                    <label class="title">$Title</label>$Field
                                <% end_control %>
                            </div>
                            <% if $Fields.dataFieldByName(Address) %>
                                <div class="field address wrap">
                                    <% control $Fields.dataFieldByName(Address) %>
                                        <label class="title">$Title</label>
                                    <% end_control %>
                                    <div class="address-fields wrap">
                                        $Fields.dataFieldByName(Address)
                                        $Fields.dataFieldByName(Suburb)
                                        $Fields.dataFieldByName(State)
                                        $Fields.dataFieldByName(PostCode)
                                    </div>
                                </div>
                            <% end_if %>

                            <% control $Fields.dataFieldByName(Phone) %>
                                <div class="field wrap">
                                        <label class="title">$Title</label>$Field
                                </div>
                            <% end_control %>

                            <% control $Fields.dataFieldByName(Email) %>
                                <div id="$HolderID" class="field wrap <% if $extraClass %> $extraClass<% end_if %>">
                                    <label class="title" for="$ID">$Title</label>
                                    $Field
                                    <% if $Message %><span class="message $MessageType">$Message</span><% end_if %>
                                </div>
                            <% end_control %>

                        </div>
                        <div class="password col lg-mobile-12 tablet-6 sm-desktop-6 md-desktop-6">
                            <div class="field confirmedpassword">
                                $Fields.dataFieldByName(Password)                   
                            </div>
                        </div>
                        $Fields.dataFieldByName(SecurityID)
                    </fieldset>
                    <div class="col lg-mobile-12 tablet-12 sm-desktop-12 md-desktop-12">
                        <% if $Actions %>
                        <div class="Actions">
                            <% loop $Actions %>
                                $Field
                            <% end_loop %>
                        </div>
                        <% end_if %>
                    </div>
                </form>
            <% end_control %>

【问题讨论】:

    标签: php templates content-management-system silverstripe


    【解决方案1】:

    在模板中编写所有表单标记——就像你所做的那样——几乎违背了表单呈现和模板的目的。它也非常不灵活,因为您必须在模板中添加新添加的字段,而不仅仅是将它们添加到表单中。

    就个人而言,我会使用CompositeField 对您的字段进行分组(例如,包含多个字段的字段)并在需要自定义模板的字段上使用setTemplate

    我认为您应该能够使用自定义模板和复合字段实现与您的模板非常相似的输出。这样您就可以将$Form 放到您的模板中并开心...

    【讨论】:

      【解决方案2】:

      要获取每个字段级别的消息,您需要使用每个字段对象。

      <% with $Fields.dataFieldByName(Name) %>
          <div>
            <label>Name</label>
            <input name="Name" type="text" id="name" placeholder="Name" required>
            <% if $Message %><span>$Message</span><% end_if %>
          </div>
      <% end_with %>
      

      在 with 中添加 $Debug 将列出该字段中可用的内容,这对于添加 css id 和其他 CMS 控制的文本很有用。

      【讨论】:

      • 感谢您的回复。当我这样做时(请参阅上面的电子邮件字段),当出现错误时会输出消息。但是,当我修复错误并保存表单时,数据会保存但错误消息仍然存在并且不会消失......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多