我使用的是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
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;
}
你如何嘗試驗證?有什麼條件? 我知道它不是解決方案,但如果你檢查基本的東西,你可以使用[在tml驗證](http://tapestry.apache.org/forms-and-validation.html) – LakiGeri
其實,我只是想檢查如果該字段值爲空或不是。在空字段中,應顯示錯誤消息。你是對的,我可以自己做,但是我有什麼Tapestry的?另外我不知道如何將自己的JavaScript邏輯集成到Tapestry的JavaScript工作中。 – khituras
1.它的怪異..因爲它在我的項目中工作我目前正在處理..(你可以在textfield標籤中使用'validate =「required」'btw)但是我完全理解你的想法..也許你應該編輯問題,並複製你的java和tml文件,它可以是一個語義問題。 2.我建議你在另一個問題中問你的js問題,我會看看它,也許我可以幫忙; ) – LakiGeri