CatalystX::CRUD::Controller::RHTMLO - Rose::HTML::Objects CRUD controller


CatalystX-CRUD-Controller-RHTMLO documentation Contained in the CatalystX-CRUD-Controller-RHTMLO distribution.

Index


Code Index:

NAME

Top

CatalystX::CRUD::Controller::RHTMLO - Rose::HTML::Objects CRUD controller

SYNOPSIS

Top

 see CatalystX::CRUD::Controller

DESCRIPTION

Top

This is an implementation of CatalystX::CRUD::Controller for Rose::HTML::Objects. It supercedes Catalyst::Controller::Rose for basic CRUD applications.

METHODS

Top

The following methods are new or override base methods.

form( context )

Returns an instance of config->{form_class}. A single form object is instantiated and cached in the controller object. The form's clear() method is called before returning. context object is set in forms's app() method.

NOTE: The form is cleared only the first time form() is called in each request cycle. This is different than the behaviour described in CatalystX::CRUD::Controller.

field_names( context )

Returns an array ref of the field names in form.

all_form_errors

Convenience method for aggregating all form errors. Returns a single scalar string.

form_to_object( context )

Overrides base method.

do_search( context, arg )

Makes form values sticky then calls the base do_search() method with next::method().

AUTHOR

Top

Peter Karman, <karman at cpan dot org>

BUGS

Top

Please report any bugs or feature requests to bug-catalystx-crud-controller-rhtmlo at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CatalystX-CRUD-Controller-RHTMLO. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc CatalystX::CRUD::Controller::RHTMLO

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/CatalystX-CRUD-Controller-RHTMLO

* CPAN Ratings

http://cpanratings.perl.org/d/CatalystX-CRUD-Controller-RHTMLO

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=CatalystX-CRUD-Controller-RHTMLO

* Search CPAN

http://search.cpan.org/dist/CatalystX-CRUD-Controller-RHTMLO

COPYRIGHT & LICENSE

Top


CatalystX-CRUD-Controller-RHTMLO documentation Contained in the CatalystX-CRUD-Controller-RHTMLO distribution.
package CatalystX::CRUD::Controller::RHTMLO;
use strict;
use base qw( CatalystX::CRUD::Controller );
use Carp;
use MRO::Compat;
use mro 'c3';

our $VERSION = '0.20';

sub form {
    my ( $self, $c ) = @_;
    $self->{_form} ||= $self->form_class->new( app => $c );
    $self->{_form}->app($c) unless defined $self->{_form}->app;
    $self->{_form}->clear
        unless $self->{_form}->app->stash->{_form_called}
            ->{ $self->action_namespace }++;
    return $self->{_form};
}

sub field_names {
    my ( $self, $c ) = @_;
    $self->throw_error("context required") unless defined $c;
    return $self->form($c)->field_names;
}

sub all_form_errors {
    my ( $self, $form ) = @_;
    my @err = ( $form->error );
    for my $f ( $form->fields ) {
        push( @err, $f->name . ': ' . $f->error ) if $f->error;
    }
    return join( "\n", grep {defined} @err );
}

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

    my $form      = $c->stash->{form};
    my $obj       = $c->stash->{object};
    my $obj_meth  = $self->init_object;
    my $form_meth = $self->init_form;

    # id always comes from url but not necessarily from form,
    # but in either case, $obj should already have %pk set
    # since it was used in fetch()
    my $id = $c->stash->{object_id};
    my %pk = $self->get_primary_key( $c, $id );

    # initialize the form with the object's values
    # TODO this might not work if the delegate() does not have
    # 1-to-1 mapping of form fields to object methods.
    $form->$form_meth($obj);

    # set param values from request.
    $form->params( $c->req->params );

    # override form's values with those from params
    # no_clear is important because we already initialized with object
    # and we do not want to undo those mods.
    $form->init_fields( no_clear => 1 );

    # return if there was a problem with any param values
    unless ( $form->validate() ) {
        my $err = $self->all_form_errors($form);
        $c->stash( error => $err );    # NOT throw_error()
        $c->log->debug("RHTMLO: form error:\n$err\n")
            if $c->debug;
        $c->stash->{template} ||= $self->default_template;    # MUST specify
        return 0;
    }

    # re-set object's values from the now-valid form
    # TODO this might not work if the delegate() does not have
    # 1-to-1 mapping of form fields to object methods.
    # this is same objection as $form_method call above
    $form->$obj_meth($obj);

    return $obj;
}

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

    # make form sticky
    $c->stash->{form} ||= $self->form($c);

    # if we have no input, just return for initial search
    if ( !@arg && !$c->req->param && $c->action->name eq 'search' ) {
        $c->log->debug("no input to search. return") if $c->debug;

        # must clear explicitly since this is a new search
        # and form may have been initialized elsewhere
        $c->stash->{form}->clear;
        $c->log->debug("rhtmlo form cleared") if $c->debug;
        return;
    }

    $c->stash->{form}->params( $c->req->params );
    $c->stash->{form}->init_fields();

    return $self->next::method( $c, scalar $self->field_names($c), @arg );
}

1;

__END__