[Catalyst] FormFu: How to ensure a field value is unique?

Bjørn-Helge Mevik bhx6 at mevik.net
Sun Jun 2 11:33:22 GMT 2013


Dear all,

I'm writing a Catalyst application that uses FormFu
(Catalyst::Controller::HTML::FormFu), and need to ensure that a supplied field
value ("brukernavn" -- user name) is uniqueue, i.e., that it does _not_ appear
in a table in the data base.

I discovered HTML::FormFu::Constraint::DBIC::Unique, but haven't been able to
get it to work.  I've tried the following:

In the controller:

package Opusmedl::Controller::Medlemmer;
use parent 'Catalyst::Controller::HTML::FormFu';
[...]
# The function for registering a new person:
sub registrer :Local :FormConfig {
    my ($self, $c) = @_;

    # Get the form that the :FormConfig attribute saved in the stash
    my $form = $c->stash->{form};

    # Check if the form as been submitted (vs. displaying the initial
    # form) and if the data based validation.  "submitted_and_valid"
    # is shorthand for "$form->submitted && !$form->has_errors"
    if ($form->submitted_and_valid) {
        # Create a new person
        my $person = $c->model('DB::Opusmedl2')->new_result({});
        # Save the form data for the person
        $form->model->update($person);
	# Register the initial status
        my $medlstatus = $c->model('DB::Opusstatus')->new_result({
	    medl_id => $person->medl_id()
	    });
        $form->model->update($medlstatus);

        # Set a status message for the user and return to medlemslista.
        $c->flash->{status_msg} = 'Person registrert';
	$c->response->redirect($c->uri_for('liste'));
    }
}

In the form file:
[...]
elements:
[...]
    - type: Text
      name: brukernavn
      label: Brukernavn på internsidene
      comment: (på formen BrukerNavn, uten æøå e.l.)
      size: 20
      constraints:
          - type: Required
            when:
              field: status
              values: [ 'prosjekt', 'prøve', 'medlem' ]
          - type: Length
            max: 30
            message: Brukernavnet må være maks 30 tegn
          - type: Regex
            regex: ^([A-Z]+[a-z]+)+[A-Z]*$
            message: Må bestå av bokstavene A-Z/a-z og være på formen FornavnEtternavn, FornavnMellomnavnEtternavn, e.l.
          - type: DBIC::Unique
            model: DB::OpusTwikiBrukernavn
            column: brukernavn

I have tried with different values for "model": DB::OpusTwikiBrukernavn,
Opusmedl::Schema::OpusTwikiBrukernavn and Opusmedl::Model::DB, but they all
give the same result.  (Opusmedl is the name of the application, DB.pm is the
Model module, and OpusTwikiBrukernavn is the name of the schema module
impelemnting the table in question.  The table is named
"opus_twiki_brukernavn" in the SQL db, and has one coloumn, "brukernavn".

The lib/Opusmedl/Schema/OpusTwikiBrukernavn.pm was created with the
auto-generated opusmedl_create.pl script.  It contains:

package Opusmedl::Schema::OpusTwikiBrukernavn;
[...]
__PACKAGE__->table("opus_twiki_brukernavn");
__PACKAGE__->add_columns(
  "brukernavn",
  { data_type => "varchar", is_nullable => 0, size => 30 },
);
[...]


I start the test server like this:

APP_TEST=1 DBIC_TRACE=1 ./script/opusmedl_server.pl -p 4000 -r -d

(the APP_TEST is for getting a separate test configuration.)  

When submitting the form, I get the following output on the terminal, whether
the supplied value exists in the table or not:

Use of uninitialized value $name in pattern match (m//) at /home/bhm/perl5/lib/perl5/HTML/FormFu/Element.pm line 71.
Use of uninitialized value $root in hash element at /home/bhm/perl5/lib/perl5/HTML/FormFu/Role/NestedHashUtils.pm line 43.
'resultset' is not defined at /home/bhm/perl5/lib/perl5/HTML/FormFu.pm line 498.
[info] *** Request 17 (0.034/s) [16804] [Mon May 27 20:14:27 2013] ***
[debug] Path is "medlemmer/registrer"
[debug] "POST" request for "medlemmer/registrer" from "127.0.0.1"
[debug] Body Parameters are:
.-------------------------------------+--------------------------------------.
| Parameter                           | Value                                |
+-------------------------------------+--------------------------------------+
| action                              | Registrer person                     |
| brukernavn                          | UbruktBrukernavn                     |
| dato                                |                                      |
| epost                               |                                      |
| etternavn                           | Etternavn                            |
| fdato                               |                                      |
| fornavn                             | Fornavn                              |
| gateadr                             |                                      |
| instrument                          | annet                                |
| kommentar                           |                                      |
| listeepost                          |                                      |
| medltype                            | vanlig                               |
| postnr                              |                                      |
| poststed                            |                                      |
| rekr_kommentar                      |                                      |
| rekruttering                        | nei                                  |
| status                              | ekstern                              |
| telefon                             |                                      |
'-------------------------------------+--------------------------------------'


Can anyone shed any light on what I do wrong?

-- 
Regards,
Bjørn-Helge Mevik



More information about the Catalyst mailing list