On 9 September 2010 14:32, Carl Franks <fireart...@gmail.com> wrote: > On 9 September 2010 12:01, Tim Rayner <tfray...@gmail.com> wrote: >> >> I've just found what I think are a couple of typos in the recent >> changes to H::F::Constraint::DBIC::Unique. The attached patch >> illustrates and fixes the problem. > > Hi, > Thanks for this - though do notice that Constraint::DBIC::Unique is > listed in MANIFEST.SKIP, meaning it won't be included in any cpan > releases. > The reason for this is simply that there isn't any tests for it - once > there are, it can be included. > >> Incidentally, would there be any >> interest in me adding in a couple of our local improvements to this >> module, and maybe working up a test suite? > > I'd recommend describing to us the changes first, so you can get > feedback on them. > They won't be included until there's tests for them, so you don't want > to waste time if they don't get a positive feedback.
Hi, That's fair enough. I guess the addition I'd really like to see is a method allowing the user to specify more than one column to check against, in cases where the database table has a compound unique key. Charlie's "method_name" argument might well be a better way of doing this, but in the attached example patch this method is called "others" for the sake of argument. Alternatively it could be modified such that the column method accepts an arrayref; I'm open to suggestions and/or opinions. If this is already covered by a patch in the pipeline then that's fine by me; the offer to work up a test suite still stands, though, since I'd like to see this module released to CPAN so I don't have to maintain a local fork. Cheers, Tim
--- ../../src/svn/html-formfu-read-only/HTML-FormFu-Model-DBIC/lib/HTML/FormFu/Constraint/DBIC/Unique.pm 2010-09-09 11:53:36.000000000 +0100 +++ lib/HTML/FormFu/Constraint/DBIC/Unique.pm 2010-09-09 14:54:14.000000000 +0100 @@ -6,14 +6,14 @@ use Carp qw( carp croak ); -__PACKAGE__->mk_accessors(qw/ model resultset column self_stash_key /); +__PACKAGE__->mk_accessors(qw/ model resultset column self_stash_key others /); sub constrain_value { my ( $self, $value ) = @_; return 1 if !defined $value || $value eq ''; - for (qw/ resultset column /) { + for (qw/ resultset /) { if ( !defined $self->$_ ) { # warn and die, as errors are swallowed by HTML-FormFu carp "'$_' is not defined"; @@ -51,12 +51,22 @@ } my $column = $self->column || $self->parent->name; + my %others; + if ( $self->others ) { + my @others = ref $self->others ? @{ $self->others } + : $self->others; + + my $param = $self->form->input; + %others = map { $_ => $param->{$_} } + grep { defined $param->{$_} && $param->{$_} ne q{} } @others; + + } my $existing_row = eval { - $resultset->find( { $column => $value } ); + $resultset->find( { %others, $column => $value } ); }; - - if ( defined( my $error = $@ ) ) { + + if ( my $error = $@ ) { # warn and die, as errors are swallowed by HTML-FormFu carp $error; croak $error; @@ -130,7 +140,7 @@ - Required - type: DBIC::Unique model: DBIC::User - field: username + column: username =head1 DESCRIPTION @@ -147,12 +157,35 @@ Arguments: $string # a DBIC resultset name like 'User' -=head2 myself +=head2 self_stash_key reference to a key in the form stash. if this key exists, the constraint will check if the id matches the one of this element, so that you can use your own name. +=head2 others + +Use this key to manage unique compound database keys which consist of +more than one column. For example, if a database key consists of +'category' and 'value', use a config file such as this: + + --- + elements: + - type: Text + name: category + label: Category + constraints: + - Required + + - type: Text + name: value + label: Value + constraints: + - Required + - type: DBIC::Unique + resultset: ControlledVocab + others: category + =head2 SEE ALSO Is a sub-class of, and inherits methods from L<HTML::FormFu::Constraint>
_______________________________________________ HTML-FormFu mailing list HTML-FormFu@lists.scsys.co.uk http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu