Skip to content

Instantly share code, notes, and snippets.

@andyvanee
Created December 15, 2011 20:12
Show Gist options
  • Select an option

  • Save andyvanee/1482652 to your computer and use it in GitHub Desktop.

Select an option

Save andyvanee/1482652 to your computer and use it in GitHub Desktop.
PHP Smarty Form Templating
<h1>Basic Form Template</h1>
{form for=$my_form}
<!-- this is a hidden field -->
{$id.html}
<!-- Default rendering of the field -->
<p>
{if $cms_headline.error}{$cms_headline.error} <br>{/if}
{$cms_headline.label} <br>
{$cms_headline.html}
</p>
<!-- Custom rendering of the field -->
<p>
{if $content.error}<div class="error">{$content.error}</div>{/if}
{$content.label} <br>
<textarea class='custom-text' name="{$content.name}">{$content.value}</textarea>
</p>
{$__submit__.html} <br>
{$my_form.requirednote}
{/form}
A var_dump of the form. all the elements get assigned by "name"
in the template, but it's all available via the $form that you
pass in. Errors are copied from $form['errors'] and added to the
field itself as $field['error'].
'javascript' and 'attributes' are added to the form by the {form}
block function, so you shouldn't have to worry about those.
$form =
array(6) {
["frozen"]= bool(false)
["javascript"]= string(549) "..."
["attributes"]= string(195) "..."
["requirednote"]= string(111) "..."
["errors"]= array(0) {
}
["sections"]= array(1) {
[0]= array(3) {
["header"]= string(7) "Content"
["name"]= string(10) "__header__"
["elements"]= array(5) {
[0]= array(8) {
["name"]= string(2) "id"
["value"]= NULL
["type"]= string(6) "hidden"
["frozen"]= bool(false)
["required"]= bool(false)
["error"]= NULL
["label"]= string(0) ""
["html"]= string(42) "<input name="id" type="hidden" value="">"
}
[1]= array(8) {
["name"]= string(12) "cms_headline"
["value"]= NULL
["type"]= string(4) "text"
["frozen"]= bool(false)
["required"]= bool(true)
["error"]= NULL
["label"]= string(8) "Headline"
["html"]= string(41) "<input name="cms_headline" type="text">"
}
[2]= array(8) {
["name"]= string(7) "content"
["value"]= NULL
["type"]= string(8) "textarea"
["frozen"]= bool(false)
["required"]= bool(true)
["error"]= NULL
["label"]= string(7) "Content"
["html"]= string(53) "<textarea class="ckeditor" name="content"></textarea>"
}
[3]= array(8) {
["name"]= string(10) "__submit__"
["value"]= string(4) "Save"
["type"]= string(6) "submit"
["frozen"]= bool(false)
["required"]= bool(false)
["error"]= NULL
["label"]= string(0) ""
["html"]= string(54) "<input name="__submit__" value="Save" type="submit">"
}
[4]= array(8) {
["name"]= string(16) "__submit_draft__"
["value"]= string(13) "Save as Draft"
["type"]= string(6) "submit"
["frozen"]= bool(false)
["required"]= bool(false)
["error"]= NULL
["label"]= string(0) ""
["html"]= string(69) "<input name="__submit_draft__" value="Save as Draft" type="submit">"
}
}
}
}
}
<?php
require_once 'app/controllers/asset_controller.php';
class TextController extends AssetController {
function __construct() {
$this->name = 'text';
$this->versioning = true;
$this->base_view_dir = ROOT_DIR;
parent::__construct();
}
function testform(){
$model = $this->getDefaultModel();
$form = $this->getForm();
$vals = $form->exportValues();
if ($form->validate()){
$inserted = $this->insert(true);
$model->find(array('conditions'=>'cms_headline = '.$vals['cms_headline']));
$model->fetch();
$this->set($model->toArray());
$entry = $this->render(array('action'=>'default', 'return'=>true));
return $entry;
}
$this->set('my_form',$form->toArray());
return $this->render(array('action'=>'form', 'return'=>true));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment