Skip to content

Instantly share code, notes, and snippets.

@TomiS
Created April 11, 2010 14:37
Show Gist options
  • Save TomiS/362782 to your computer and use it in GitHub Desktop.
Save TomiS/362782 to your computer and use it in GitHub Desktop.
//This is an untested code snippet. Read it as pseudo code
// CONTROLLER (in PHP)
...
//Helper function for creating a form
private function prepare_my_form($namespace)
{
$form = midgardmvc_helper_forms::create($namespace);
if ($namespace == 'create_form')
{
$form->add_field('text_field', 'text');
$group = $form->add_group('group_name');
$group->add_field('email_field', 'email');
$group->add_field('number_field', 'integer', true);
$form->add_field('boolean_field', 'boolean', true, array('clean'));
$form->add_field('ipaddress_field', 'ipaddress', true, array('validate'));
}
elseif($namespace == 'delete_form')
{
//Parameters are: name, field type, is required, field actions methods in an array
$form->add_field('id', 'integer', true, array('validate'));
}
return $form;
}
public function get_crud($args)
{
//Prepare creation form and pass to template
$this->data['my_create_form'] = $this->prepare_my_form('create_form');
//Prepare deletion form and pass to template
$this->data['my_delete_form'] = $this->prepare_my_form('delete_form');
}
public function post_crud($args)
{
$mvc = midgardmvc_core::get_instance();
try
{
//Figure out which form was posted
$namespace = midgardmvc_helper_forms::identify_post();
//Prepare the one specific form that was posted
$form = $this->prepare_my_form($namespace);
//Process post, call field validators and cleaners
$form->process_post();
//Do what you want with the posted variables
if ($namespace == 'create_form')
{
$my_object = new some_object();
$my_object->content = $form->text_field->get_value();
$my_object->number = $form->group_name->number_field->get_value();
$my_object->create();
}
elseif($namespace == 'delete_form')
{
$my_object = new some_object($form->id);
$my_object->delete();
}
//Show an UI message about successful operation
}
catch(midgardmvc_helper_forms_exception_validation $e)
{
//Show an UI message about validation error or do what ever you want
}
}
...
//TEMPLATE (in PHPTAL)
<form method='post' action=''>
<!-- this variable definition is just for convenience -->
<tal:block tal:define="global form my_migard_component/my_create_form" />
<-- namespace must be included in every form -->
<input type='hidden' name='midgardmvc_helper_forms_namespace' value='${form/namespace}' />
Username<br />
<!-- input field name is equal to the field name defined in controller -->
<input type='text' name='text_field' value='' /><br />
<fieldset>
<legend>Stuff</legend>
Email<br />
<!-- this row generates html using a widget, notice also the usage of groups -->
<tal:block tal:replace="structure form/group_name/email_field/email" /><br />
Number<br />
<input type='text' name='number_field' value='' /><br />
</fieldset>
<input type='submit' value='Create' />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment