Wednesday, October 3, 2012

Creating a loop to save form input data with a variable amount of form inputs?

Hi,

Firstly, I'm new here so hopefully I'm posting this in the right area.

I've created a form using cake's form helper. The form consists of checkboxes for each child of a particular record found with a normal find('all') call. Given that I know there are four children for each parent, I've come up with the following code that works perfectly (at least for my skill level it gets the job done lol).

View code to generate form inputs (dynamically generates inputs per child, not limited to 4 children):
<?php foreach($controllerNodes as $controllerNode): ?>
<div class="wrapperFull">
<div class="inputLabel fontSizeDefault fontWeightBold"><?php echo $controllerNode['ControllerNode']['name']; ?></div>
<div class="clearfix">
<?php
foreach($controllerNode['ActionNode'] as $key => $value){
echo $this->Form->input($value['name'].'.'.$controllerNode['ControllerNode']['id'], array(
'type' => 'checkbox',
'after' => '<div class="inputCheckboxLabel fontSizeSmall floatLeft">'.$value['name'].'</div>',
));
}
?>
</div>
</div>
<?php endforeach; ?>
 
 Controller code to save the 4 results I know always exist:
function permissions(){
$id = $this->request->data['Permissions']['id'];
if($this->request->is('post')){
foreach($this->request->data['Create'] as $key => $value){
if($value == 1){
$this->Acl->allow(array('model' => 'Subgroup', 'foreign_key' => $id), array('model' => 'ControllerNode', 'foreign_key' => $key), 'create');
}else{
$this->Acl->deny(array('model' => 'Subgroup', 'foreign_key' => $id), array('model' => 'ControllerNode', 'foreign_key' => $key), 'create');
}
}
foreach($this->request->data['Read'] as $key => $value){
if($value == 1){
$this->Acl->allow(array('model' => 'Subgroup', 'foreign_key' => $id), array('model' => 'ControllerNode', 'foreign_key' => $key), 'read');
}else{
$this->Acl->deny(array('model' => 'Subgroup', 'foreign_key' => $id), array('model' => 'ControllerNode', 'foreign_key' => $key), 'read');
}
}
foreach($this->request->data['Update'] as $key => $value){
if($value == 1){
$this->Acl->allow(array('model' => 'Subgroup', 'foreign_key' => $id), array('model' => 'ControllerNode', 'foreign_key' => $key), 'update');
}else{
$this->Acl->deny(array('model' => 'Subgroup', 'foreign_key' => $id), array('model' => 'ControllerNode', 'foreign_key' => $key), 'update');
}
}
foreach($this->request->data['Delete'] as $key => $value){
if($value == 1){
$this->Acl->allow(array('model' => 'Subgroup', 'foreign_key' => $id), array('model' => 'ControllerNode', 'foreign_key' => $key), 'delete');
}else{
$this->Acl->deny(array('model' => 'Subgroup', 'foreign_key' => $id), array('model' => 'ControllerNode', 'foreign_key' => $key), 'delete');
}
}
}
}
 
 So my goal is to get the controller to generate the above foreach loops dynamically based on the amount of $this->request->data coming in from the form. I could always add another foreach loop for every new input I create down the road, but I much prefer the idea of these loops being generated based on the info from the database (which draws the form, which submits the data).

In all my searching, I've not figured out a way to know the ammount of inputs submitted by the form although I'm sure there's probably an easy answer staring right at me :o

Thanks a lot!

--
Like Us on FacekBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: