Form Helper
The Form Helper file contains handlers that assist in working with forms.
Loading this Helper
This helper is loaded using the following code:
rigLoadHelper "form"
The following handlers are available:
rigFormOpen()
Creates an opening form tag with a base URL built from your config preferences. It will optionally let you add form attributes and hidden input fields, and will always add the attribute accept-charset based on the Output Charset value in your config file. This attribute defaults to "UTF-8".
The main benefit of using this tag rather than hard coding your own HTML is that it permits your site to be more portable in the event your URLs ever change. Furthermore, if CSRF protection is enabled in your application/config/config.lc file, the rigFormOpen() function automatically inserts a hidden CSRF token field in your forms.
Here's a simple example:
put rigFormOpen("email/send") into gData["formOpen"]
The above example would create a form that points to your base URL plus the "email/send" URI segments, like this:
<form method="post" action="http:/example.com/index.lc/email/send" />
If no action parameter is provided then the current URL is set.
Note: You can override the current URL by providing an action attribute in the second parameter.
Adding Attributes
Attributes can be added by passing an associative array to the second parameter, like this:
put "email" into tAttributes["class"]
put "myform" into tAttributes["id"]
put rigFormOpen("email/send", tAttributes) into gData["formOpen"]
The above example would create a form similar to this:
<form method="post" action="http:/example.com/index.lc/email/send" class="email" id="myform" />
Adding Hidden Input Fields
Hidden fields can be added by passing an associative array to the third parameter, like this:
put "Joe" into tHidden["username"]
put "234" into tHidden["memberID"]
put rigFormOpen("email/send", "", tHidden) into gData["formOpen"]
The above example would create a form similar to this:
<form method="post" action="http:/example.com/index.lc/email/send">
<input type="hidden" name="username" value="Joe" />
<input type="hidden" name="memberID" value="234" />
rigFormOpenMultiPart()
This function is absolutely identical to the rigFormOpen() tag above except that it adds a multipart attribute, which is necessary if you would like to use the form to upload files with.
rigFormHidden()
Lets you generate hidden input fields. You can either submit a name/value string to create one field:
put rigFormHidden("username", "johndoe") into gData["hidden"]
-- Would produce:
<input type="hidden" name="username" value="johndoe" />
Or you can submit an associative array to create multiple fields:
put "John Doe" into tData["name"]
put "john@example.com" into tData["email"]
put "http://example.com" into tData["url"]
put rigFormHidden(tData) into gData["hidden"]
-- Would produce:
<input type="hidden" name="name" value="John Doe" />
<input type="hidden" name="email" value="john@example.com" />
<input type="hidden" name="url" value="http://example.com" />
rigFormInput()
Lets you generate a standard text input field. You can minimally pass the field name and value in the first and second parameter:
put rigFormInput("username", "johndoe") into gData["input"]
Or you can pass an associative array containing any data you wish your form to contain:
put "username" into tData["name"]
put "username" into tData["id"]
put "johndoe" into tData["value"]
put "100" into tData["maxlength"]
put "50" into tData["size"]
put "width:50%" into tData["style"]
put rigFormInput(tData) into gData["input"]
-- Would produce:
<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" />
Of course you can define the input type too by setting an array key named "type" to any valid data.
Adding Data to <input>
If you would like your input field to contain some additional data, like Javascript, you can pass it as a string in the third parameter:
put "onClick=" & quote & "some_function()" & quote into tJS
put rigFormInput("username", "johndoe", tJS) into gData["input"]
rigFormPassword()
This function is identical in all respects to the rigFormInput() function above except that it sets it as a "password" type.
rigFormUpload()
This function is identical in all respects to the rigFormInput() function above except that it sets it as a "file" type, allowing it to be used to upload files.
rigFormTextarea()
This function is identical in all respects to the rigFormInput() function above except that it generates a "textarea" type. Note: Instead of the "maxlength" and "size" attributes in the above example, you will instead specify "rows" and "cols".
rigFormDropDown()
Lets you create a standard drop-down field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the optional third parameter will contain the value you wish to be selected. You can also pass an array of multiple items through the third parameter, and revIgniter will create a multiple select for you. Example:
put "Small Shirt" into tOptions["small"]
put "Medium Shirt" into tOptions["med"]
put "Large Shirt" into tOptions["large"]
put "Extra Large Shirt" into tOptions["exlarge"]
put "small" into tShirtsOnSale[1]
put "large" into tShirtsOnSale[2]
put rigFormDropDown("shirts", tOptions, "large") into gData["dropDown"]
-- Would produce:
<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="exlarge">Extra Large Shirt</option>
</select>
put rigFormDropDown("shirts", tOptions, tShirtsOnSale) into gData["dropDown"]
-- Would produce:
<select name="shirts" multiple="multiple">
<option value="small" selected="selected">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="exlarge">Extra Large Shirt</option>
</select>
Adding Data to <select>
If you would like the opening <select> to contain additional data, like an id attribute or JavaScript, you can pass it as a string in the fourth parameter:
put "id=" & quote & "shirts" & quote && "onChange=" & quote & "some_function();" & quote into tJS
put rigFormDropDown("shirts", tOptions, "large", tJS) into gData["dropDown"]
Adding Attributes to <option>
You can add attributes to the <option> tag by appending comma delimited attribute value pairs to a key of the options array like in the following example:
put "Choose a size ..." into tOptions["choose","data-placeholder=" & quote & "true" & quote,"data-hilite=" & quote & "true" & quote]
If the array passed as tOptions is a multidimensional array, rigFormDropDown() will produce an <optgroup> with the array key as the label.
Sorting
The optional fifth parameter let's you sort the drop-down options. Possible values defining sort direction (ascending or descending) and sort type (numeric or text) are: asctext, desctext, ascnum and descnum. If sort type is not specified ascending text is applied. Following an example sorting the options above from "small" to "exlarge" or from "Small Shirt" to "Extra Large Shirt" respectively:
put rigFormDropDown("shirts", tOptions, "large", , "desctext") into gData["dropDown"]
Note: Sorting applies to option keys, not values.
This way, as option keys are arbitrary, you can achieve any order for drop-down options independent from their values.
rigFormMultiselect()
Lets you create a standard multiselect field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value or values you wish to be selected. The parameter usage is identical to using rigFormDropDown() above, except of course that the name of the field will need to use POST array syntax, e.g. foo[].
rigFormFieldset()
Lets you generate fieldset/legend fields.
put rigFormFieldset("Address Information") into gData["fieldSet"]
put <p>"form content here"</p> into gData["fieldData"]
put rigFormFieldsetClose() into gData["setClose"]
-- Produces
<fieldset>
<legend>Address Information</legend>
<p>form content here</p>
</fieldset>
Similar to other functions, you can submit an associative array in the second parameter if you prefer to set additional attributes.
put "address_info" into tAttributes["id"]
put "address_info" into tAttributes["class"]
put rigFormFieldset("Address Information", tAttributes) into gData["fieldSet"]
put <p>"form content here"</p> into gData["fieldData"]
put rigFormFieldsetClose() into gData["setClose"]
-- Produces
<fieldset id="address_info" class="address_info">
<legend>Address Information</legend>
<p>form content here</p>
</fieldset>
rigFormFieldsetClose()
Produces a closing </fieldset> tag. The only advantage to using this function is it permits you to pass data to it which will be added below the tag. For example:
put "</div></div>" into tString
put rigFormFieldsetClose(tString) into gData["fieldClose"]
-- Would produce:
</fieldset>
</div></div>
rigFormCheckbox()
Lets you generate a checkbox field. Simple example:
put rigFormCheckbox("newsletter", "accept", TRUE) into gData["checkbox"]
-- Would produce:
<input type="checkbox" name="newsletter" value="accept" checked="checked" />
The third parameter contains a boolean TRUE/FALSE to determine whether the box should be checked or not.
Similar to the other form functions in this helper, you can also pass an array of attributes to the function:
put "newsletter" into tData["name"]
put "newsletter" into tData["id"]
put "accept" into tData["value"]
put TRUE into tData["checked"]
put "margin:10px" into tData["style"]
put rigFormCheckbox(tData) into gData["checkbox"]
-- Would produce:
<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />
As with other functions, if you would like the tag to contain additional data, like JavaScript, you can pass it as a string in the fourth parameter:
put "onClick=" & quote & "some_function()" & quote into tJS
put rigFormCheckbox("newsletter", "accept", TRUE, tJS) into gData["checkbox"]
rigFormRadio()
This function is identical in all respects to the rigFormCheckbox() function above except that it sets it as a "radio" type.
rigSubmitButton()
Lets you generate a standard submit button. Simple example:
put rigSubmitButton("mysubmit", "Submit Post!") into gData["submit"]
-- Would produce:
<input type="submit" name="mysubmit" value="Submit Post!" />
Similar to other functions, you can submit an associative array in the first parameter if you prefer to set your own attributes. The third parameter lets you add extra data to your form, like JavaScript.
rigFormLabel()
Lets you generate a <label>. Simple example:
put rigFormLabel("What is your Name", "username") into gData["label"]
-- Would produce:
<label for="username">What is your Name</label>
Similar to other functions, you can submit an associative array in the third parameter if you prefer to set additional attributes.
put "mycustomclass" into tAttributes["class"]
put "color: #000;" into tAttributes["style"]
put rigFormLabel("What is your Name", "username", tAttributes) into gData["label"]
-- Would produce:
<label for="username" class="mycustomclass" style="color: #000;">What is your Name</label>
rigResetButton()
Lets you generate a standard reset button. Use is identical to rigSubmitButton().
rigFormButton()
Lets you generate a standard button element. You can minimally pass the button name and content in the first and second parameter:
put rigFormButton("name", "content") into gData["nameBtn"]
-- Would produce
<button name="name" type="button">Content</button>
Or you can pass an associative array containing any data you wish your form to contain:
put "button" into tData["name"]
put "button" into tData["id"]
put "true" into tData["value"]
put "reset" into tData["type"]
put "Reset" into tData["content"]
put rigFormButton(tData) into gData["nameBtn"]
-- Would produce:
<button name="button" id="button" value="true" type="reset">Reset</button>
If you would like your form to contain some additional data, like JavaScript, you can pass it as a string in the third parameter:
put "onClick=" & quote & "some_function()" & quote into tJS
put rigFormButton("mybutton", "Click Me", tJS) into gData["myBtn"]
rigFormClose()
Produces a closing </form> tag. The only advantage to using this function is it permits you to pass data to it which will be added below the tag. For example:
put "</div></div>" into tString
put rigFormClose(tString) into gData["formClose"]
-- Would produce:
</form>
</div></div>
rigFormPrep()
Allows you to safely use HTML and characters such as quotes within form elements without breaking out of the form. Consider this example:
put "Here is a string containing " & quote & "quoted" & quote & " text." into gData["string"]
<input type="text" name="myform" value="[[gData["string"]]]" />
Since the above string contains a set of quotes it will cause the form to break. The rigFormPrep function converts HTML so that it can be used safely:
put rigFormPrep("Here is a string containing " & quote & "quoted" & quote & " text.") into gData["string"]
Note: If you use any of the form helper handlers listed in this page the form values will be prepped automatically, so there is no need to call this function. Use it only if you are creating your own form elements.
rigSetValue()
Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
put rigSetValue("quantity", "0") into gData["qty"]
<input type="text" name="quantity" value="[[gData["qty"]]]" size="50" />
The above form will show "0" when loaded for the first time.
In case you need to use this function in combination with i.e. rigFormInput() you can turn off HTML escaping of the value and avoid double-escaping setting the third (optional) parameter to FALSE.
rigSetSelect()
If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).
Example:
put rigSetSelect("mySelect", "one", TRUE) into gData["mySelectOne"]
put rigSetSelect("mySelect", "two") into gData["mySelectTwo"]
put rigSetSelect("mySelect", "three") into gData["mySelectThree"]
<select name="myselect">
<option value="one" [[gData["mySelectOne"]]] >One</option>
<option value="two" [[gData["mySelectTwo"]]] >Two</option>
<option value="three" [[gData["mySelectThree"]]] >Three</option>
</select>
rigSetCheckbox()
Permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:
put rigSetCheckbox("myCheck[]", "A") into gData["myCheckA"]
put rigSetCheckbox("myCheck[]", "B") into gData["myCheckB"]
put rigSetCheckbox("myCheck[]", "C") into gData["myCheckC"]
<input type="checkbox" name="myCheck[]" value="A" [[gData["myCheckA"]]] />
<input type="checkbox" name="myCheck[]" value="B" [[gData["myCheckB"]]] />
<input type="checkbox" name="myCheck[]" value="C" [[gData["myCheckC"]]] />
rigSetRadio()
Permits you to display radio buttons in the state they were submitted. This function is identical to the rigSetCheckbox() function above.
put rigSetRadio("myRadio", "1", TRUE) into gData["myRadio1"]
put rigSetRadio("myRadio", "2") into gData["myRadio2"]
<input type="radio" name="myRadio" value="1" [[gData["myRadio1"]]] />
<input type="radio" name="myRadio" value="2" [[gData["myRadio2"]]] />