 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
dgs
Joined: 13 May 2008 Posts: 20
|
Posted: Tue May 13, 2008 10:41 pm Post subject: Solved: QuickForm removeElement method on group item |
|
|
I am very new to object oriented programming, and am trying to figure out how to remove elements in a group, or actually the whole group. For example:
$buttons[] = &HTML_QuickForm::createElement('reset', 'submit_reset', 'Reset');
$buttons[] = &HTML_QuickForm::createElement('submit', 'submit_request', 'Submit Request');
$form->addGroup($buttons, null, null, ' ');
and then :
$form->removeElement('submit_reset');
does not work, as I would expect, because it is in an array, but how do I do it?
thanks in advance
Last edited by dgs on Wed May 14, 2008 6:45 pm; edited 1 time in total |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1011
|
Posted: Tue May 13, 2008 11:19 pm Post subject: |
|
|
removeElement() indeed doesn't help you for groups. At first, you'll need a reference to the group element. This can be done either by "remembering" the object, e.g:
| Code: |
$group = $form->addGroup($buttons, null, null, ' ');
|
or by a getElement() call, e.g.:
| Code: |
$group = $form->getElement('nameOfYourGroup'); // name isn't assigned in your example
|
$group has now a getElements() method that returns an array of the group elements:
| Code: |
$elements = $group->getElements();
|
Removing the element from the group is now only a usual array operation.
(If you're using PHP 4, you'll need to add some "&".) |
|
| Back to top |
|
 |
dgs
Joined: 13 May 2008 Posts: 20
|
Posted: Wed May 14, 2008 2:18 am Post subject: |
|
|
Thanks for the reply, I tried the following, and the buttons still show up. There must be something, I don't get. I am using php 5, by the way.
| Code: | <?php
// Include HTML_QuickForm
require_once 'HTML/QuickForm.php';
// Instantiate our form, called 'Create'
$form = new HTML_QuickForm('Create', 'POST');
// Don't forget the Submit button!
$buttons[] = &HTML_QuickForm::createElement('reset', 'submit_reset', 'Reset');
$buttons[] = &HTML_QuickForm::createElement('submit', 'submit_request', 'Submit Request');
$group = $form->addGroup($buttons, null, null, ' ');
$elements = $group->getElements();
// Remove unnecessary elements and show result
print_r ($elements);
unset($elements[0]);
unset($elements[1]);
print_r ($elements);
// Process Form
if ($form->validate())
{
// Form Validates
// Handle Form here
$form->freeze();
$formsource = $form->toHtml();
}
else
{
// Form has not yet been submitted, or is invalid, show form
$formsource = $form->toHtml();
}
echo $formsource;
?> |
I realize this is a dumb example, but I wanted to simplify it to post.
thanks,
Last edited by dgs on Wed May 14, 2008 6:29 pm; edited 1 time in total |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1011
|
Posted: Wed May 14, 2008 6:17 pm Post subject: |
|
|
Please use the [code]...[/code] environment next time, this makes the code more readable.
I made a mistake in my explanation: The getElements() call needs the &. This ensures a reference, and your example works then as expected.
BTW, the example isn't dumb. Such short examples without any unneeded things are very welcome. |
|
| Back to top |
|
 |
dgs
Joined: 13 May 2008 Posts: 20
|
Posted: Wed May 14, 2008 6:46 pm Post subject: |
|
|
| That was it. Thanks, very much. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|