PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Solved: QuickForm removeElement method on group item

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML
View previous topic :: View next topic  
Author Message
dgs



Joined: 13 May 2008
Posts: 20

PostPosted: Tue May 13, 2008 10:41 pm    Post subject: Solved: QuickForm removeElement method on group item Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Tue May 13, 2008 11:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
dgs



Joined: 13 May 2008
Posts: 20

PostPosted: Wed May 14, 2008 2:18 am    Post subject: Reply with quote

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, '&nbsp;');
  $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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Wed May 14, 2008 6:17 pm    Post subject: Reply with quote

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
View user's profile Send private message
dgs



Joined: 13 May 2008
Posts: 20

PostPosted: Wed May 14, 2008 6:46 pm    Post subject: Reply with quote

That was it. Thanks, very much.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
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



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software