2017-03-16 383 views
1

我使用的是Tapestry 5.4.1,但也試用了5.4。我只想要一個帶有字段驗證的啓用Ajax的表單。基本上,我想要的是這樣的: http://jumpstart.doublenegative.com.au/jumpstart7/examples/ajax/form掛毯5.4,Ajax表單驗證不顯示字段錯誤

但是,我無法獲得基於字段的錯誤報告顯示何時啓用Ajax(沒有Ajax工作正常)。這意味着,我想要默認的行爲,其中輸入字段會得到一個紅色邊框,並在輸入值無效的情況下打印出錯誤信息。但我無法實現它的工作。

所以我只是完全複製了這個例子。但是這個例子也不起作用,所以現在我真的沒有想法。在Jumpstart頁面上,該示例正在運行,但顯然不使用顯示的代碼,因爲這些示例在空的名字上給出了錯誤,但當第一個名稱是「Acme」時,該示例給出錯誤。我可以在日誌中看到表單驗證已完成,並且可以識別錯誤,並且還可以使用錯誤元素上的globalOnly選項將錯誤消息設置爲false。

但我無法獲得輸入字段以獲得樣式並顯示其錯誤。

任何想法這裏有什麼錯?

編輯:這裏所有的我使用的資源:

Java類:

import java.util.Date; 

import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Past; 

import org.apache.tapestry5.annotations.Import; 
import org.apache.tapestry5.annotations.InjectComponent; 
import org.apache.tapestry5.annotations.Property; 
import org.apache.tapestry5.corelib.components.Form; 
import org.apache.tapestry5.corelib.components.TextField; 
import org.apache.tapestry5.corelib.components.Zone; 
import org.apache.tapestry5.ioc.annotations.Inject; 
import org.apache.tapestry5.services.Request; 
import org.apache.tapestry5.services.ajax.AjaxResponseRenderer; 

@Import(stylesheet = "js.css") 
public class FormTest { 
    // Screen fields 

    @Property 
    @NotNull 
    private String firstName; 

    @Property 
    @NotNull 
    private String lastName; 

    @Property 
    @NotNull 
    @Past 
    private Date birthday; 

    // Generally useful bits and pieces 

    @Inject 
    private Request request; 

    @InjectComponent("ajaxForm") 
    private Form form; 

    @InjectComponent("firstName") 
    private TextField firstNameField; 

    @InjectComponent 
    private Zone formZone; 

    @Inject 
    private AjaxResponseRenderer ajaxResponseRenderer; 

    // The code 

    void setupRender() { 
     if (firstName == null && lastName == null && birthday == null) { 
      firstName = "Humpty"; 
      lastName = "Dumpty"; 
      birthday = new Date(0); 
     } 
    } 

    void onValidateFromAjaxForm() { 

     // Note, this method is triggered even if server-side validation has already found error(s). 

     System.out.println(firstName); 
     if (firstName != null && firstName.equals("Acme")) { 
      System.out.println("Fehler"); 
      form.recordError(firstNameField, "First Name must not be Acme."); 
     } 

    } 

    void onSuccess() { 
     if (request.isXHR()) { 
      System.out.println("Success"); 
      ajaxResponseRenderer.addRender(formZone); 
     } 
    } 

    void onFailure() { 
     if (request.isXHR()) { 
      System.out.println("Failure"); 
      ajaxResponseRenderer.addRender(formZone); 
     } 
    } 

    public String getName() { 
     return firstName + " " + lastName; 
    } 

    public Date getServerTime() { 
     return new Date(); 
    } 
} 

模板:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<!-- We need a doctype to allow us to use special characters like &nbsp; 
    We use a "strict" DTD to make IE follow the alignment rules. --> 

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd"> 
<body class="container"> 
    <h3>AJAX Form</h3> 

    <noscript class="js-required"> 
     ${message:javascript_required} 
    </noscript>  

    <p>This page demonstrates how Tapestry AJAX-enables a Form if you specify the zone parameter of the Form.</p> 

    <div class="eg"> 
     <t:zone t:id="formZone" id="formZone"> 
      <t:form t:id="ajaxForm" class="form-horizontal" async="true"> 

       <t:errors globalOnly="false"/> 

       <div class="form-group"> 
        <t:label for="firstName" class="col-sm-2"/> 
        <div class="col-sm-4"> 
         <t:textfield t:id="firstName"/> 
        </div> 
       </div> 
       <div class="form-group"> 
        <t:label for="lastName" class="col-sm-2"/> 
        <div class="col-sm-4"> 
         <t:textfield t:id="lastName"/> 
        </div> 
       </div> 
       <div class="form-group"> 
        <t:label for="birthday" class="col-sm-2"/> 
        <div class="col-sm-4"> 
         <t:datefield t:id="birthday"/> 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-sm-4 col-sm-offset-2"> 
         <t:submit value="Accept"/> 
        </div> 
       </div> 

       Welcome ${name}. Your birthday is ${birthday} 
      </t:form> 
     </t:zone> 
    </div> 

    Note that the following time field does not update because it's not in the zone: ${serverTime}<br/><br/> 

    References: 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/Form.html">Form</a>, 
    <a href="http://tapestry.apache.org/ajax-and-zones.html">Ajax and Zones</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/Zone.html">Zone</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/services/ajax/AjaxResponseRenderer.html">AjaxResponseRenderer</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/services/Request.html">Request</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html">@Inject</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/annotations/InjectComponent.html">@InjectComponent</a>, 
    <a href="http://tapestry.apache.org/5.4/coffeescript/zone.html">t5/core/zone</a>, 
    <a href="http://tapestry.apache.org/5.4/coffeescript/ajax.html">t5/core/ajax</a>, 
    <a href="http://tapestry.apache.org/5.4/coffeescript/forms.html">t5/core/forms</a>.<br/><br/> 

    <t:pagelink page="Index">Home</t:pagelink><br/><br/> 

</body> 
</html> 

js.css:

.eg { 
       margin: 20px 0; 
       padding: 14px; 
       color: #888; 
       border: 1px solid #ddd; 
       border-radius: 6px; 
       -webkit-border-radius: 6px; 
       -mox-border-radius: 6px; 
} 

.js-required { 
       color: red; 
       display: block; 
       margin-bottom: 14px; 
} 

.js-recommended { 
       color: red; 
       display: block; 
       margin-bottom: 14px; 
} 
+0

你如何嘗試驗證?有什麼條件? 我知道它不是解決方案,但如果你檢查基本的東西,你可以使用[在tml驗證](http://tapestry.apache.org/forms-and-validation.html) – LakiGeri

+0

其實,我只是想檢查如果該字段值爲空或不是。在空字段中,應顯示錯誤消息。你是對的,我可以自己做,但是我有什麼Tapestry的?另外我不知道如何將自己的JavaScript邏輯集成到Tapestry的JavaScript工作中。 – khituras

+1

1.它的怪異..因爲它在我的項目中工作我目前正在處理..(你可以在textfield標籤中使用'validate =「required」'btw)但是我完全理解你的想法..也許你應該編輯問題,並複製你的java和tml文件,它可以是一個語義問題。 2.我建議你在另一個問題中問你的js問題,我會看看它,也許我可以幫忙; ) – LakiGeri

回答

0

我有發現t之間的差異他jumpstart描述和真實的形式。我不知道它是否會引起問題,但如果你檢查JumpStart示例的源代碼(在Firefox中檢查元素) 形式標記有一些額外的變量:

<form class="form-horizontal" data-async-trigger="true" data-validate="submit" action="/jumpstart7/examples/ajax/form.ajaxform" method="post" id="ajaxForm"> 

也許如果添加data-async-trigger="true"變量它將工作。

+1

這對我來說沒有任何改變。 xl0e得到了答案:我必須在輸入元素 – khituras

0

你想Tapestry無聲驗證? Tapestry5.4 clientValidation您必須設置您的AppModule contributeApplicationDefaults方法是這樣的:

configuration.add(SymbolConstants.FORM_CLIENT_LOGIC_ENABLED, true); 

刪除該區域,如果FORM_CLIENT_LOGIC_ENABLED = true時,當您點擊提交BTN,你可以看到形狀誤差和項目的URL是這樣的: http://localhost:8080/projectanme/FormTest。這意味着真正的clientValidation。 如果FORM_CLIENT_LOGIC_ENABLED = false,當你點擊你的提交btn時,你可以看到這樣的表單錯誤和項目url:http://localhost:8080/projectanme/FormTest.ajaxform它的意思是不是clientValidation。

更多tapestry5.4教程,你可以看到tapestry5-CMS和掛毯的小部件代碼在github上

+0

上使用t:clientId =「...」謝謝您的回答,但這不是必需的。請參閱xl0e對原始問題的評論。通過給每個區域一個單獨的id,但是t:id相同(因爲組件ID不能是動態的),可以解決問題。 – khituras