1779 Posts in 582 Topics by 556 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1055 Views |
-
(solved) Reading all form-fields

14 March 2011 at 12:11am
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
-
Re: (solved) Reading all form-fields

14 March 2011 at 12:57am
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
-
Re: (solved) Reading all form-fields

14 March 2011 at 1:00am
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
-
Re: (solved) Reading all form-fields

14 March 2011 at 2:00am
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.
-
Re: (solved) Reading all form-fields

14 March 2011 at 9:38pm
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
-
Re: (solved) Reading all form-fields

19 March 2011 at 5:06am Last edited: 19 March 2011 5:07am
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
}
}
}
| 1055 Views | ||
|
Page:
1
|
Go to Top |



