Wednesday, 13 July 2016

Forms

Form
Before we talk about “form” we need to make few changes, first of all edit composer.json file located in your project e.g. D:\wamp\www\bitspedia with notepad you will see the following code written
"require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.2.*"
    },
change it as
"require": {
    "laravelcollective/html": "5.2.*"
},
Now go to command prompt and type
composer update
After this open the “app.php” file located in config folder e.g. D:\wamp\www\bitspedia\config add following things
Remember don’t delete any text while adding these classes
'providers' => [
    Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
     
'Form' => Collective\Html\FormFacade::class,
     
'Html' => Collective\Html\HtmlFacade::class,
 
],
Another thing you must do before starting a form is define its URL i.e. which URL will be hit to open this form, for that open routes.php file located in http folder which is located in app folder e.g. D:\wamp\www\bitspedia\app\Http
For now just define a function in “routes.php” which will just tell that when a specific URL is hit it must open the form. We will also create a function which tells what it will do when form is submitted, we just want to print “information received”.



Route::get('createUser',function(){
    return View::make('form');
});

Route::get('storeUser',function(){
    return
"information received";
});

This tells that when createUser is hit it must open the form located in views.
Last work you need to do is build a migration as explained in migration article, however let me show you the up and down methods of the migration you will build
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->string('name');
        $table->string('subjects');
        $table->string('gender');
        $table->date('date_of_birth');
        $table->integer('phone_no');
    });
}
public function down()
{
    Schema::drop("users");
}
Now you are ready to start your form

Creating a form
Now first of all you must know where to create form, as form is shown to user so it must be located in views folder located in resource folder e.g. D:\wamp\www\bitspedia\resources\views

Create a php file here named as form.blade.php
“form” is name of my form so you can change it however “blade” is extension. Blade is templating engine. If you don’t know what templating engine is don’t worry, you will know it after reading this.
Now let’s start writing your code in form.blade.php file. The output of the following code is shown below.

{{ Form::open(['url'=>'storeUser' , 'method'=>'post', 'files' => true]) }} //starting the form
{{Form::label('Name', 'Name',array('id'=>'namefield','class'=>'textfield', 'style'=>'height:100%'))}}<br> //label

{{Form::input('text', 'name','', array('id'=>'namefield'))}}<br><br>//textfiled

{{Form::label('OOP', 'OOP')}}<br>
{{ Form::checkbox('oop', true)}}
//checkbox
{{Form::label('DS', 'DS')}}
{{ Form::checkbox('ds')}}
{{Form::label('SC', 'SC')}}
{{ Form::checkbox('sc')}}<br><br>

{{Form::label('Gender', 'Gender')}}<br>//radio button
{{Form::label('Male', 'Male')}}
{{ Form::radio('gender', 'male')}}
{{Form::label('Female', 'Female')}}
{{ Form::radio('gender', 'female')}} <br><br>
{{Form::label('DateOfBirth', 'Date Of Birth')}}<br>//date type
{{Form::date('date')}}<br><br>
{{Form::label('PhoneNo', ' PhoneNo ')}}<br> //number type
{{Form::number('phone_no', 'value')}}<br><br>
{{Form::file('image')}}<br><br>
{{Form::select('size', array('English', 'Math'), 'Math')}} <br><br> //select with two options
{{Form::submit('enter')}} //button
{!! Form::close() !!} //close the form

The form starting line will generate the following code
<form method="POST" action="http://localhost/bitspedia/public/index.php/storeUser" accept-charset="UTF-8" enctype="multipart/form-data"><input name="_token" type="hidden">
You can see that 'url'=>'storeUser' tells us where to go when submit this form and 'files' => true tells us that this form contains some file to be uploaded and 'method'=>'post' tells us the method by which this form will be submitted
The first label in the form and the very next line i.e. text filed will generate the following code
<label for="Name" id="namefield" class="textfield" style="height:100%">Name</label><br>
<input id="namefield" name="name" type="text" value=""><br>
You see we are writing a simple code and it is automatically generating the php code for it, this is what a templating engines do.
So we have created a form with a text filed and label. The id of the label is “namefield” and
class of that is “textfield” and we have also used styling in it. I gave an empty single quotation marks in the text field that’s because the third attribute is equal to value so if you want to set id,class etc of the text field you have to give any value in the third attribute.
We created three chekboxes with “OOP” selected as default. It is done by giving it a 2nd argument as true.
We have created a drop down list with two options i.e. English and math and Math selected as default bu writing it in the third attribute.
And the last line where we are closing the form will simple generate
</form>


No comments:

Post a Comment