创建表单

除了原有的方式创建表单,Laravel提供了一种便捷的方式

  1. <!-- app/views/form.blade.php -->
  2. Form::open(array('url' => 'our/target/route'))
  3. Form::close()

编译后HTML为

  1. <formmethod="POST"action="http://demo.dev/our/target/route"accept-charset="UTF-8">
  2. <inputname="_token"type="hidden"value="83KCsmJF1Z2LMZfhb17ihvt9ks5NEcAwFoRFTq6u">
  3. </form>

自定义提交方法

  1. Form::open(array(
  2. 'url'=>'our/target/route',
  3. 'method'=>'GET',
  4. 'accept-charset'=>'ISO-8859-1'
  5. ))
  6. Form::close()

在表单中需要上传文件

  1. Form::open(array(
  2. 'url'=>'our/target/route',
  3. 'files'=>true
  4. ))
  5. Form::close()

在表单中指向路由别名或控制器

  1. Form::open(array('route'=>'my_route'))
  2. orm::open(array('action'=>'MyController@myAction'))

表单标签

Lable标签

  1. Form::label('first_name','First Name')
  2. //<label for="first_name">First Name</label>
  3. Form::label('first_name','First Name', array('id'=>'first_name'))
  4. //<label for="first_name" id="first_name">First Name</label>

文本域标签

  1. Form::text('first_name','Taylor Otwell')
  2. //<input name="first_name" type="text" value="Taylor Otwell">
  3. orm::textarea('description','Best field ever!')
  4. //<textarea name="description" cols="50" rows="10">Best field ever!</textarea>
  5. Form::password('secret')
  6. //<input name="secret" type="password" value="">
  7. Form::email('email','me@email.com')
  8. //<input name="email" type="email" value="me@email.com">
  9. Form::hidden('panda','luishi')
  10. //<input name="panda" type="hidden" value="luishi">

多选框标签

  1. Form::checkbox('pandas_are_cute','1',true)
  2. //<input checked="checked" name="pandas_are_cute" type="checkbox" value="1">

单选框标签

  1. Form::radio('panda_colour','red',true)
  2. //<input checked="checked" name="panda_colour" type="radio" value="red">

下拉菜单

  1. Form::select('panda_colour', array(
  2. 'red'=>'Red',
  3. 'black'=>'Black',
  4. 'white'=>'White'
  5. ),'red')
  6. //<select id="panda_colour" name="panda_colour">
  7. <option value="red" selected="selected">Red</option>
  8. <option value="black">Black</option>
  9. <option value="white">White</option>
  10. </select>
  11. //菜单分组
  12. Form::select('bear', array(
  13. 'Panda'=> array(
  14. 'red'=>'Red',
  15. 'black'=>'Black',
  16. 'white'=>'White'
  17. ),
  18. 'Character'=> array(
  19. 'pooh'=>'Pooh',
  20. 'baloo'=>'Baloo'
  21. )
  22. ),'black')

文件上传标签

  1. Form::file('avatar')
  2. //<input name="avatar" type="file">

按钮标签

  1. Form::submit('Save')
  2. //<input type="submit" value="Save">
  3. Form::button('Smile')
  4. //<button type="button">Smile</button>
  5. Form::reset('Clear')
  6. <input type="reset" value="Clear">

图片标签

  1. Form::image(asset('my/image.gif','submit'))
  2. //<input src="https://demo.dev/my/image.gif" type="image">

自定义宏

定义宏

  1. Form::macro('fullName',function()
  2. {
  3. return'<p>Full name: <input type="text" name="full_name"></p>';
  4. });

使用宏

  1. Form::fullName();

使用参数

  1. Form::macro('fullName',function($name)
  2. {
  3. return'<p>Full name: <input type="text" name="'.$name.'"></p>';
  4. });
  5. Form::fullName('my_field')

CSRF防御机制

使用Form::open()会自动创建添加CSRF令牌,也可以手动添加CSRF令牌到表单中

  1. <formaction=""method="POST">
  2. </form>

使用默认的CSRF过滤器

  1. Route::post('/handle-form', array('before'=>'csrf',function()
  2. {
  3. // Handle our posted form data.
  4. }));

结束