1. Go to this page and download the library: Download raftalks/form library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
raftalks / form example snippets
'Form' => 'Form\Form',
//making table based forms
Form::make('div', function($form))
{
$form->table(function($table)
{
$table->thead(function($table)
{
$table->tr(function($tr)
{
$tr->th('item');
$tr->th('category');
});
});
$table->tr(function($tr)
{
$tr->td()->ng_bind('item.name','ng-bind');
$tr->td()->ng_bind('item.category','ng-bind');
$tr->setNgRepeat('item in list','ng-repeat'); //using second parameter to force the attribute name.
});
$table->setClass('table');
});
});
//sample function to filter errors
function_to_filter_error_by_field($fieldname, $errors)
{
//filter errors and return matched error
}
//Macro created to show error message for fields
Form::macro('show_error',function($fieldName, $message=null)
{
return Form::template('span',function($form) use($fieldName, $message)
{
$error_messages = $form->get_errors();
$error_message = function_to_filter_error_by_field($fieldName, $error_messages);
// the contaner is <span></span> and we are
// adding the error message as text
$form->putText($error_message);
// set the container class
$form->setClass('help-block text-error');
});
});
// Now we are creating a nother styled input text field which uses
// the above show error macro
Form::macro('input_text', function($name, $label, $value=null, $attr = array())
{
return Form::template('div',function($form) use ($name, $label, $attr, $value)
{
//notice the setAttribute method used here can fill the element with an array
//of attributes
$form->text($name)->placeholder($label)->value($value)->setAttributes($attr);
//we are calling the macro to run the template and show the error message for this input template
$form->show_error($name);
$form->setClass('input');
});
});
// Now we use everything above like this to make a real form.
Form::make(function($form) use($validation_errors)
{
$form->share_errors($validation_errors);
//This will run the above macro and will show error messages if any exists
$form->input_text('username','User Name');
});
//globaly apply attributes to tag elements
//apply attribute to all text input fields
Form::decorate('text',function($tag)
{
$tag->class('class decorated');
});
//Use Form::decorate to apply attribute to all text input fields in templates
Form::decorate('text',function($tag)
{
$tag->class('class decorated');
});
//Create Form Macros with template
//bootstrap controlgroup textfield
Form::macro('group_text',function($name, $label=null)
{
return Form::template(function($form) use($name, $label)
{
$form->label($label)->class('control-label');
$form->div(function($form) use($name)
{
$form->text($name);
$form->setClass('controls');
});
$form->setClass('group-controls');
});
});
//the above Macro is now available as a Form field type and can be called within a Form
Form::make(function($form))
{
$form->group_text('telephone','Telephone Number');
}
// will
echo Form::make(function($form)
{
$form->div(function($form){ //makes a div container for the enclosed fields
//creates a text input with label
$form->text('username','User Name')->class('myname')->value('some name');
//creates a password input with label
$form->password('password','Enter Password');
$form->select('usergroup','User Group')->options(array('admin'=>'admin','manager'=>'manager','user'=>'user'),
array('user','admin'))->multiple('multiple');
$form->setClass('input'); //sets container class
$form->setId('UserAccount'); //sets container id
});
// creates an custom tag element like <group>dome</group>
$form->group('dome');
//creates a fieldset container <fieldset></fieldset> and enclose the fields in it
$form->fieldset(function($form)
{
$form->legend('HelloWOrld');
$form->label('Your Address')->for('address'); //create label field separately
$form->text('address');
});
//create Angularjs type input
$form->text('timer','Time')->ngmodel('time','ng-model');
$form->select('countries','select country')->ngrepeat('country.name in countries','ng-repeat');
$form->submit('Save');
//sets container attributes, therefore, as this is form container, this sets the form attributes
$form->setId('formIDhere');
$form->setAction(URL::to('test'));
$form->setMethod('POST');
$form->setClass('fill-up');
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.