Page 1 of 1

Form Builder - Year pulldown

Posted: Mon Mar 13, 2017 12:21 am
by chips
Hi. I'm running CMS Made Simple 2.1.6. PHP version is 5.5.38 I am running FormBuilder 0.8.1.6 and CMSMailer 6.2.14

I tried using the year pulldown type in FormBuilder and specified the end year to select from. The year pulldown works with allowed selection from current year descending to the declared end year.

Is there a way for the year pulldown selection to start in the future (i.e. beyond current year)? Has anyone come across a similar situation?

Thanks for any feedback.

Re: Form Builder - Year pulldown

Posted: Tue Mar 14, 2017 12:23 pm
by paulbaker
One way, I suppose, would be to use a standard pull down field and manually enter the years to show in the list.

Re: Form Builder - Year pulldown

Posted: Tue Mar 14, 2017 11:59 pm
by Jos
There is another way to accomplish this, if there weren't a small flaw in the current version of FormBuilder

With the Field Type: "-Module Interface Field" you can call a UDT.
The UDT can output your dropdown like this

Code: Select all

$FBid = $smarty->get_template_vars('FBid'); // get the field id
$year = Date('Y'); // get current year
$selected = ' selected="selected"';

echo '<div>
<label for="'.$FBid.'">Select Year</label>
<select class="cms_dropdown" name="'.$FBid.'"  id="'.$FBid.'">';
//create 10 dropdown items
for ($i = 0; $i <= 10; $i++) {
    echo '<option value="'.($year+$i).'"'.$selected.'>'.($year+$i).'</option>';
    $selected = '';
}
echo '</select>
</div>';
It's just that the smarty variable {$FBid} should be available, which isn't currently :(
http://dev.cmsmadesimple.org/bug/view/11327

Re: Form Builder - Year pulldown

Posted: Wed Mar 15, 2017 1:18 pm
by Jo Morg
FormBuilder has a form template that you can (and should) customize at will, thus you can actually use any valid HTML per field as you wish. So using an UDT as Jos has suggested or even just using the full HTML as paulbaker also suggested is equally valid. However in the case of jos' UDT I would call it directly from the form template instead of using a Field Type: "-Module Interface Field" as he suggested (btw Jos, thanks for catching that bug, I'll investigate it asap, although we will probably only fix it for next major release of FB). By calling the UDT on the form template you could pass the field id as a parameter for the UDT as well as the action id which is also necessary for the form to be processed. The UDT would then replace the field in the form template, something along the lines of (assuming you are still using the standard generated template for the form):
- where you find

Code: Select all

{foreach from=$fields item=entry}
		{if $entry->display == 1}

- replace whith

Code: Select all

{foreach from=$fields item=entry}
    {if $entry->id == $myyearpulldownfieldname->id}
      {myyearpulldownudt id="{$actionid}{$entry->id}"}
    {else}  
		{if $entry->display == 1}
- further down where you find

Code: Select all

		{/if}
	{/foreach}

- replace with

Code: Select all

		{/if}
    {/if}
	{/foreach}
Note: the code is not tested and you need to replace the names of the filed and that of the UDT accordingly. An additional tweak to the UDT would still be needed:

Code: Select all

//$FBid = $smarty->get_template_vars('FBid'); // get the field id
  $FBid = isset($params['id']) ? $params['id'] : ''; // get the field id new version
$year = Date('Y'); // get current year
$selected = ' selected="selected"';

echo '<div>
<label for="'.$FBid.'">Select Year</label>
<select class="cms_dropdown" name="'.$FBid.'"  id="'.$FBid.'">';
//create 10 dropdown items
for ($i = 0; $i <= 10; $i++) {
    echo '<option value="'.($year+$i).'"'.$selected.'>'.($year+$i).'</option>';
    $selected = '';
}
echo '</select>
</div>';
Again not tested, it's just a starting point... and if you have further customized the form template you'll have to figure out how to adapt it.
All that said, the field could actually have a more versatile approach and that will be taken in consideration too for next major version of FB.

Re: Form Builder - Year pulldown

Posted: Wed Mar 15, 2017 2:14 pm
by calguy1000
The provided solutions are WAY too over complicated.

There is a simple rule if using UDT's. UDT's should not output HTML, just data.

So it's a 2 step process:
a: Create a UDT that returns an array of the years you want to select.
b: Modify the formbuilder template to call that UDT, and provide the output of it to {html_options} to build a dropdown. Easy.

Code: Select all

// udt - untested
$this_year=Date('Y');
$start_year=$this_year-10;  // for example
$end_year=$this_year+10;  // for example
$out = [];
for( $yr = $start_year; $yr <= $end_year; $yr++ ) { 
    $out[$yr] = $yr;
}
if( isset($params['assign']) ) {
    $smarty->assign($params['assign'],$out);
} else {
    return $out;
}

Code: Select all

{* form template -- untested *}
{foreach from=$fields item=entry}
    {if $entry->id == $myyearpulldownfieldname->id}
        {myudt assign=years}{$yr=$smarty.now|date_format:'%Y'}
        <select name="{$entry->id}" id="{$entry->id}">
            {html_options options=$years selected=$yr}
         </select>
     {/if}
      ...