[Catalyst] Using C::C::FormBuilder With DBIC

2007-02-18 Thread hkclark

Hi Everyone,

I have been experimenting with Catalyst::Controller::FormBuilder.  I'm
trying to provide an edit form for objects read from and written to the DB
via DBIx::Class.  Have FormBuilder automatically load the data from DBIC
into the form seems pretty easy with something like:

   my $widget = $c-model('MyAppDB::Widget')-find($id);
   my $form = $self-formbuilder;
   $form-values($widget-get_columns);


However, an elegant way of writing the data back out is not jumping out at
me.  I know I could manually copy each field out of the $form object to my
$widget object, but it seems like there should be simple way to do it all in
one line like I got with the $form-values($widget-get_columns); above.

Any suggestions?  What are other folks doing for C::C::FormBuilder and DBIC?

Thanks,
Kennedy
___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Using C::C::FormBuilder With DBIC

2007-02-18 Thread RA Jones

[EMAIL PROTECTED] wrote:

Hi Everyone,

I have been experimenting with Catalyst::Controller::FormBuilder.  I'm 
trying to provide an edit form for objects read from and written to the 
DB via DBIx::Class.  Have FormBuilder automatically load the data from 
DBIC into the form seems pretty easy with something like:


my $widget = $c-model('MyAppDB::Widget')-find($id);
my $form = $self-formbuilder;
$form-values($widget-get_columns);


However, an elegant way of writing the data back out is not jumping out 
at me.  I know I could manually copy each field out of the $form object 
to my $widget object, but it seems like there should be simple way to do 
it all in one line like I got with the 
$form-values($widget-get_columns); above.


Any suggestions?  What are other folks doing for C::C::FormBuilder and DBIC?


I'm pretty new to this, but this is what I did:

sub edit : Local Form {

  # get data into form pretty much as above, then:

if ( $form-submitted  $form-validate ) {
  $c-stash-{user} = $user;
  $c-forward('do_edit');
}

sub do_edit : Private {
  my ($self, $c) = @_;
  my $form = $self-formbuilder;
  my $fields = $form-field;
  $c-stash-{user}-update($fields);
  $c-flash-{status_msg} = 'User updated';
  $c-response-redirect( $c-req-base . 'users/view/' .
$c-stash-{user}-id );
}

Not sure if it's the 'best' way, but it works. Will be interesting to 
see what other suggestions you get.

--
Richard Jones
Leeds, UK
mailto:[EMAIL PROTECTED]

___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Using C::C::FormBuilder With DBIC

2007-02-18 Thread hkclark

On 2/18/07, RA Jones [EMAIL PROTECTED] wrote:


 Any suggestions?  What are other folks doing for C::C::FormBuilder and
DBIC?

I'm pretty new to this, but this is what I did:

sub edit : Local Form {

   # get data into form pretty much as above, then:

if ( $form-submitted  $form-validate ) {
   $c-stash-{user} = $user;
   $c-forward('do_edit');
}

sub do_edit : Private {
   my ($self, $c) = @_;
   my $form = $self-formbuilder;
   my $fields = $form-field;
   $c-stash-{user}-update($fields);
   $c-flash-{status_msg} = 'User updated';
   $c-response-redirect( $c-req-base . 'users/view/' .
 $c-stash-{user}-id );
}

Not sure if it's the 'best' way, but it works. Will be interesting to
see what other suggestions you get.
--
Richard Jones
Leeds, UK
mailto:[EMAIL PROTECTED]



Hi Richard,

Thank you very much for your suggestion.  Argh!  That crossed my mind when I
saw ResultSet-update() in the docs, but I incorrectly looked at
Row-update() (which doesn't support a hashref of fields to update) and
convinced myself it wouldn't be possible (but Row obviously doesn't
represent a single object returned by ResultSet, so I should have stuck with
the docs for ResultSet-update!).  That's what I get for staying up too late
playing with this vs. getting some sleep and trying it the next AM. :-(

I would still be interested to see if other folks have alternative
solutions.  It's also interesting to see how folks break up the flow of the
initial form display vs. the form updating (in one method of the controller
vs. multiple methods, how they organize it, etc.).  Are there any
recommendations on best practices?

Thanks,
Kennedy
___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Using C::C::FormBuilder With DBIC

2007-02-18 Thread Carl Franks

On 18/02/07, RA Jones [EMAIL PROTECTED] wrote:

[EMAIL PROTECTED] wrote:
 Hi Everyone,

 I have been experimenting with Catalyst::Controller::FormBuilder.  I'm
 trying to provide an edit form for objects read from and written to the
 DB via DBIx::Class.  Have FormBuilder automatically load the data from
 DBIC into the form seems pretty easy with something like:

 my $widget = $c-model('MyAppDB::Widget')-find($id);
 my $form = $self-formbuilder;
 $form-values($widget-get_columns);


 However, an elegant way of writing the data back out is not jumping out
 at me.  I know I could manually copy each field out of the $form object
 to my $widget object, but it seems like there should be simple way to do
 it all in one line like I got with the
 $form-values($widget-get_columns); above.

 Any suggestions?  What are other folks doing for C::C::FormBuilder and DBIC?

I'm pretty new to this, but this is what I did:

sub edit : Local Form {

   # get data into form pretty much as above, then:

if ( $form-submitted  $form-validate ) {
   $c-stash-{user} = $user;
   $c-forward('do_edit');
}

sub do_edit : Private {
   my ($self, $c) = @_;
   my $form = $self-formbuilder;
   my $fields = $form-field;
   $c-stash-{user}-update($fields);
   $c-flash-{status_msg} = 'User updated';
   $c-response-redirect( $c-req-base . 'users/view/' .
 $c-stash-{user}-id );
}

Not sure if it's the 'best' way, but it works. Will be interesting to
see what other suggestions you get.


It might be worth looking at the code for DBIx::Class::HTMLWidget for some ideas
http://search.cpan.org/src/ANDREMAR/DBIx-Class-HTMLWidget-0.09/lib/DBIx/Class/HTMLWidget.pm

When you're populating the form from the database, would your
   $form-values($widget-get_columns);
correctly set select menus?

Also, (for all browsers that I'm aware of) no value is sent back to
the server for unchecked radioboxes - so it'd be worth checking
whether the $fb-field solution handles this properly.
(I don't know, as I've only read the formbuilder docs, I've never used it).

Cheers,
Carl

___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


[Catalyst] best way to create a flexible, multiple key, CRUD application?

2007-02-18 Thread Francesc Romà i Frigolé

Hello,

I want to create a web application which is basically CRUD, and I was
wondering if there is a scaffolding script that can do it for catalyst. It
has to support tables with composite primary keys.

I thought InstantCRUD would be a good solution but i quickly discovered that
it doesn't handle composite primary keys ( it actually says it on the
documentation ). I naively thought that it wouldn't take much time to add
such functionality, and I started preparing a patch for it. I actually
managed it to work for Edit/Update and Delete (I think so), but I got stuck
with the Create part. The problem is, at least, that InstantCRUD depends on
DBSchema.pm which has the same limitation.

Now I got the feeling I'm wasting my time because somebody must already have
a solution for this problem, right?

BTW: I've seen a lot of talk about Reaction on the list recently but I
haven't quite got what is it about, and apparently is undocumented. Does it
have any relation with CRUD?

thanks in advance,
Francesc
___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Using C::C::FormBuilder With DBIC

2007-02-18 Thread Mark Zealey
wrt radioboxes on forms/dbic; I have been doing a true/false/null using the 
following:

invited:
label: Invited?
options: 1=Yes, 0=No
required: 0

As this is not required, if nothing is sent back the value will be undef = 
null; otherwise 1/0 will be returned based on yes/no response. To remove 
null, simply set required to 1.

re the easy solutions presented earlier in the thread for sticking a form into 
a db and back again; I don't ususally make the code that simple because it 
could open up injection attacks and doesn't work too well with more complex 
forms or fields. I usually explicitly list which fields i want to use so then 
a typo in the form or a forgotten/newly added field in the form will not 
allow remote users to mess with bits of the database you don't want them to 
mess with. I guess you could probably use db column permissions to do that 
db-side though.

Mark

On Sunday 18 February 2007 1:01 pm, Carl Franks wrote:
 On 18/02/07, RA Jones [EMAIL PROTECTED] wrote:
  [EMAIL PROTECTED] wrote:
   Hi Everyone,
  
   I have been experimenting with Catalyst::Controller::FormBuilder.  I'm
   trying to provide an edit form for objects read from and written to the
   DB via DBIx::Class.  Have FormBuilder automatically load the data from
   DBIC into the form seems pretty easy with something like:
  
   my $widget = $c-model('MyAppDB::Widget')-find($id);
   my $form = $self-formbuilder;
   $form-values($widget-get_columns);
  
  
   However, an elegant way of writing the data back out is not jumping out
   at me.  I know I could manually copy each field out of the $form object
   to my $widget object, but it seems like there should be simple way to
   do it all in one line like I got with the
   $form-values($widget-get_columns); above.
  
   Any suggestions?  What are other folks doing for C::C::FormBuilder and
   DBIC?
 
  I'm pretty new to this, but this is what I did:
 
  sub edit : Local Form {
 
 # get data into form pretty much as above, then:
 
  if ( $form-submitted  $form-validate ) {
 $c-stash-{user} = $user;
 $c-forward('do_edit');
  }
 
  sub do_edit : Private {
 my ($self, $c) = @_;
 my $form = $self-formbuilder;
 my $fields = $form-field;
 $c-stash-{user}-update($fields);
 $c-flash-{status_msg} = 'User updated';
 $c-response-redirect( $c-req-base . 'users/view/' .
   $c-stash-{user}-id );
  }
 
  Not sure if it's the 'best' way, but it works. Will be interesting to
  see what other suggestions you get.

 It might be worth looking at the code for DBIx::Class::HTMLWidget for some
 ideas
 http://search.cpan.org/src/ANDREMAR/DBIx-Class-HTMLWidget-0.09/lib/DBIx/Cla
ss/HTMLWidget.pm

 When you're populating the form from the database, would your
 $form-values($widget-get_columns);
 correctly set select menus?

 Also, (for all browsers that I'm aware of) no value is sent back to
 the server for unchecked radioboxes - so it'd be worth checking
 whether the $fb-field solution handles this properly.
 (I don't know, as I've only read the formbuilder docs, I've never used it).

 Cheers,
 Carl

 ___
 List: Catalyst@lists.rawmode.org
 Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
 Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
 Dev site: http://dev.catalyst.perl.org/

___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Question on serving files

2007-02-18 Thread Chris


You can set the header information to give mime hints to the browser:

  $c-res-headers-content_type('application/octet-stream');
$c-res-headers-content_length( $stat-size );
$c-res-headers-last_modified( $stat-mtime );
$c-response-headers-header(
'Content-disposition:' = attachment; filename=$filename );
$c-res-headers-expires( time() );
$c-res-headers-header( 'Pragma'= 'no-cache' );
$c-res-headers-header( 'Cache-Control' = 'no-cache' );




Interestingly, if you set 'no-cache' on file downloads over an SSL
(https) connection where IE has a mime-handler defined (word/xls/pdf),
will throw spurious errors about being unable to cache the file, even
when trying to 'save target as'.

This isn't a catalyst issue, but an IE 'feature'- might be useful to
know before it bites, though.

- Chris

___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


[Catalyst] CRUD Question. How to update a record.

2007-02-18 Thread Hans Ophüls
Hello,

I have just started with catalyst.
I have worked through the tutorial
and every thing works fine yet.

But I have a problem with implementing an
update function.
It schould select a record from the database
and insert the data into a widget.

sub hw_update : Local {
my ($self, $c, $id) = @_;

# Search for the product 
c-model('MyAppDB::Product')-search({id = $id});

# Create the widget and set the action for the form
my $w = $self-make_product_widget($c);
$w-action($c-uri_for('hw_create_do'));



sub make_product_widget {
my ($self, $c) = @_;

# Create an HTML::Widget to build the form
my $w = $c-widget('product_form')-method('post');

# Create the form fields
my $e = $w-element('Textfield', 'itemid'  )-label('ItemId')-size(30);
$e-value('4711');

Just where there is '4711' should be the value from the column 'itemid'.
I have tried various expression with
$c-model('MyAppDB::Product')...
but I have not found the right way.

Can anybody help please.

Hans


___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] best way to create a flexible, multiple key, CRUD application?

2007-02-18 Thread Francesc Romà i Frigolé

Mark,

Your words are very encouraging: I'll happily trade 50% more work for
getting rid of the black magic ( and get some peace of mind )

I'll start over with your examples.Thank you very much,
Francesc



On 2/18/07, Mark Zealey [EMAIL PROTECTED] wrote:


I looked at the crud modules but i could never really get them to work;
they
were always rather too limited for me and if you ever want to extend it
slightly; you will have to rewrite it all yourself. It's easy enough to do
a
simple crud app yourself; you want to use DBIx::Class probably with the
Schema::Loader module at least during development. use the FormBuilder
controller base class, with .fb file like:

name: invite
method: post
fields:
name:
label: Name
required: 1
number:
label: Number of people
validate: INT
value: 1
required: 1
address:
label: Postal address
type: textarea
rows: 6
cols: 40
required: 0
email:
label: Email Address
required: 0
coming:
label: Coming?
options: 1=Yes, 0=No
required: 0


and then just write a little glue code; something like:

package Zealey::Controller::Invite;
use strict;
use warnings;

use base 'Catalyst::Controller::FormBuilder';

sub index : Private {
my ($self, $c) = @_;
$c-res-redirect($c-uri_for('view'));
}

sub add : Local Form('/invite/main') {
my ($self, $c) = @_;

my $form = $self-formbuilder;

$form-submit('Add new invite');

if($form-submitted and $form-validate) {
$c-model('Zealey::Invite')-create($form-fields)
$c-res-redirect($c-uri_for('view'));
}
}

sub update : Local Form('/invite/main') {
my ($self, $c, $id) = @_;

my $form = $self-formbuilder;

$form-submit('Update invite');

my $db = $c-model('Zealey::Invite')-find($id) or die $id not
found;

if($form-submitted) {
if ($form-validate) {
$db-set_columns($form-fields);
$db-update;
$c-res-redirect($c-uri_for('view'));
}
} else {
   # Fill in form
$form-field( name = $_, value = $db-$_ ) for @DB_COLS
}
}

sub delete : Local {
my ($self, $c, $id) = @_;

my $db = $c-model('Zealey::Invite')-find($id) or die Not found;
$db-delete;
$c-res-redirect($c-uri_for('view'));
}

sub view : Local {
my ($self, $c) = @_;

$c-stash-{rows} = [ $c-model('Zealey::Invite')-all ];
}

1;

I use a View::TT module; you just stick [% FormBuilder.render %] in the
add/update files; and some sort of table renderer for each column in the
view
one iterating over the 'rows' variable.

Admittedly; this is probably 50% more complicated than setting up
InstantCRUD
or the HTML::Widget::DBIC module, but it is very easily extendable as it
is
not really tied to any particular framework - it would be trivial for
example
to change it to link across 2 tables by simply issuing the create/update
statements and using foreign key constraints. It's much less
black-magic'y;
it's easy to see everything that's happening but at the same time not see
the
bits you don't really care about.

Mark



On Sunday 18 February 2007 5:41 pm, Francesc Romà i Frigolé wrote:
 Hello,

 I want to create a web application which is basically CRUD, and I was
 wondering if there is a scaffolding script that can do it for catalyst.
It
 has to support tables with composite primary keys.

 I thought InstantCRUD would be a good solution but i quickly discovered
 that it doesn't handle composite primary keys ( it actually says it on
the
 documentation ). I naively thought that it wouldn't take much time to
add
 such functionality, and I started preparing a patch for it. I actually
 managed it to work for Edit/Update and Delete (I think so), but I got
stuck
 with the Create part. The problem is, at least, that InstantCRUD depends
on
 DBSchema.pm which has the same limitation.

 Now I got the feeling I'm wasting my time because somebody must already
 have a solution for this problem, right?

 BTW: I've seen a lot of talk about Reaction on the list recently but I
 haven't quite got what is it about, and apparently is undocumented. Does
it
 have any relation with CRUD?

 thanks in advance,
 Francesc

___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/