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 
QuickForm2 setting 'selected' item in select list ?

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



Joined: 11 Feb 2008
Posts: 6

PostPosted: Wed Apr 02, 2008 7:30 pm    Post subject: QuickForm2 setting 'selected' item in select list ? Reply with quote

Hi

I'm creating <select> lists fine in QuickForm2 but can't work out how to set one of the items as selected.

Am creating thus:
Code:
   $specialism =    $fsText->addElement(
    'select', 'specialism', null , array('options' => $specialism_options, 'label' => 'Specialism' )
      );
      


populating the data with an array:
Code:
$specialism_options = array(
      'acc' => 'Accountancy', 'off' => 'Office services',  'exec' => 'Executive search'
      );   
      


I'm assuming the selected values is something to do with the $attributes array which I've tried creating in place on the NULL

Code:
 $specialism_att = array('selected' => 'exec');


but this has no affect...

Any ideas? As I can't believe doing something so essential should be so difficult!

Thanks
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Wed Apr 02, 2008 9:37 pm    Post subject: Reply with quote

In HTML_QuickForm a method setSelected() exists, but it is recommended to use $form->setDefaultValues().

According to the phpdoc comment in the HTML_QuickForm2_Element_Select_OptionContainer class, you can use the addOption() method to mark an item as selected:
Code:

   /**
    * Adds a new option 
    *
    * Please note that if you pass 'selected' attribute in the $attributes
    * parameter then this option's value will be added to <select>'s values.
    *
    * @param    string  Option text
    * @param    string  'value' attribute for <option> tag
    * @param    mixed   Additional attributes for <option> tag (either as a
    *                   string or as an associative array)
    */
    public function addOption($text, $value, $attributes = null)


I haven't checked it, but according to what happens in this method, setDefaultValues() should still work in QF2. (This method might have another name in QF2, though.)
Back to top
View user's profile Send private message
roger06



Joined: 11 Feb 2008
Posts: 6

PostPosted: Thu Apr 03, 2008 8:07 pm    Post subject: Reply with quote

thank you...

I'd been looking at this from the wrong angle. I think the QF2 version is addDataSource and I was using that for other inputs - simply not clicking that I need this for <selects> as well!

Cheers - all working now!
Back to top
View user's profile Send private message
Jb boin



Joined: 20 May 2008
Posts: 6
Location: Grenoble, France

PostPosted: Thu May 29, 2008 5:48 pm    Post subject: Reply with quote

I am also looking for a solution but addoption is not a good one as it add a new option instead of just marking as selected an existing one and for adddatasource i havent foud how it could solve the problem.

Does anyone have a solution to put a selected value on QF2?
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Thu May 29, 2008 8:10 pm    Post subject: Reply with quote

You can find an example of using addDataSource() here:
http://www.joomlaportal.de/geloeste-themen-komponenten/126178-submit-und-ausgabe-einer-quickform2-formular-komponente.html
(it's a German forum, but you should get the idea from the example code there)
Back to top
View user's profile Send private message
Jb boin



Joined: 20 May 2008
Posts: 6
Location: Grenoble, France

PostPosted: Fri May 30, 2008 10:12 am    Post subject: Reply with quote

Thanks for the answer but i still dont see why and how to use adddatasource for this job, i found a workaround but its unelegant :

Code:
function selected($object, $array, $selected) {
   foreach ($array as $id => $name) {
      if ($id == $selected) {
       $object->addoption($name, $id, selected);
      } else {
       $object->addoption($name, $id);
   }}
}



And i call it this way :

Code:
$usercountry  = & $fsinfos->addelement('select', 'country', null, array('label' => 'Country:')); selected($usercountry, $countrieslistar, $userar[0][usercountry]);
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Fri May 30, 2008 12:18 pm    Post subject: Reply with quote

Do you have experience with HTML_QuickForm 3.x? Setting default values (including selected values of select boxes) was done via setDefaultValues(). In QF2 this concept was made more flexible by data sources. And therefore, addDataSource() is the way to pass default values to forms with QF2.
Back to top
View user's profile Send private message
Jb boin



Joined: 20 May 2008
Posts: 6
Location: Grenoble, France

PostPosted: Mon Jun 02, 2008 12:07 pm    Post subject: Reply with quote

I never user QF 3.x and for the adddatasource can you give me an example to elect an item on a select because documentation isn't really clear on that.

Thanks by advance.
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