Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Ajax, Jemplate, forms, rest & data validation

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


henry.drinkwater at googlemail

May 23, 2008, 3:10 PM

Post #1 of 5 (369 views)
Permalink
Ajax, Jemplate, forms, rest & data validation

Hi,

I'm new to Catalyst so apologies if this is a dumb question. I'm working on
a web site using Catalyst and want to use quite a bit of Ajax interactivity
to make the site as user friendly and modern as possible. The majority of my
knowledge of Catalyst comes from the Jonathon Rockway book, so I have so far
been concentrating on using Jemplate for the AJAX, and using a REST
interface to handle the data serialization.

This is working very nicely for the retrieve/get part, but I am struggling a
bit to figure out the best way of handling forms and the submission &
validation of the data. The formbuilder controller makes it incredibly
simple to build and validate a form, on both the client and the server, and
I want to have similar functionality but AJAX enabled - i.e. I envisage the
form submission method being a PUT to a REST controller, and for the
submission to cause a div to be updated in place, rather than a load of a
new page.

In the book the example uses a POST to a pre-existing formbuilder controller
as the submission method of the ajax form, which is neat in that it handles
the data validation part without writing any code, but not so good in that
it is not really ajax-y, as it results in loading a new page. The book also
mentions putting data validation into the Model and not in the controller,
which I guess would work fine but will require quite a bit more code to be
written. I'd rather not have to write the validation myself if there's a
good module out there which would do it for me, based on the form
definition.

So I'm looking for recommendations as to how to handle this - would I be
better taking the time to learn one of the javascript libraries (e.g. YUI or
Dojo) and moving away from Jemplate, or is there a way to combine
formbuilder and Jemplate in a more dynamic ajax-y way? If anyone has any
suggestions for other tutorials or example code I could look at to get more
ideas of how to do this, I'd be very grateful.

regards,
Henry

ps I have been following the thread on RFC: Catalyst::Controller::REST::DBIC
on the list, which seems to be talking about stuff along these lines, but I
haven't really gleaned any ideas on how best to tackle this. I also found
Jay Shirley's slides on "No REST for the Wicked: Catalyst, REST and YUI"
which sounded very relevant, but again I didn't learn enough to really get
me coding.

pps I found the book incredibly helpful, once I found the errata web site
and got past chapter 3. It got me very quickly over the learning curve on
Catalyst and writing real code.


jshirley at gmail

May 23, 2008, 4:20 PM

Post #2 of 5 (347 views)
Permalink
Re: Ajax, Jemplate, forms, rest & data validation [In reply to]

On Fri, May 23, 2008 at 3:10 PM, Henry Drinkwater
<henry.drinkwater[at]googlemail.com> wrote:
> Hi,
>
> I'm new to Catalyst so apologies if this is a dumb question. I'm working on
> a web site using Catalyst and want to use quite a bit of Ajax interactivity
> to make the site as user friendly and modern as possible. The majority of my
> knowledge of Catalyst comes from the Jonathon Rockway book, so I have so far
> been concentrating on using Jemplate for the AJAX, and using a REST
> interface to handle the data serialization.
>
> This is working very nicely for the retrieve/get part, but I am struggling a
> bit to figure out the best way of handling forms and the submission &
> validation of the data. The formbuilder controller makes it incredibly
> simple to build and validate a form, on both the client and the server, and
> I want to have similar functionality but AJAX enabled - i.e. I envisage the
> form submission method being a PUT to a REST controller, and for the
> submission to cause a div to be updated in place, rather than a load of a
> new page.
>
> In the book the example uses a POST to a pre-existing formbuilder controller
> as the submission method of the ajax form, which is neat in that it handles
> the data validation part without writing any code, but not so good in that
> it is not really ajax-y, as it results in loading a new page. The book also
> mentions putting data validation into the Model and not in the controller,
> which I guess would work fine but will require quite a bit more code to be
> written. I'd rather not have to write the validation myself if there's a
> good module out there which would do it for me, based on the form
> definition.
>
> So I'm looking for recommendations as to how to handle this - would I be
> better taking the time to learn one of the javascript libraries (e.g. YUI or
> Dojo) and moving away from Jemplate, or is there a way to combine
> formbuilder and Jemplate in a more dynamic ajax-y way? If anyone has any
> suggestions for other tutorials or example code I could look at to get more
> ideas of how to do this, I'd be very grateful.
>
> regards,
> Henry
>
> ps I have been following the thread on RFC: Catalyst::Controller::REST::DBIC
> on the list, which seems to be talking about stuff along these lines, but I
> haven't really gleaned any ideas on how best to tackle this. I also found
> Jay Shirley's slides on "No REST for the Wicked: Catalyst, REST and YUI"
> which sounded very relevant, but again I didn't learn enough to really get
> me coding.
>
> pps I found the book incredibly helpful, once I found the errata web site
> and got past chapter 3. It got me very quickly over the learning curve on
> Catalyst and writing real code.
>
> _______________________________________________
> List: Catalyst[at]lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>


Henry,

At this point I'd advise you to look through the examples/ dir in the
Catalyst SVN and you'll get more applicable knowledge.

http://dev.catalystframework.org/repos/Catalyst/trunk/examples/

My talk on REST coincides with principles that are coded up in
http://dev.catalystframework.org/repos/Catalyst/trunk/examples/RestYUI
(although the application name is AdventREST, it is modified from the
original Advent calendar entry to work with YUI).

On the validation note, I use Data::FormValidator in conjunction with
a DBIx::Class ResultSet - still in the model, in that it is outside of
the Catalyst scope. Easier to unit test this stuff, too, since you
don't have to muck with a controller. Lots of other folk use
FormBuilder or FormFu, so it is really just picking what makes the
most sense to you.

Good luck,
-J

_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


perlwle at yahoo

May 26, 2008, 10:03 AM

Post #3 of 5 (314 views)
Permalink
Re: Ajax, Jemplate, forms, rest & data validation [In reply to]

On Fri, May 23, 2008 at 3:10 PM, Henry Drinkwater
<henry.drinkwater[at]googlemail.com> wrote:

[snip]

On the validation note, I use Data::FormValidator in
conjunction with
a DBIx::Class ResultSet - still in the model, in that
it is outside of
the Catalyst scope. Easier to unit test this stuff,
too, since you
don't have to muck with a controller. Lots of other
folk use
FormBuilder or FormFu, so it is really just picking
what makes the
most sense to you.

Good luck,
-J
===================================================

hi,

i am using D::F in controller only then rely on the
database(sometimes little bit on model) to die on
unexpected data.

I am interested in how you managed to use
Data::FormValidator in conjunction with a DBIx::Class
ResultSet.. do you still do form validation in
controller? how do you map the error from model to
webGUI?

thanks!

James.




_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


jshirley at gmail

May 27, 2008, 7:46 AM

Post #4 of 5 (303 views)
Permalink
Re: Ajax, Jemplate, forms, rest & data validatio [In reply to]

On Mon, May 26, 2008 at 10:03 AM, James. L <perlwle[at]yahoo.com> wrote:
> On Fri, May 23, 2008 at 3:10 PM, Henry Drinkwater
> <henry.drinkwater[at]googlemail.com> wrote:
>
> [snip]
>
> On the validation note, I use Data::FormValidator in
> conjunction with
> a DBIx::Class ResultSet - still in the model, in that
> it is outside of
> the Catalyst scope. Easier to unit test this stuff,
> too, since you
> don't have to muck with a controller. Lots of other
> folk use
> FormBuilder or FormFu, so it is really just picking
> what makes the
> most sense to you.
>
> Good luck,
> -J
> ===================================================
>
> hi,
>
> i am using D::F in controller only then rely on the
> database(sometimes little bit on model) to die on
> unexpected data.
>
> I am interested in how you managed to use
> Data::FormValidator in conjunction with a DBIx::Class
> ResultSet.. do you still do form validation in
> controller? how do you map the error from model to
> webGUI?
>
> thanks!
>
> James.
>
>
>
>


The basic idea is that you have a base schema class, rather than all
of your schema classes doing:
use base qw/DBIx::Class/;

Instead, something like this (naming rather verbose, just to be clear):
use base qw/MyApp::Base::Schema::Class/;

In there, you assign the ResultSet. Because of the order of
operations in DBIC, the way to have a base schema class setup a custom
ResultSet class is to run something after table. In the easiest of
ways (and not very pretty):

package MyApp::Base::Schema::Class;

use base 'DBIx::Class';

sub table {
my $class = shift;
$class->next::method(@_);
# Automatically populate the resultset to include serialization.
$class->resultset_class('MyApp::ResultSet::Validate')
if $class->resultset_class eq 'DBIx::Class::ResultSet';
}

1;

Now, you have a custom resultset class that gets loaded by default.
Ideally, you'll not use this globally but name it like
Class::WithValidate or something.

Then inside of the resultset, just create a ->validate method that you
can call. A very basic validate method (that just returns the valid
data, you could pass it onwards to a $self->create($data)):

sub validate {
my ( $self, $data, $profile ) = @_;
my $results = Data::FormValidator->check($data, $profile);

if ( $results->has_invalid or $results->has_missing ) {
die $results;
}
return scalar $results->valid;
}

How you determine how to store your $profile is up to you, and there
are way too many ways to do it. If inside of Catalyst,
__PACKAGE__->config->profiles->{create} seems reasonable enough.

I'm curious what others on the list think of this. I've never been
pleased enough with it to bake it for public eyes, and it doesn't
evolve fast enough for me to seek feedback.

_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


c.a.vincent at newcastle

May 28, 2008, 6:48 AM

Post #5 of 5 (291 views)
Permalink
RE: Re: Ajax, Jemplate, forms, rest & data validatio [In reply to]

>-----Original Message-----
>From: J. Shirley [mailto:jshirley[at]gmail.com]
>Sent: 27 May 2008 15:46
>To: catalyst[at]lists.scsys.co.uk
>Subject: [Catalyst] Re: Ajax, Jemplate, forms, rest & data validatio
>
>On Mon, May 26, 2008 at 10:03 AM, James. L <perlwle[at]yahoo.com> wrote:
>> On Fri, May 23, 2008 at 3:10 PM, Henry Drinkwater
>> <henry.drinkwater[at]googlemail.com> wrote:
>>
>> [snip]
>>
>> On the validation note, I use Data::FormValidator in
>> conjunction with
>> a DBIx::Class ResultSet - still in the model, in that
>> it is outside of
>> the Catalyst scope. Easier to unit test this stuff,
>> too, since you
>> don't have to muck with a controller. Lots of other
>> folk use
>> FormBuilder or FormFu, so it is really just picking
>> what makes the
>> most sense to you.
>>
>> Good luck,
>> -J
>> ===================================================
>>
>> hi,
>>
>> i am using D::F in controller only then rely on the
>> database(sometimes little bit on model) to die on
>> unexpected data.
>>
>> I am interested in how you managed to use
>> Data::FormValidator in conjunction with a DBIx::Class
>> ResultSet.. do you still do form validation in
>> controller? how do you map the error from model to
>> webGUI?
>>
>> thanks!
>>
>> James.
>>
>>
>>
>>
>
>
>The basic idea is that you have a base schema class, rather than all
>of your schema classes doing:
>use base qw/DBIx::Class/;
>
>Instead, something like this (naming rather verbose, just to be
>clear):
>use base qw/MyApp::Base::Schema::Class/;
>
>In there, you assign the ResultSet. Because of the order of
>operations in DBIC, the way to have a base schema class setup a custom
>ResultSet class is to run something after table. In the easiest of
>ways (and not very pretty):
>
>package MyApp::Base::Schema::Class;
>
>use base 'DBIx::Class';
>
>sub table {
> my $class = shift;
> $class->next::method(@_);
> # Automatically populate the resultset to include serialization.
> $class->resultset_class('MyApp::ResultSet::Validate')
> if $class->resultset_class eq 'DBIx::Class::ResultSet';
>}
>
>1;
>
>Now, you have a custom resultset class that gets loaded by default.
>Ideally, you'll not use this globally but name it like
>Class::WithValidate or something.
>
>Then inside of the resultset, just create a ->validate method that you
>can call. A very basic validate method (that just returns the valid
>data, you could pass it onwards to a $self->create($data)):
>
>sub validate {
> my ( $self, $data, $profile ) = @_;
> my $results = Data::FormValidator->check($data, $profile);
>
> if ( $results->has_invalid or $results->has_missing ) {
> die $results;
> }
> return scalar $results->valid;
>}
>
>How you determine how to store your $profile is up to you, and there
>are way too many ways to do it. If inside of Catalyst,
>__PACKAGE__->config->profiles->{create} seems reasonable enough.
>
>I'm curious what others on the list think of this. I've never been
>pleased enough with it to bake it for public eyes, and it doesn't
>evolve fast enough for me to seek feedback.


Or you can use CLaco's DBIx::Class::Validation component in your DBIC
schemas to hook in a Data::Form::Validator (FormValidator::Simple is
also supported) profile to validate against on inserts and updates.

We use that method - getting the validation in at that level protects my
data from:
1) me - forgetting to validate at a higher level - sometimes I think I
know better...
2) MySQL - which can't be relied upon to throw an error when given duff
data - sometimes it thinks it knows better...

Cheers

Carl

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Carl Vincent http://www.netskills.ac.uk/ (URL)
Systems Manager 0191 222 5003 (voice)
Netskills, Newcastle University 0191 222 5001 (fax)
Training - Accreditation - Consultancy - Development


_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.