Wednesday, May 20, 2015

Contoh penggunaan facade form::

1. Buka Form baru :

{{ Form::open() }}
 
Hasil :
<form method="POST" action="http://currenturl" accept-charset="UTF-8">
<input name="_token" type="hidden" value="somelongrandom string">
 
 
-guna url :
 
{{ Form::open(array('url' => 'http://full.url/here')) }}
 
hasil :
<form method="POST" action="http://full.url/here" accept-charset="UTF-8">
<input name="_token" type="hidden" value="somelongrandom string">
 
    
-guna route
 
{{ Form::open(array('route' => 'named.route')) }}
 
hasil :
<form method="POST" action="http://full.url/someplace" accept-charset="UTF-8">
<input name="_token" type="hidden" value="somelongrandom string">
 
-guna controller
 
{{ Form::open(array('action' => 'Controller@method')) }}
 
hasil :
<form method="POST" action="http://full.url/someplace" accept-charset="UTF-8">
<input name="_token" type="hidden" value="somelongrandom string">
 
-guna method
 
{{ Form::open(array('method' => 'get')) }}
 
hasil :
<form method="GET" action="http://currenturl" accept-charset="UTF-8">
 
-guna fail upload :
 
{{ Form::open(array('files' => true)) }}
 
hasil :
<form method="POST" action="http://currenturl" accept-charset="UTF-8"
  enctype="multipart/form-data">
<input name="_token" type="hidden" value="somelongrandom string">
 
- guna method tertentu - PUT/PATCH/DELETE (laravel) :
 
{{ Form::open(array('method' => 'PUT')) }}
 
hasil :
<form method="POST" action="http://currenturl" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PUT">
<input name="_token" type="hidden" value="somelongrandom string">          
 
 

2. CiptaForm berdasarkan Model baru

{{ Form::model($item, array('route' => array('items.update', $item->id))) }}
 
Nota :
arahan2 Form::input(), Form::textarea() dan Form::select() akan menggunakan data dari model.


Terdapat turutan untuk pengisian data :
  1. Data dari Session, dimana 'old input' disimpan. Ini berlaku sekiranya ada ralat dari pengisian Form.
  2. Data explicit, contohnya data yang diberi sebagai argument bila Form::input() dipanggil.
  3. Data model, bina nama field sama dengan nama input form.



3. Tutup Form

{{ Form::close() }}



4. Bina token CSRF

{{ Form::token() }}
 
hasil :
 
<input name="_token" type="hidden" value="somelongvalue"> 


5. Cipta label Form :

{{ Form::label('name', 'Your Name') }}
 
hasil :
 
<label for="name">Your Name</label>
 
tambah atribute
 
{{ Form::label('name', 'Your Name', array('class' => 'mylabel')) }}
 
hasil :
 
<label for="name" class="mylabel">Your Name</label>
 
 
Nota : Form::label() akan secara automatic bina ids    


6. Cipta input Text :

{{ Form::text('first_name') }}
 
hasil :
 
<input name="first_name" type="text">
 
- tambah nilai 
 
{{ Form::text('first_name', 'Chuck') }}
 
hasil :
 
<input name="first_name" type="text" value="Chuck">
 
- tambah atribute
 
{{ Form::text('first_name', 'Chuck', array('class' => 'field')) }}
 
 
hasil :
 
<input class="field" name="first_name" type="text" value="Chuck">
 
Nota : Flash Session Input akan diguna sebegai nilai sebelum menggunaka data nilai.      

7. Cipata input Password :

{{ Form::password('secret') }}
 
hasil :
 
<input name="secret" type="password" value="">
 
tambah attribute :
 
{{ Form::password('secret', array('class' => 'field')) }}
 
hasil :
 
<input class="field" name="secret" type="password" value="">
 
    
8. Cipta imput  Hidden :

{{ Form::hidden('invisible', 'secret') }}
 
hasil :
 
<input name="invisible" type="hidden" value="secret">
 
dengan atribute :
 
{{ Form::hidden('invisible', 'secret', array('id' => 'invisible_id')) }}
 
hasil :
 
<input id="invisible_id" name="invisible" type="hidden" value="secret">
 
    

9. Cipta input Email :

{{ Form::email('email_address') }}
 
hasil :
 
<input name="email_address" type="email">
 
 
dengan nilai :
 
{{ Form::text('email_address', 'noreply@gmail.com') }}  
 
hasil :
 
<input name="email_address" type="email" value="noreply@gmail.com">
 
dengan atribute :
 
{{ Form::email('email_address', 'noreply@gmail.com', ['class' => 'field']) }}
 
hasil :
 
<input class="field" name="email_address" type="email" value="noreply@gmail.com">   

10. Cipta input  URL :

{{ Form::url('webpage') }}
 
hasil :
 
<input name="webpage" type="url">
 
dengan nilai :
 
{{ Form::url('webpage', 'http://a.com') }}
 
hasil :
 
<input name="webpage" type="url" value="http://a.com">
 
dengan atribute :
 
{{ Form::url('webpage', 'http://a.com', ['class' => 'field']) }}
 
hasil :
 
<input class="field" name="webpage" type="url" value="http://a.com">     

11. Cipta input File :

{{ Form::file('thefile') }}
 
hasil :
 
<input name="thefile" type="file">
 
dengan atribute :
 
{{ Form::file('thefile', ['class' => 'field']) }}
 
hasil :
 
<input class="field" name="thefile" type="file">   


12. Cipta input Form :
- format umum untuk input form :

{{ Form::input('email', 'email_address', null, ['class' => 'emailfld']) }}
 
Nota : 4 argument :
  1. $type - (perlu) jeni input contoh "text", "password", "file" ...
  2. $name - (perlu) nama input.
  3. $value - (pilihan) nilai input.
  4. $options - (pilihan) atrubute tambahan contoh  "id", "size""class" ...

13. Cipta input Textarea :

{{ Form::textarea('notes') }}
 
hasil :
 
<textarea name="notes" cols="50" rows="10"></textarea>
 
nilai ke2 :
 
{{ Form::textarea('notes', '3 < 4') }} 

hasil :
<textarea name="notes" cols="50" rows="10">3 &lt; 4</textarea>
 
dengan atribute :
 
{{ Form::textarea('notes', null, ['class' => 'field']) }}
 
hasil :
 
<textarea class="field" name="notes" cols="50" rows="10"></textarea>
 
 
dengan saiz :
 
{{ Form::textarea('notes', null, ['size' => '30x5']) }}
 
hasil :
 
<textarea name="notes" cols="30" rows="5"></textarea>
 
 
14. Cipta kotak pilihan Select Box :

{{ Form::select('age', ['Under 18', '19 to 30', 'Over 30']) }}
 
hasil :
 
<select name="age">
  <option value="0">Under 18</option>
  <option value="1">19 to 30</option>
  <option value="2">Over 30</option>
</select>
 
atau :
 
{{ Form::select('age', [
   'young' => 'Under 18',
   'adult' => '19 to 30',
   'adult2' => 'Over 30']
) }}
 
hasil :
 
<select name="age">
  <option value="young">Under 18</option>
  <option value="adult">19 to 30</option>
  <option value="adult2">Over 30</option>
</select>
 
 
dengan nilai default :
 
{{ Form::select('number', [0, 1, 2], 2) }}
 
<select name="number">
  <option value="0">0</option>
  <option value="1">1</option>
  <option value="2" selected="selected">2</option>
</select>
 
dengan argument :
 
{{ Form::select('number', [1, 2, 3], null, ['class' => 'field']) }}
 
hasil :
 
<select class="field" name="number">
  <option value="0">1</option>
  <option value="1">2</option>
  <option value="2">3</option>
</select>
 
 
sub array :
 
{{ Form::select('feeling', array(
  'Happy' => array('Joyous', 'Glad', 'Ecstatic'),
  'Sad' => array('Bereaved', 'Pensive', 'Down'),
))}}
 
hasil :
 
<select name="feeling">
  <optgroup label="Happy">
    <option value="0">Joyous</option>
    <option value="1">Glad</option>
    <option value="2">Ecstatic</option>
  </optgroup>
  <optgroup label="Sad">
    <option value="0">Bereaved</option>
    <option value="1">Pensive</option>
    <option value="2">Down</option>
  </optgroup>
</select>
 
15. Cipta kotak pilihan Select Range :

{{ Form::selectRange('number', 10, 15) }}
 
hasil :
 
<select name="number">
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
  <option value="13">13</option>
  <option value="14">14</option>
  <option value="15">15</option>
</select>
 
dengan nilai default dan atibute:
 
{{ Form::selectRange('number', 10, 15, 13, ['class' => 'field']) }}
 
hasil :   
 
<select class="field" name="number">
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
  <option value="13" selected="selected">13</option>
  <option value="14">14</option>
  <option value="15">15</option>
</select> 
 
16. Cipta Kotak pilihan Tahun :

{{ Form::selectYear('year', 2013, 2015) }}
 
hasil :
 
<select name="year">
  <option value="2013">2013</option>
  <option value="2014">2014</option>
  <option value="2015">2015</option>
</select>
 
dengan nilai default dan atribute tambahan :
 
{{ Form::selectYear('year', 2013, 2015, 2014, ['class' => 'field']) }}
 
hasil :
 
<select class="field" name="year">
  <option value="2013">2013</option>
  <option value="2014" selected="selected">2014</option>
  <option value="2015">2015</option>
</select>
 
17. Cipta kotak pilihah Bulan :

{{ Form::selectMonth('month') }}
 
hasil : 

<select name="month">
  <option value="1">January</option>
  <option value="2">February</option>
  <option value="3">March</option>
  <option value="4">April</option>
  <option value="5">May</option>
  <option value="6">June</option>
  <option value="7">July</option>
  <option value="8">August</option>
  <option value="9">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select>
 
dengan nilai default dan atribute tambahan :
 
{{ Form::selectMonth('month', 7, ['class' => 'field']) }}
 
hasil :
 
<select class="field" name="month">
  <option value="1">January</option>
  <option value="2">February</option>
  <option value="3">March</option>
  <option value="4">April</option>
  <option value="5">May</option>
  <option value="6">June</option>
  <option value="7" selected="selected">July</option>
  <option value="8">August</option>
  <option value="9">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select> 
  

17. Cipta input Checkbox :

{{ Form::checkbox('agree') }}
 
hasil :
 
<input name="agree" type="checkbox" value="1">
 
dengan nilai : 
 
{{ Form::checkbox('agree', 'yes') }}
 
hasil :
 
<input name="agree" type="checkbox" value="yes">
 
dengan checked = true :
 
{{ Form::checkbox('agree', 1, true) }}
 
hasil ;
 
<input checked="checked" name="agree" type="checkbox" value="1">
 
dengan argument :
 
{{ Form::checkbox('agree', 1, null, ['class' => 'field']) }}
 
hasil :
 
<input class="field" name="agree" type="checkbox" value="1">
 
        
18. Cipta butang Radio :

{{ Form::radio('single') }}
 
hasil :
 
<input name="single" type="radio" value="single">
 
dengan nama sama :
 
{{ Form::radio('sex', 'male') }}<br>
{{ Form::radio('sex', 'female') }}
 
hasil :
 
<input name="sex" type="radio" value="male"><br>
<input name="sex" type="radio" value="female">
 
dengan nilai default :
 
{{ Form::radio('sex', 'male') }}<br>
{{ Form::radio('sex', 'female', true) }}
 
hasil :
 
<input name="sex" type="radio" value="male"><br>
<input checked="checked" name="sex" type="radio" value="female">
 
dengan atribute :
 
{{ Form::radio('example', 1, true, ['class' => 'field']) }}
 
hasil :
 
<input class="field" checked="checked" name="example" type="radio" value="1">
 
        

19. Cipta Input Reset :

{{ Form::reset('Clear form') }}
 
hasil :
 
<input type="reset" value="Clear form">
 
dengan atribute :
 
{{ Form::reset('Clear form', ['class' => 'form-button']) }}
 
hasil :
 
<input class="form-button" type="reset" value="Clear form">
    
 

20. Cipta input image Image :

{{ Form::image('images/submit-button.jpg') }} hasil : <input src="http://your.url/images/submit-button.jpg" type="image"> dengan name : {{ Form::image('images/submit-button.jpg', 'btnSub') }}   hasil : <input src="http://your.url/images/submit-button.jpg" name="btnSub" type="image"> dengan atribute tambahan : {{ Form::image('images/submit-button.jpg', 'btnSub', ['class' => 'btn']) }} hasil : <input class="btn" src="http://your.url/images/submit-button.jpg" name="btnSub" type="image">   
21. Cipta butang Submit :

{{ Form::submit() }} hasil : <input type="submit"> dengan nilai : {{ Form::submit('Save') }}   hasil : <input type="submit" value="Save"> dengan argument tambahan : {{ Form::submit('Save', array('class' => 'btn')) }} hasil : <input class="btn" type="submit" value="Save">   
 
 

22. Cipta butang Element :

{{ Form::button('Hit Me') }} hasil : <button type="button">Hit Me</button> dengan atribute tambahan : {{ Form::button('Hit Me', array('class' => 'btn')) }} hasil : <button class="btn" type="button">Hit Me</button>    

23. Dapatkan id bagi nama input :

$id = Form::getIdAttribute('fieldname', $attributes);  


24. Dapatkan nilat bagi input :

$value = Form::getValueAttribute($fieldname); jika form adalah model, boleh overwrite jujukan dengan : $value = Form::getValueAttribute($fieldname, $value); 

25. Dapatkan nilai dari Session's Old Input :

$value = Form::old('fieldname');  




26. Memastikan jika Old Input kosong :



if ( ! Form::oldInputIsEmpty()) { // check the old input value for specific fields }  27. Dapatkan Session Store :

$session = Form::getSessionStore(); 


28. Setkan Session Store :

$session = new MySessionHandler; Form::setSessionStore($session); - sesuai digunakan utk test, mock sesi 



29.  Dapatakan nilai pilihan Select


$html = Form::getSelectOption('My Option', 1, 3); hasil : $html = <option value="1">My Option</option>   3 argument :
  1. $display - papar nilai untuk option.
  2. $value - nilai untuk option.
  3. $selected - nilai option yang dipilih.
  
 

No comments:

Post a Comment