Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » (solved) Reading all form-fields

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1298 Views
  • Beppo98
    Avatar
    Community Member
    19 Posts

    (solved) Reading all form-fields Link to this post

    Hello,

    I think I have a quite simple problem.

    In a form methode I create a form with a variable count of checkboxes.

    $SkripteAll = DataObject::get('Skript');
    $LastField = 'Nachname';
    foreach($SkripteAll as $SkriptItem)
    {
       $fields->insertAfter(new CheckboxField($SkriptItem->Name, $SkriptItem->Name),$LastField);
       $LastField = $SkriptItem->Name;
    }

    I the submit-method I want to save these information depending on the chosen checkboxes. For example I have 4 checkboxes and the user marked three of the four checkboxes I need to save three records.

    But now I have the problem that I don't know in the submit-method how I can get the checkboxes with the value true. Because in this method I have to decide if the record has to be saved or not.

    My first try is that

    function doSubmit($data, $form) {
    foreach ($form->fields as $FieldItem)
    {
       //Here I have to decide if the $FieldItem is a checkbox

       //Furthermore I have to decide if the checkbox is true
    }
    }

    Best regards, Beppo

  • swaiba
    Avatar
    Forum Moderator
    1785 Posts

    Re: (solved) Reading all form-fields Link to this post

    somethign like...

    function doSubmit($data, $form) {
    if (isset($data['name_of_checkbox_field'])) {
    //do a save
    }}
    if (isset($data['name_of_other_checkbox_field'])) {
    //do a save
    }}

    if you are repeating the action several times it might be worth putting the the "do a save" bit into a function

  • Beppo98
    Avatar
    Community Member
    19 Posts

    Re: (solved) Reading all form-fields Link to this post

    Thank you for your answer, but I don't know the names of the checkbox fields, because the data come from the database. (see code sample 1). I also don't know the count of the checkbox fields.

    Best regards, Beppo

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: (solved) Reading all form-fields Link to this post

    You could do something like:

    foreach ($form->Fields() as $field) {
       if ($field instanceof CheckboxField && $field->dataValue()) {
          // do stuff
       }
    }

    But it sounds like what you're trying to achieve would be much better and more easily accomplished using a CheckboxSetField.

  • Beppo98
    Avatar
    Community Member
    19 Posts

    Re: (solved) Reading all form-fields Link to this post

    Thank you for your answer. Now I implemented a CheckboxSetField. Here is the new Code:

    ...
    $fields = new FieldSet(
    new TextField('Vorname', 'Vorname'),
    new TextField('Nachname', 'Nachname')
    );
          
    $All = $this->GetSkripte();

    $fields->insertAfter(new CheckboxSetField('All','', $All),'Nachname');
    ...

    In the submit method I implemented now the following code:

    ...
    foreach ($form->Fields() as $FieldItem) {
    if ($FieldItem instanceof CheckboxSetField) {

    foreach($FieldItem->Fields() as $CheckboxItem){
       
    //Save each element
                
    }
    }
    }
    ...

    Now I think I have a problem with

    foreach($FieldItem->Fields() as $CheckboxItem)

    Because the $FieldItem (CheckboxSetField) doesn't have a method Fields(). How I can read each Checkbox in the CheckboxField?

    Best regards, Beppo

  • Beppo98
    Avatar
    Community Member
    19 Posts

    Re: (solved) Reading all form-fields Link to this post

    Now I solved my problem:

    Here is the solution:

    foreach ($form->Fields() as $FieldItem) {
    if ($FieldItem instanceof CheckboxSetField) {

    foreach(explode(',',$FieldItem->dataValue()) as $SkriptID){
       
    //in $SkriptID is the id of each selected checkbox
    }
    }
    }

    1298 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.