Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

(solved) Reading all form-fields


Go to End


6 Posts   2077 Views

Avatar
Beppo98

Community Member, 19 Posts

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

Avatar
swaiba

Forum Moderator, 1899 Posts

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

Avatar
Beppo98

Community Member, 19 Posts

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

Avatar
ajshort

Community Member, 244 Posts

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.

Avatar
Beppo98

Community Member, 19 Posts

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

Avatar
Beppo98

Community Member, 19 Posts

19 March 2011 at 5:06am

Edited: 19/03/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
   }
   }
}