CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Arbitrary number of elements in buildForm hook
Pages: [1]

Author Topic: Arbitrary number of elements in buildForm hook  (Read 1026 times)

qjensen

  • I post frequently
  • ***
  • Posts: 141
  • Karma: 11
    • Scaled Solutions
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Arbitrary number of elements in buildForm hook
June 02, 2009, 01:31:30 pm
Hello All,

I am working on customizing a form for a client, and it means that I have to be able to add an arbitrary number of select fields to the underlying form. The use case is that I am allowing a person signing up for membership to join a chapter at the same time. I am indicating chapters by assigning "Chapter" to the contact sub type field in the db, and I can get the list easily using the api.

I can create a group of radio buttons that lists the various chapters, but there are options for signing up that the client wants presented as select menus. I can't seem to figure out how to add an arbitrary number of select fields to the form. I tried to add the select objects to an array and have the array processed in a custom template file, but Smarty complained that it couldn't handle an array of Quick Form select objects.

Has anyone done something like this in the past? Any pointers are greatly appreciated.

Thanks,
Quint
Quint Jensen
Web Developer
Scaled Solutions
--" Remove the 'I want you to like me' notes from your forehead and place them where they truly will do the most good -- on your mirror!" Author Unknown

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Arbitrary number of elements in buildForm hook
June 02, 2009, 02:20:58 pm

here is one potential solution: (what we mainly use)

1. Create form elements in QF as you normally do. At the same time, also add the form element names to a smarty array, call it elements

2. assign the elements to smarty ($this->assign( 'elements', $elements );)

3. in the template, loop on the elements and render each element

Code: [Select]
            {foreach from=$elements item=element}
            <tr>
                <td class="label">{$form.$element.label}</td><td>{$form.$element.html}</td>
            </tr>
            {/foreach}

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

qjensen

  • I post frequently
  • ***
  • Posts: 141
  • Karma: 11
    • Scaled Solutions
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Arbitrary number of elements in buildForm hook
June 02, 2009, 06:34:03 pm
Hey Lobo - thanks for the pointer. I can't pass an array of select objects directly, but I can index into the array with the entry name and use a loop in smarty to process the array. It worked like a charm  :D
Quint Jensen
Web Developer
Scaled Solutions
--" Remove the 'I want you to like me' notes from your forehead and place them where they truly will do the most good -- on your mirror!" Author Unknown

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Arbitrary number of elements in buildForm hook

This forum was archived on 2017-11-26.