You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Michal Gruca <mi...@gmail.com> on 2010/07/20 11:23:26 UTC

Validation in component using onValidate method

Hi all,
I'm using tapestry for some time but for first time I cannot find correct
solution by myself. My problem is with validation in onValidate method. I
get that onValidate is called for every component inside form and
onValidateForm is called after all other onValidates but I don't get why
first Tapestery generates validate event and only after that it calls setter
method for validated property. 
As I always could call onValidateForm, then it wasn't such a problem, but
currently I need to do validation from inside of component. Structure is
following: Page contains form and component which is just a part of that
form. Component contains just a textarea, so validateForm method is not
called. IMHO onValidateFromMyField method should do the job but inside I
always get null. About year ago I was putting hidden field inside the form,
but it's wasn't the solution and it definitely wasn't reliable. I always
could find some simple workaround, but now the only solution that I see, is
to make whole validation inside page validateForm method, what would result
in violation of DRY principle (b/c I would have to do same trick in every
page that uses my component). Project in which I have this problem is
relatively small, so I could implement this workaround, but everything in me
opose to do that (because of the smell it would left, and feeling that some
day it won't be a small project).
As my english is not the best I prepared small example which should be more
expressive than my explanation.
	
1. Page class

		@Property
		@Persist
		private String foo;
		@Property
		@Persist
		private String bar;
		@Inject
		private Logger log;
		@Inject
		private ComponentResources resources;

		@Log
		public void onValidateForm() throws ValidationException {
			log.debug("--------------------------------------------------------");
			log.debug("foo '{}', bar '{}'", foo, bar);
		}
		@Log
		void onSuccess() {
			resources.discardPersistentFieldChanges();
		}
	
	
2. tml file for that page
	
		<form t:type="form" t:id="foobar">
			<t:textfield value="foo"/>
			<t:textfield value="bar"/>
			<t:comp/>
			<t:submit value="save"/>
		</form>
	
3. component code
	
		@Inject
		private Logger log;
		@Persist
		private String password;

		@Log
		void onValidateFromPasswordComponent() throws ValidationException {
			log.debug("Password '{}'", password);
			// do validation, if goes wrong throw new
			// ValidationException(recordedError);
		}
		@Log
		void onValidate() throws ValidationException {
			log.debug("Password '{}'", password);
			// do validation, if goes wrong throw new
			// ValidationException(recordedError);
		}
		@Log
		public void setPassword(String password) {
			this.password = password;
		}
		@Log
		public String getPassword() {
			return password;
		}
	
	
	
		<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
			  xmlns:p="tapestry:parameter">

		<t:content>
				<t:textfield t:id="passwordComponent" value="password"/>
		</t:content>
		</html>
	

	
4. Log from submit. Values inputed into fields were a, b, c:

[DEBUG] components.Comp [ENTER] onValidate()
[DEBUG] components.Comp Password 'null'
[DEBUG] components.Comp [ EXIT] onValidate
[DEBUG] components.Comp [ENTER] onValidateFromPasswordComponent()
[DEBUG] components.Comp Password 'null'
[DEBUG] components.Comp [ EXIT] onValidateFromPasswordComponent
[DEBUG] components.Comp [ENTER] setPassword("c")
[DEBUG] components.Comp [ EXIT] setPassword
[DEBUG] pages.About [ENTER] onValidateForm()
[DEBUG] pages.About --------------------------------------------------------
[DEBUG] pages.About foo 'a', bar 'b'
[DEBUG] pages.About [ EXIT] onValidateForm
[DEBUG] pages.About [ENTER] onSuccess()
[DEBUG] pages.About [ EXIT] onSuccess

IMHO it is bug or really counterintuitive feature. How I am supposed to do
validation when value is always null?
Please help, I really want to do this properly.

Regards 
Michał Gruca
-- 
View this message in context: http://tapestry-users.832.n2.nabble.com/Validation-in-component-using-onValidate-method-tp5315887p5315887.html
Sent from the Tapestry Users 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: Validation in component using onValidate method

Posted by Michal Gruca <mi...@gmail.com>.
Issue created:
https://issues.apache.org/jira/browse/TAP5-1212
-- 
View this message in context: http://tapestry-users.832.n2.nabble.com/Validation-in-component-using-onValidate-method-tp5315887p5317888.html
Sent from the Tapestry Users 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: Validation in component using onValidate method

Posted by Michal Gruca <mi...@gmail.com>.

Thiago H. de Paula Figueiredo wrote:
> 
>> But one suggestion. Maybe tapestry should log on warn level if someone
>> creates onValidate method with no parameters, else someone may be as
>> confused as I was. I don't see any purpose of onValidate without  
>> parameters.
> 
> That's a very good idea. Maybe Tapestry should go further and throw an  
> exception, as a no-arg onValidate method is useless. Please file a JIRA in  
> the Tapestry bug tracker for that.
> 
I can't agree on that. This could break some apps that were using my kind of
workaround (adding additional hidden fields) to make onValidate() work
properly. I will create ticket, but later. Now time to go home and test new
design :P
-- 
View this message in context: http://tapestry-users.832.n2.nabble.com/Validation-in-component-using-onValidate-method-tp5315887p5316675.html
Sent from the Tapestry Users 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: Validation in component using onValidate method

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 20 Jul 2010 10:02:41 -0300, Michal Gruca <mi...@gmail.com>  
wrote:

> I'm just blind or dumb. My code was almost same as example I provided,  
> with onValidate metod without any value. That's why I didn't saw any  
> value. That solves the problem, thanks.

This happens. :)

> But one suggestion. Maybe tapestry should log on warn level if someone
> creates onValidate method with no parameters, else someone may be as
> confused as I was. I don't see any purpose of onValidate without  
> parameters.

That's a very good idea. Maybe Tapestry should go further and throw an  
exception, as a no-arg onValidate method is useless. Please file a JIRA in  
the Tapestry bug tracker for that.

> Thanks again, that helped a lot :)

You're welcome!

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Validation in component using onValidate method

Posted by Michal Gruca <mi...@gmail.com>.

Thiago H. de Paula Figueiredo wrote:
> 
> Hi!
> This way, the developer can prevent the setter method of being invoked  
> with an invalid value.
> 
Ok, I get it now. First I wanted to ask how are You supposed to get that
value, but example I wanted to use (from bottom of this page
http://tapestry.apache.org/tapestry5.1/guide/validation.html) showed me that
I'm just blind or dumb. My code was almost same as example I provided, with
onValidate metod without any value. That's why I didn't saw any value. That
solves the problem, thanks. 
But one suggestion. Maybe tapestry should log on warn level if someone
creates onValidate method with no parameters, else someone may be as
confused as I was. I don't see any purpose of onValidate without parameters.

Thanks again, that helped a lot :)

Regards
Michał Gruca 
-- 
View this message in context: http://tapestry-users.832.n2.nabble.com/Validation-in-component-using-onValidate-method-tp5315887p5316511.html
Sent from the Tapestry Users 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: Validation in component using onValidate method

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 20 Jul 2010 06:23:26 -0300, Michal Gruca <mi...@gmail.com>  
wrote:

> Hi all,

Hi!

> I'm using tapestry for some time but for first time I cannot find correct
> solution by myself. My problem is with validation in onValidate method. I
> get that onValidate is called for every component inside form and
> onValidateForm is called after all other onValidates but I don't get why
> first Tapestery generates validate event and only after that it calls  
> setter method for validated property.

This way, the developer can prevent the setter method of being invoked  
with an invalid value.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org