我使用的是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