nandeeshmp/form-builder

Yet another form builder library

v1.0 2015-07-25 05:49 UTC

This package is not auto-updated.

Last update: 2020-04-03 17:19:52 UTC


README

Coverage Status Code Climate Build Status

Yet another form builder & validator package.

This pack provides an interface to generate/render html form and validate submitted form with defined rules.

Installation

You can install this package via Composer by running the following command in the root of your project.

composer require nandeeshmp/form-builder

Form Builder Basic Usage

	require 'vendor/autoload.php';

	$form = new FormBuilder\Form(TRUE);

	$form->open();

	$form->label('name');
	$form->text('name', 'Nandeesh Mp', [
                        'class' => 'cls-name',
                        'error' => 'Name cannot be <2 characters'
                    ]);

	$form->label('email');
	$form->email('email', 'nandeesh@example.com', ['class' => 'cls-email']);

	$form->close();

Builder Documentation

Opening and Closing a Form

// <form method="POST">
$form->open();

// <form method="GET">
$form->open(['method' => 'GET']);
    
// <form method="POST" action="/action_handler">
$form->open(['action' => '/action_handler']);

// <form method="POST" action="" enctype="multipart/form-data">
$form->open(['files' => true]);

Text, Hidden, Email, Password, URL, Image, Submit, Reset Fields

// <input type="text" name="username">
$form->text('username');

// <input type="text" name="username" id="username">
$form->text('username', '', ['id' => 'username']);

// <input type="hidden" name="user-id" id="user-id">
$form->hidden('user-id', '', ['id' => 'user-id']);

// <input type="email" name="email">
$form->email('email');

// <input type="email" name="email" value="nandeesh@example.com" required="required">
$form->email('email', 'nandeesh@example.com', ['required' => required]);

// <input type="password" name="password" class="cls-password">
$form->password('password','', ['class' => 'cls-password']);

// <input type="url" name="website" value="" class="form-control" placeholder="Enter your website / blog">
$form->url('website', '', ['class' => 'form-control', 'placeholder' => 'Enter your website / blog']);

// <input type="submit" value="Submit" class="btn btn-primary">
$form->submit('Submit', ['class' => 'btn btn-primary']);

// <input type="reset" value="Reset" class="btn btn-primary">
$form->reset('Reset', ['class' => 'btn btn-primary']);

Textareas

// <textarea name="description" rows="4" cols="30"></textarea>
$form->textarea('description', ['rows' => 4]);

// <textarea name="description" rows="4" cols="30"></textarea>
$form->textarea('description', ['rows' => 4, 'cols' => 30]);

// <textarea name="description" rows="4" cols="30" class="cls-desc">Description...</textarea>
$form->textarea('description', 'Description...', ['rows' => 4, 'cols' => 30, 'class' => 'cls-desc']);

Checkboxes and Radio Buttons

// <input type="checkbox" name="language" value="PHP">
$form->checkbox('language','PHP');

// <input type="checkbox" name="language" value="Ruby" checked="checked">
$form->checkbox('language','Ruby',TRUE)

// <input type="checkbox" name="language" value="Java" class='cls-check-box'>
$form->checkbox('language','Java',FALSE,[class => 'cls-check-box'])

// <input type="radio" name="language" value="PHP">
$form->radio('language','PHP');

// <input type="radio" name="language" value="Ruby" checked="checked">
$form->radio('language','Ruby',TRUE)

// <input type="radio" name="language" value="Java" class='cls-check-box'>
$form->radio('language','Java',FALSE,[class => 'cls-check-box'])

Select Element

// <select class="form-control" name="qualification">
//    <option value="0" selected="">-- Select --</option>
//    <option value="1">Bachelor Degree</option>
//    <option value="2">Master Degree</option>
// </select>
$options_list = [0 => '-- Select --', 1 => 'Bachelor Degree', 2 => 'Master Degree'];
$form->select('qualification', $options_list, '', ['class' => 'form-control']);

Buttons

// <button  class="cls-button" type="button">Save Data</button>
$form->button('Save Data', ['class' => 'cls-button'])

Labels

// <input  type="text" name="username" value="Nandeesh" class="cls-username"/>'
$form->text('username', 'Nandeesh', ['class' => 'cls-username']);

Setting Attributes

Any number of additional attributes can be passed as an array. Each element accepts optional last argument of type array. For example in the below code $form->button() accepts ['class' => 'cls-button', data-dismiss => 'modal', aria-label => 'Close'] to set additional attributes,

// <button type="button" class="cls-button close" data-dismiss="modal" aria-label="Close">Save Data</button>
$form->button('Save Data', ['class' => 'cls-button', data-dismiss => 'modal', aria-label => 'Close']);

Validator Basic Usage

require 'vendor/autoload.php';

$jsonObject = json_encode([
    'username'         => 'nandeesh',
    'email'            => 'nandeesh@example.com',
    'password'         => 'nandeesh',
    'confirm-password' => 'nandeesh'
]);

$rules = [
    'username'         => 'required|Minlength:4',
    'email'            => 'required|email',
    'password'         => 'required',
    'confirm-password' => 'required|same:password',
];

$validator = new \Form\Validator($rules, $jsonObject);

if($validator->isValid()){
    echo 'Passed json object is valid';
}

Validator Documentation

Supported validation rules :

  1. required - Validates specified property exists in the passed values
  2. minlength:length - Validates the value of specified property length is greater than 'length'
  3. maxlength:length - Validates the value of specified property length is lesser than 'length'
  4. minvalue:size - Validates the value of specified property is greater than 'size'
  5. maxvalue:size - Validates the value of specified property is greater than 'size'
  6. email - Validates specified property is email
  7. url - Validates specified property is url
  8. same:property - Validates specified property is equal to another another 'property'

Some Validator Examples

required

	$object = json_encode([
		'username' => 'nandeesh',
		'email'    => 'nandeesh@example.com',
		'password' => 'nandeesh',
	]);
	$rules = [
		'username' => 'required',
		'email'    => 'required',
		'password' => 'required'
	];
	$validator = new \Form\Validator($rules, $object);
    if($validator->isValid()){
        echo 'Passed json object is valid';
    }

email

	$object = json_encode(['email' => 'nandeesh@example.com']);
	$rules = ['email' => 'required|email'];
	$validator = new \Form\Validator($rules, $object);
    if($validator->isValid()){
        echo 'Passed json object is valid';
    }

minlength:4

	$object = json_encode(['username' => 'nandeesh']);
	$rules = ['username' => 'required|Minlength:4'];
	$validator = new \Form\Validator($rules, $object);
    if($validator->isValid()){
        echo 'Passed json object is valid';
    }

maxlength:10

	$object = json_encode(['username' => 'nandeesh']);
	$rules = ['username' => 'required|Maxlength:10'];
	$validator = new \Form\Validator($rules, $object);
    if($validator->isValid()){
        echo 'Passed json object is valid';
    }

url

	$object = json_encode(['link' => 'http://www.nandeesh.com']);
	$rules = ['link' => 'required|url'];
	$validator = new \Form\Validator($rules, $object);
    if($validator->isValid()){
        echo 'Passed json object is valid';
    }

same:property

	$object = json_encode(['password' => 'not-real', 'password-confirm' => 'not-real']);
	$rules = ['password' => 'same:password-confirm'];
	$validator = new \Form\Validator($rules, $object);
    if($validator->isValid()){
        echo 'Passed json object is valid';
    }