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-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to