root/lang/perl/DBIx-Class-FromValidators/trunk/lib/DBIx/Class/FromValidators.pm @ 1808

Revision 1808, 3.0 kB (checked in by woremacx, 7 years ago)

lang/perl/DBIx-Class-FromValidators?: fixes around packaging

Line 
1package DBIx::Class::FromValidators;
2use strict;
3use warnings;
4use base 'DBIx::Class';
5use Carp::Clan qw/^DBIx::Class/;
6use Scalar::Util qw(blessed);
7
8our $VERSION = '0.02';
9
10{
11    package
12        DBIx::Class::ResultSet;
13    use strict;
14    use warnings;
15    use Carp::Clan qw/^DBIx::Class/;
16    use Scalar::Util qw(blessed);
17
18    sub create_from_fv {
19        my ($self, $results, $args) = @_;
20        croak "pass me a form results object" unless blessed($results);
21        croak "pass me a object which can call 'success' and 'valid'"
22            unless $results->can('success') and $results->can('valid');
23        croak "has error on form" unless $results->success;
24
25        my $cols = $args || {};
26
27        for my $col ($self->result_source->columns) {
28            next if defined($cols->{$col});
29
30            my $val;
31            if (defined($val = $results->valid($col))) {
32                $cols->{$col} = $val;
33            } elsif (defined($val = $args->{$col})) {
34                $cols->{$col} = $val;
35            }
36        }
37
38        return $self->create($cols);
39    }
40}
41
42sub update_from_fv {
43    my ($self, $results, $args) = @_;
44    croak "pass me a form results object" unless blessed($results);
45    croak "pass me a object which can call 'success' and 'valid'"
46        unless $results->can('success') and $results->can('valid');
47    croak "has error on form" unless $results->success;
48
49    my $cols = $args || {};
50
51    for my $col ($self->result_source->columns) {
52        next if defined($cols->{$col});
53
54        my $val;
55        if (defined($val = $results->valid($col))) {
56            $cols->{$col} = $val;
57        } elsif (defined($val = $args->{$col})) {
58            $cols->{$col} = $val;
59        }
60    }
61
62    return $self->update($cols);
63}
64
651;
66__END__
67
68=head1 NAME
69
70DBIx::Class::FromValidators - Update or Insert DBIx::Class data from Validators
71
72=head1 SYNOPSIS
73
74    # in your Schema class
75
76    package
77        Test::Schema;
78    use base qw( DBIx::Class::Schema::Loader );
79
80    __PACKAGE__->loader_options(
81        components    => [ qw( FromValidators ) ],
82    );
83
84    # in your Catalyst controller
85
86    $c->form;
87    $c->model('DBIC::Member')->create_from_fv($c->form,
88        {
89            # extra stuff
90            name  => "woremacx",
91            email => "woremacx at gmail",
92        }
93    );
94
95    $c->model('DBIC::Member')->search({ name => "woremacx" })->first->update_from_fv($c->from,
96        {
97            # extra stuff
98            email => 'woremacx plus vagina at gmail',
99        }
100    );
101
102=head1 DESCRIPTION
103
104DBIx::Class::FromValidators allows you to Update or Insert DBIx::Class objects
105from FormValidator::Simple or Data::FormValidator.
106
107=head1 METHODS
108
109=head2 create_from_fv
110
111call DBIC's create method.
112
113=head2 update_from_fv
114
115call DBIC's update method.
116
117=head1 AUTHOR
118
119woremacx E<lt>woremacx at cpan dot orgE<gt>
120
121=head1 LICENSE
122
123This library is free software; you can redistribute it and/or modify
124it under the same terms as Perl itself.
125
126=head1 SEE ALSO
127
128L<DBIx::Class::WebForm>, L<DBIx::Class::FromSledge>, L<FormValidator::Simple>, L<Catalyst::Plugin::FormValidator::Simple>
129
130=cut
Note: See TracBrowser for help on using the browser.