correction:
you can pass a reference to $c->stash(template => \'foobas'), so you end up
with:
 $c->stash( template => \$form->render, current_view => 'Ajax', );

On Mon, Sep 20, 2010 at 8:09 PM, Hernan Lopes <[email protected]> wrote:

> perl Catalyst Forms with Formhandler and Thickbox:
>
> You know you dont always need a template.tt2 file... this is useful with
> ajax (especially with thickbox) so you can pass a scalar as template, so you
> end up with:
>  $c->stash( template => $form->render, current_view => 'Ajax', );
> 1. You could create an Ajax view.
> 2. Or you can set body content directly.
> This way you skip creating TT files for forms and render them directly into
> the $c->res->body or $c->stash( template => \'foo baz')
> If you use something like thickbox, you can render any $form directly into
> a modal.
> Its very handy!
>
> For example:
>
>
> __PACKAGE__->config(
>     action => {
>         edit => { Chained => 'base', Args => 1, },
>     },
> );
>
> sub base : Chained('/') PathPart('foobar') CaptureArgs(0) {}
>
> sub edit :Action {
>   my ( $self, $c, $foobar_id ) = @_;
>   my $form = HTML::FormHandler->new(
>     schema => 'DBSchema::Foo',
>     params => $c->req->params,
>     field_list => $self->form_fields($c),
>       );
>     if( $c->req->method eq 'POST' && $form->process() ) {
>       ...
>       } else {
>   #OPTION 1 (create an Ajax View and create a wrapper for it. Then render
> the form into stash template var):
>         $c->stash(
>           template => \$form->render,
>           current_view => 'Ajax',
>           );
>   #OPTION 2 (set your content type and charset and render the form into the
> body, needs no view/TT files):
>         $c->res->content_type('text/html charset=utf-8');
>         $c->res->body($form->render);
>         }
>   }
>
>
>
>
> sub form_fields {
>     return [
>         field_one => {
>         type => 'Text',
>         label => '...',
>         css_class => '...',
>         maxlength => 160,
>         required => 1,
>         required_message => 'Required Text',
>         },
>         submit => {
>           type => 'Submit',
>           value => 'Save',
>           css_class => '...',
>          },
>     ];
> }
>
>
>
> and DRY on edit & update unless necessary...
> 1. If there is an argument its "update?"
> 2. Else, when it has no args then its "edit/new" ?
>
> use your foregin_key_id and $form('foregin_key_id')->value = '...' to set
> it , then formhandler will know whether to update or create a new entry.
>
> Take care,
> hernan
>
>
>
>
>
>
> On Mon, Sep 20, 2010 at 6:48 PM, E R <[email protected]> wrote:
>
>> Hi,
>>
>> I am curious what everyone thinks as being the best practices for
>> handling forms in Catalyst. Are there any Catalyst applications you
>> have run across which are good examples of how to use Catalyst's
>> features to handle forms?
>>
>> To illustrate what I am getting at, below is typical Rails (v2)
>> controller code which implements updating the attributes of an object:
>>
>>   def edit
>>      @book = Book.find(params[:id])
>>      @subjects = Subject.find(:all)
>>   end
>>   def update
>>      @book = Book.find(params[:id])
>>      if @book.update_attributes(params[:book])
>>         flash[:notice] = 'Book successfully updated.'
>>         redirect_to :action => 'show', :id => @book
>>      else
>>         @subjects = Subject.find(:all)
>>         render :action => 'edit'
>>      end
>>   end
>>
>> In Catalyst, this would be appear something like (and please correct
>> me if I have made any errors here):
>>
>> sub edit  :Args(1) {
>>  my ($self, $c, $id) = @_;
>>  ... set up $c->stash for template 'edit' ...
>>  # no need to set $c->stash->{template} - will be set from the current
>> action
>> }
>>
>> sub update :Args(1) {
>>  my ($self, $c, $id) = @_;
>>  ...process form...
>>  if (form is valid) {
>>    ...perform updates...
>>    $c->flash->{notice} = 'Book successfully updated.';
>>    $c->res->redirect('show', $id);
>>  } else {
>>    ... set up $c->stash for 'edit' template ...
>>    $c->stash->{template} = 'edit';
>>  }
>> }
>>
>> Any comments on this architecture? Is there a better way? My main problems
>> are:
>>
>> 1. The code ... set up $c->stash for 'edit' template ... is duplicated
>> in both edit and update (which is also true for the Rails code).
>>
>> 2. Having the template name defaulted from the current action is nice,
>> but that means we have to explicitly set it in the update method. Is
>> it better to always explicitly set the template name in a controller
>> method? Then update could perform a $c->detach('edit', $id) or would
>> you use $c->go('edit', $id)?
>>
>> Thanks,
>> ER
>>
>> _______________________________________________
>> List: [email protected]
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive:
>> http://www.mail-archive.com/[email protected]/
>> Dev site: http://dev.catalyst.perl.org/
>>
>
>
_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/

Reply via email to