This example is in reply to a question in
this topic. and I’ve kept the solution rather simple. There’s not a lot of error checking for instance. I’m not trying to solve the problem but rather to give anyone wanting to dynamically build a select field using TV’s with eForm something to start from.
The goal:
Creating a form where visitors can select a meeting from a list of dates.
Meeting dates can be entered by the site admin or editor.
The solution:
1. Create a TV and name it
tv_dates. Set the input type to textarea (mini) and set the description to "Add one date per line". Leave the rest of the fields blank and assign the TV to the same template the page your form is using.
2. Create the form and report template.
form template: (named sampleForm here)
[+validationmessage+]
<form action="[~[*id*]~]" id="test1" method="post">
<p><label for="fld_name">Name</label><br />
<input type="text" id="fld_name" name="name" eform=":text:1:Please fill in your full name" /></p>
<p><label for="fld_options">TV Options</label><br />
<select id="fld_options" name="tv_options" eForm="::1:You need to select something:">
[+display_tv_options+]
</select></p>
<p><input type="submit" value="send" /></p>
</form>
Note the [+display_tv_options+] place holder. This will be replaced with the values from the TV.
The report: (named sampleReport here)
<p>Here's the results of your form:</P
<p>Name: [+name+]</p>
<p>Your options:[+tv_options+]</p>
3 Create a snippet with the function we are going to use in the eFormOnBeforeFormParse event. (I’ve named the snippet
getTvDates)
<?php
if( !function_exists('eformGetTvDates') ) {
function eformGetTvDates(&$fields,&$templates){
global $modx;
$rawvalue = $modx->getTemplateVarOutput('tv_dates');
if(!$rawvalue['tv_dates']){ //no data, don't display a form at all
$templates['tpl']="<p>There are no meetings scheduled</p>";
}
$dates = explode("\n",$rawvalue['tv_dates']);
foreach( $dates as $dateString ){
$dateString = trim($dateString);
if($dateString) $options .= "<option value=\"$dateString\">$dateString</option>\n";
}
$templates['tpl']=str_replace('[+display_tv_options+]',$options,$templates['tpl']);
return true;
}
}
return '';
?>
4. Create the form page and add both snippets.
[[getTvDates]]
[!eForm? &formid=`sampleForm` &tpl=`sampleForm` &report=`sampleReport` &noemail=`1` &eFormOnBeforeFormParse=`eformGetTvDates`!]
5.
Add some values in the TV. As I said before there’s not much error checking so potentially you could fill in anything, date or not.
21 February 2008
12 April 2008
1 January 2009
The function in 3. will get the template variable output if there is one and split each value on a line ending. Empty lines will be discarded. Each value is added as an option field and the result is used to fill in the [+display_tv_options+] placeholder in the form template. If no TV values are found (and thus no meeting dates) the form template is replaced completely with a simple message.
This is pretty limited example and you should not just copy and paste it for a live site. Think about what you require and adapt this to your needs!