jeudi 24 mai 2018

Checkboxgroup / HTML::FormFu::Model::DBIC / many_to_many selection (Catalyst/Perl)

I have a Checkboxgroup and I want to check the checkboxes based on my mysql db values.

Here's the relevant code of my first table 'dicos':

__PACKAGE__->has_many(
  "favorites",
  "accessidico::Schema::Result::Favorite",
  { "foreign.dico_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

Here's the relevant code of my second table 'favorites':

__PACKAGE__->belongs_to(
  "dico",
  "accessidico::Schema::Result::Dico",
  { id => "dico_id" },
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
);

And my tables descriptions:

'favorites'

+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| user_id | int(11) | NO   | PRI | NULL    |       |
| dico_id | int(11) | NO   | PRI | NULL    |       |
+---------+---------+------+-----+---------+-------+

'dicos'

+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| id         | int(11) | NO   | PRI | NULL    |       |
| name       | text    | YES  |     | NULL    |       |
| name_index | text    | YES  |     | NULL    |       |
+------------+---------+------+-----+---------+-------+

I want to have the labels of the checkboxes corespond to the 'name' of my dicos table and check the values based on the 'dico_id' of the 'favorites' table. The table 'favorites' is based on the current user of the web-app and his selection of favorites dictionnaries.

So far, the behaviour is: I have my checkboxes checked based on 'dico_id' but the labels correspond to the column 'dico_id' of 'favorites'. For example with this favorites table:

+---------+---------+
| user_id | dico_id |
+---------+---------+
|       1 |       2 |
|       2 |       1 |
|       2 |       2 |
+---------+---------+

If the current user have his user_id=2, my checkboxes look like this: []2 [X]1 [X]2

FormFu code (index.yml):

---
indicator: submit
action: /update_fav
elements:
    - type: Checkboxgroup
      name: dico_id
      model_config:
        model: DB
        resultset: Favorite
        default_column: dico_id 
        label_column: dico_id
    - type: Submit
      name: submit
      value: action

I tried different things with 'default_column' and 'label_column' with no success to display what I wanted.

Controller code (Favorites.pm):

sub index :Path :Args(0) :FormConfig {
    my ( $self, $c ) = @_;
    my $form = $c->stash->{form};
    my $user = $c->user->get('id');

    my @checked_objs =  $c->model('DB::Favorite')->search({user_id => $user});
    $form->process();

    if (!$form->submitted){
        $form->model->default_values(@checked_objs);
    }
    $c->stash(template => 'favorites.tt');
    $c->forward('View::HTML');
}

I also tried to change the resultset in the FormFu code:

---
indicator: submit
action: /update_fav
elements:
    - type: Checkboxgroup
      name: dico_id
      model_config:
        model: DB
        resultset: Dico
        default_column: name 
        label_column: name
    - type: Submit
      name: submit
      value: action

Again, I tried different things with 'default_column' and 'label_column'... Like this I have my labels corresponding to 'dicos' 's name but I only succeed to get one checkbox checked even when there is should be more. The controller code is the same.




Aucun commentaire:

Enregistrer un commentaire