You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Pham Hoang Tien <ph...@gmail.com> on 2012/06/28 11:31:25 UTC
Tapestry usage questions
Hi every one,
I am learning Tapestry5(tapestry core 5.1.0.5), I meet an issue make me
confused. At chapter 6 "User Input Validation", in "Cross-Form Validation"
part have a statement that "If some errors were recorded into the form,
Tapestry will always redisplay the same page, so we don't need to bother
about any navigation issues". when I run the application, I make and
recorded one error into my form in onValidate() event handler. I think it
will be redisplayed, but it go straight to another page from
onSubmitFromRegistrationForm() event handler. Here is my page class:
package com.formos.example.pages;
import java.util.Date;
import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.PasswordField;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.EnumSelectModel;
import com.formos.example.utils.User;
import com.formos.example.utils.celebrities.model.Country;
import com.formos.example.utils.celebrities.model.Gender;
public class Registration {
@ApplicationState
private User user;
@Persist
private String userName;
@Persist
private String password;
private String password2;
@Persist
private Gender gender;
@Persist
private boolean subscribe;
private String email;
private boolean unsubscribe;
@Component(id="registrationForm")
private Form registrationForm;
@Component(id="password")
private PasswordField passwordField;
@Inject
private Messages messages;
public boolean isUnsubscribe() {
return unsubscribe;
}
public void setUnsubscribe(boolean unsubscribe) {
this.unsubscribe = unsubscribe;
}
void onSelectedFromSubmitButton() {
System.out.println("The Submit button was pressed!");
User newUser = new User("John", "Johnson");
this.user = newUser;
nextPage = ShowAll.class;
}
private Class nextPage;
Object onSubmitFromRegistrationForm() {
System.out.println("The form was submitted!");
if (unsubscribe) subscribe = false;
return nextPage;
}
void onValidate() {
System.out.println("In onValidate.");
if (password != null && !password.equals(password2)) {
password = null;
registrationForm.recordError(passwordField,
messages.get("passwords-dont-match"));
}
}
void onSuccess() {
System.out.println("In onSuccess.");
}
void onFailure() {
System.out.println("In onFailure.");
}
@OnEvent(component = "resetButton", value = EventConstants.SELECTED)
void onResetButtonAS() {
userName = null;
password = null;
email = null;
gender = null;
subscribe = false;
System.out.println("The Reset button was pressed!");
}
public boolean isSubscribe() {
return subscribe;
}
public void setSubscribe(boolean subscribe) {
System.out.println("Setting subscribe: " + subscribe);
this.subscribe = subscribe;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
System.out.println("Setting user name: " + userName);
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
System.out.println("Setting password: " + password);
this.password = password;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
System.out.println("Setting gender: " + gender);
this.gender = gender;
}
public Gender getMale() {
return Gender.MALE;
}
public Gender getFemale() {
return Gender.FEMALE;
}
public boolean isPasswordNotSubmitted() {
//return userName == null;
return true;
}
public SelectModel getCountries() {
return new EnumSelectModel(Country.class, messages);
}
@Persist
private Country country;
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@Persist
private Date dateOfBirth;
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
@Persist
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
and here is my page template:
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<head>
<title>Celebrity Collector: Registration</title>
</head>
<body>
Registration
<t:form t:id="registrationForm">
<t:errors/>
<table>
<tr>
<td><t:label t:for="userName">Label for user name</t:label>:</td>
<td>
<input type="text" t:type="textfield" t:id="userName"
t:value="userName"
t:validate="required,minlength=3,maxlength=8" />
</td>
</tr>
<t:if t:test="passwordNotSubmitted">
<tr>
<td><t:label t:for="password">Label for password</t:label>:</td>
<td>
<input type="text" t:type="passwordfield" t:id="password"
t:value="password"
t:validate="required,minlength=3,maxlength=8" />
</td>
</tr>
<tr>
<td><t:label t:for="password2">Label for password 2</t:label>:</td>
<td><input type="text" t:type="passwordfield" t:id="password2"
t:label="Repeat password" t:value="password2" /></td>
</tr>
</t:if>
<tr>
<td>Gender:</td>
<td>
<t:radiogroup t:value="gender">
<input type="radio" t:type="radio" t:value="male"/>Male
<input type="radio" t:type="radio" t:value="female"/>Female
</t:radiogroup>
</td>
</tr>
<tr>
<td>Birth Date:</td>
<td><input type="text" t:type="datefield" t:value="dateOfBirth"
t:format="EEE, MMM d, ''yy" /></td>
</tr>
<tr>
<td><t:label t:for="age" />:</td>
<td>
<input type="text" t:type="textfield" t:id="age"
t:validate="required,min=5,max=120" />
</td>
</tr>
<tr>
<t:if t:test="subscribe">
<td><t:label t:for="email" /></td>
<td>
<input type="text" t:type="textfield" t:id="email" t:value="email"
t:validate="regexp" />
<input type="checkbox" t:type="checkbox" t:value="unsubscribe"
onclick="this.form.submit()" /> I don't want to subscribe
</td>
<t:parameter t:name="else">
<td colspan="2">
<input type="checkbox" t:type="checkbox"
t:value="subscribe" onclick="this.form.submit()" />
Check the box to subscribe to our Newsletter.
</td>
</t:parameter>
</t:if>
</tr>
<tr>
<td>Country:</td>
<td>
<select t:type="select" t:model="countries" t:value="country" />
</td>
</tr>
</table>
<input type="submit" t:type="submit" t:id="submitButton" value="Submit"/>
<input type="submit" t:type="submit" t:id="resetButton" value="Reset"/>
</t:form>
<br />
# Back to the Start Page
</body>
</html>
Please help me to understand, thank every one.
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-usage-questions-tp5714141.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Tapestry usage questions
Posted by Pham Hoang Tien <ph...@gmail.com>.
Thank Thiago so much, I understood this issue.
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-usage-questions-tp5714141p5714177.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Tapestry usage questions
Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 28 Jun 2012 06:31:25 -0300, Pham Hoang Tien
<ph...@gmail.com> wrote:
> Hi every one,
Hi!
> I am learning Tapestry5(tapestry core 5.1.0.5),
Why are you learning an old version?
> about any navigation issues". when I run the application, I make and
> recorded one error into my form in onValidate() event handler.
In 5.1.0.5, for whole form validation (not a single field validation), the
event is named ValidateForm, so you should use onValidateForm() instead.
Are you sure your validation event handler method is being called.
--
Thiago H. de Paula Figueiredo
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Tapestry usage questions
Posted by Pham Hoang Tien <ph...@gmail.com>.
Thank Taha,
It's perfect. I done.
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-usage-questions-tp5714141p5714176.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Tapestry usage questions
Posted by Taha Siddiqi <ta...@gmail.com>.
Hi
On form submission, onSubmit is called after both onSuccess and onFailure. You should use onFailure to handle a failure and onSuccess to handle success. In case there is a common logic in both cases to be executed at the end, only in that case, you should use onSubmit.
regards
Taha
On Jun 28, 2012, at 3:01 PM, Pham Hoang Tien wrote:
> Hi every one,
>
> I am learning Tapestry5(tapestry core 5.1.0.5), I meet an issue make me
> confused. At chapter 6 "User Input Validation", in "Cross-Form Validation"
> part have a statement that "If some errors were recorded into the form,
> Tapestry will always redisplay the same page, so we don't need to bother
> about any navigation issues". when I run the application, I make and
> recorded one error into my form in onValidate() event handler. I think it
> will be redisplayed, but it go straight to another page from
> onSubmitFromRegistrationForm() event handler. Here is my page class:
>
> package com.formos.example.pages;
>
> import java.util.Date;
>
> import org.apache.tapestry5.EventConstants;
> import org.apache.tapestry5.SelectModel;
> import org.apache.tapestry5.annotations.ApplicationState;
> import org.apache.tapestry5.annotations.Component;
> import org.apache.tapestry5.annotations.OnEvent;
> import org.apache.tapestry5.annotations.Persist;
> import org.apache.tapestry5.corelib.components.Form;
> import org.apache.tapestry5.corelib.components.PasswordField;
> import org.apache.tapestry5.ioc.Messages;
> import org.apache.tapestry5.ioc.annotations.Inject;
> import org.apache.tapestry5.util.EnumSelectModel;
>
> import com.formos.example.utils.User;
> import com.formos.example.utils.celebrities.model.Country;
> import com.formos.example.utils.celebrities.model.Gender;
>
> public class Registration {
> @ApplicationState
> private User user;
>
> @Persist
> private String userName;
>
> @Persist
> private String password;
> private String password2;
>
> @Persist
> private Gender gender;
>
> @Persist
> private boolean subscribe;
> private String email;
>
> private boolean unsubscribe;
>
> @Component(id="registrationForm")
> private Form registrationForm;
>
> @Component(id="password")
> private PasswordField passwordField;
>
> @Inject
> private Messages messages;
>
> public boolean isUnsubscribe() {
> return unsubscribe;
> }
>
> public void setUnsubscribe(boolean unsubscribe) {
> this.unsubscribe = unsubscribe;
> }
>
> void onSelectedFromSubmitButton() {
> System.out.println("The Submit button was pressed!");
> User newUser = new User("John", "Johnson");
> this.user = newUser;
> nextPage = ShowAll.class;
> }
>
> private Class nextPage;
> Object onSubmitFromRegistrationForm() {
> System.out.println("The form was submitted!");
> if (unsubscribe) subscribe = false;
> return nextPage;
> }
>
> void onValidate() {
> System.out.println("In onValidate.");
> if (password != null && !password.equals(password2)) {
> password = null;
> registrationForm.recordError(passwordField,
> messages.get("passwords-dont-match"));
> }
> }
>
> void onSuccess() {
> System.out.println("In onSuccess.");
> }
>
> void onFailure() {
> System.out.println("In onFailure.");
> }
>
> @OnEvent(component = "resetButton", value = EventConstants.SELECTED)
> void onResetButtonAS() {
> userName = null;
> password = null;
> email = null;
> gender = null;
> subscribe = false;
> System.out.println("The Reset button was pressed!");
> }
>
> public boolean isSubscribe() {
> return subscribe;
> }
>
> public void setSubscribe(boolean subscribe) {
> System.out.println("Setting subscribe: " + subscribe);
> this.subscribe = subscribe;
> }
>
> public String getEmail() {
> return email;
> }
>
> public void setEmail(String email) {
> this.email = email;
> }
>
> public String getUserName() {
> return userName;
> }
>
> public void setUserName(String userName) {
> System.out.println("Setting user name: " + userName);
> this.userName = userName;
> }
>
> public String getPassword() {
> return password;
> }
>
> public void setPassword(String password) {
> System.out.println("Setting password: " + password);
> this.password = password;
> }
>
> public String getPassword2() {
> return password2;
> }
>
> public void setPassword2(String password2) {
> this.password2 = password2;
> }
>
> public Gender getGender() {
> return gender;
> }
>
> public void setGender(Gender gender) {
> System.out.println("Setting gender: " + gender);
> this.gender = gender;
> }
>
> public Gender getMale() {
> return Gender.MALE;
> }
>
> public Gender getFemale() {
> return Gender.FEMALE;
> }
>
> public boolean isPasswordNotSubmitted() {
> //return userName == null;
> return true;
> }
>
> public SelectModel getCountries() {
> return new EnumSelectModel(Country.class, messages);
> }
>
> @Persist
> private Country country;
>
> public Country getCountry() {
> return country;
> }
>
> public void setCountry(Country country) {
> this.country = country;
> }
>
> @Persist
> private Date dateOfBirth;
>
> public Date getDateOfBirth() {
> return dateOfBirth;
> }
>
> public void setDateOfBirth(Date dateOfBirth) {
> this.dateOfBirth = dateOfBirth;
> }
>
> @Persist
> private int age;
>
> public int getAge() {
> return age;
> }
>
> public void setAge(int age) {
> this.age = age;
> }
> }
>
> and here is my page template:
>
> <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
> <head>
> <title>Celebrity Collector: Registration</title>
> </head>
> <body>
>
> Registration
>
> <t:form t:id="registrationForm">
> <t:errors/>
> <table>
> <tr>
> <td><t:label t:for="userName">Label for user name</t:label>:</td>
> <td>
> <input type="text" t:type="textfield" t:id="userName"
> t:value="userName"
> t:validate="required,minlength=3,maxlength=8" />
> </td>
> </tr>
> <t:if t:test="passwordNotSubmitted">
> <tr>
> <td><t:label t:for="password">Label for password</t:label>:</td>
> <td>
> <input type="text" t:type="passwordfield" t:id="password"
> t:value="password"
> t:validate="required,minlength=3,maxlength=8" />
> </td>
> </tr>
> <tr>
> <td><t:label t:for="password2">Label for password 2</t:label>:</td>
> <td><input type="text" t:type="passwordfield" t:id="password2"
> t:label="Repeat password" t:value="password2" /></td>
> </tr>
> </t:if>
> <tr>
> <td>Gender:</td>
> <td>
> <t:radiogroup t:value="gender">
> <input type="radio" t:type="radio" t:value="male"/>Male
> <input type="radio" t:type="radio" t:value="female"/>Female
> </t:radiogroup>
> </td>
> </tr>
> <tr>
> <td>Birth Date:</td>
> <td><input type="text" t:type="datefield" t:value="dateOfBirth"
> t:format="EEE, MMM d, ''yy" /></td>
> </tr>
> <tr>
> <td><t:label t:for="age" />:</td>
> <td>
> <input type="text" t:type="textfield" t:id="age"
> t:validate="required,min=5,max=120" />
> </td>
> </tr>
> <tr>
> <t:if t:test="subscribe">
> <td><t:label t:for="email" /></td>
> <td>
> <input type="text" t:type="textfield" t:id="email" t:value="email"
> t:validate="regexp" />
> <input type="checkbox" t:type="checkbox" t:value="unsubscribe"
> onclick="this.form.submit()" /> I don't want to subscribe
> </td>
> <t:parameter t:name="else">
> <td colspan="2">
> <input type="checkbox" t:type="checkbox"
> t:value="subscribe" onclick="this.form.submit()" />
> Check the box to subscribe to our Newsletter.
> </td>
> </t:parameter>
> </t:if>
> </tr>
> <tr>
> <td>Country:</td>
> <td>
> <select t:type="select" t:model="countries" t:value="country" />
> </td>
> </tr>
> </table>
> <input type="submit" t:type="submit" t:id="submitButton" value="Submit"/>
> <input type="submit" t:type="submit" t:id="resetButton" value="Reset"/>
> </t:form>
> <br />
> # Back to the Start Page
> </body>
> </html>
>
> Please help me to understand, thank every one.
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-usage-questions-tp5714141.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org