2016-08-18 10 views
1

Silverstripeでフロントエンドフォームを作成しました。データが記録され、エラーがあれば、データはリダイレクトされ、データは保存されません。しかし、私が直面している問題は、検証メッセージが正しく表示されていないことです。変数$Formを使用してフロントエンドにフォームを表示すると、検証とすべて正常に動作します。私がしたいのは、<% control Form %>を使ってフォームのレイアウトを制御することです。これは、フォームがどのように設計されているかによるものです。あなたがフィールドオブジェクトのそれぞれに作業する必要があるごとのフィールドレベルでメッセージを取得するにはSilverstripe:カスタムフォームテンプレート - 検証問題

(Template.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 %> 

答えて

1

テンプレートにすべてのフォームマークアップを書くことは、あなたが行ったように、フォームレンダリングとテンプレートの目的をはるかに凌駕します。新しく追加されたフィールドをフォームに追加するのではなく、テンプレート内に追加する必要があるため、柔軟性に欠けます。

個人的には、フィールドをグループ化するにはCompositeFieldを使用し、カスタムテンプレートが必要なフィールドにはsetTemplateを使用します。

私はあなたがカスタムテンプレートと複合フィールドを持つテンプレートにかなり似た出力を達成できるはずだと思います。だからあなたはちょうどあなたのテンプレートに$Formを落として幸せになることができます...

+0

ありがとうございます。私はそれを調べます。 – Dallby

1

:ここ

は私のコードです。内部$Debugを追加

<% 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 %> 

は、CSSのidのと他のCMSコントロールのテキストを追加するために有用である、フィールドに利用できるものを一覧表示されます。

+0

返信ありがとうございます。私がそれをすると(上記のメールフィールドを参照)、エラーが発生したときにメッセージが出力されます。しかし、私が間違いを修正してフォームを保存すると、データは保存されますが、エラーメッセージは消えることはありません。 – Dallby