
Social networks would not be what they are without forms. Forms enable people to tweet, comment, write reviews, and communicate with friends. It should come as no surprise, then, to find that Elgg defines some standard views and helper functions for generating forms and input elements.
We urge you to use these views when constructing your forms. Not only does it mean that your plugin can seamlessly take advantage of extra functionality defined by other plugins (such as a rich text editor), but it also helps protect you from certain security vulnerabilities.
Contents |
To output a form that submits to the "example" action, use the elgg_view_form function like so:
echo elgg_view_form('example');
Doing this should generate something like the following markup:
<form action="http://localhost/elgg/action/example"> <fieldset> <input type="hidden" name="__elgg_ts" value="1234567890" /> <input type="hidden" name="__elgg_token" value="3874acfc283d90e34" /> </fieldset> </form>
Well that was easy! Elgg does two things automatically for us when we generate forms this way:
But we're not done yet -- the form is useless unless the user has something to fill in and some way to submit the form. Conveniently, Elgg automatically looks for the body of the form in the forms/example view. So all we need to do is put the contents of our form at /mod/example/views/default/forms/example.php:
echo elgg_view('input/text', array('name' => 'example')); echo elgg_view('input/submit');
Now when we call elgg_view_form('example');, Elgg will produce something like the following source:
<form action="http://localhost/elgg/action/example"> <fieldset> <input type="hidden" name="__elgg_ts" value="1234567890" /> <input type="hidden" name="__elgg_token" value="3874acfc283d90e34" /> <input type="text" class="elgg-input-text" name="example" /> <input type="submit" class="elgg-button elgg-button-submit" value="Submit" /> </fieldset> </form>
You can add Sticky forms to protect your users from data loss when actions fail.
All input fields are encapsulated inside an input/form view. This view accepts the following parameters in its $vars array:
$form_body = "<p>This is my form</p>"; $form_body .= elgg_view('input/text', array('internalname' => 'mytextbox', 'value' => 'Initial value')); $form_body .= elgg_view('input/submit', array('internalname' => 'submit')); echo elgg_view('input/form', array('body' => $form_body, 'action' => "{$CONFIG->url}actions/my/action"));
Input views provide a safe and extendible way of representing data input fields, they all live in "input/xxxxxx".
Output views are the companion to input views and govern the display of input fields such as text, longtext, urls etc.
They all live in "output/xxxxxx".