Forms

Convenient way to generate HTML forms

[Since 0.5.0]

Not implemented yet! - Planned release: 0.6.3

Introduction

Sicherheit:

  • Cross-Site Request Forgery (CSRF)
  • Cross-Site Scripting (XSS)
  • URL attack, control codes, invalid UTF-8
  • validate sent data both client-side (JavaScript) and server-side

Form Building

Opening A Form

{{ Form::open(array('url' => 'foo/bar')) }}
    //
{{ Form::close() }}

By default, a POST method will be assumed; however, you are free to specify another method:

echo Form::open(array('url' => 'foo/bar', 'method' => 'put'))

Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form.

You may also open forms that point to named routes or controller actions:

echo Form::open(array('route' => 'route.name'))

echo Form::open(array('action' => 'Controller@method'))

You may pass in route parameters as well:

echo Form::open(array('route' => array('route.name', $user->id)))

echo Form::open(array('action' => array('Controller@method', $user->id)))

Often, you will want to populate a form based on the contents of a model. To do so, use the Form::model method:

echo Form::model($user, array('route' => array('user.update', $user->id)))

Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named email, the user model's email attribute would be set as the value.

Available Methods

The Forms object has these methods:

Method Listing

button()

echo Form::button('Click Me!');

checkbox()

echo Form::radio('name', 'value');

Checkbox that is checked

echo Form::checkbox('name', 'value', true);

email()

echo Form::text('email', 'example@gmail.com');

echo Form::email($name, $value = null, $attributes = array());

file()

echo Form::file('image');

echo Form::file($name, $attributes = array());

<!--
(s. FuelPHP, https://fuelphp.com/docs/classes/upload/usage.html)
-->

hidden()

echo Form::hidden('email', 'example@gmail.com');

label()

echo Form::label('email', 'E-Mail Address');

Specifying Extra HTML Attributes

echo Form::label('email', 'E-Mail Address', array('class' => 'awesome'));

number()

echo Form::number('name', 'value');

password()

echo Form::password('password');

radio()

echo Form::radio('name', 'value');

Radio that is checked

echo Form::radio('name', 'value', true);

submit()

echo Form::submit('Click Me!');

select()

echo Form::select('size', array('L' => 'Large', 'S' => 'Small'));

Drop-Down List with selected default:

echo Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S');

Grouped List:

echo Form::select('animal', array(
    'Cats' => array('leopard' => 'Leopard'),
    'Dogs' => array('spaniel' => 'Spaniel'),
));

Drop-Down List with a range:

echo Form::selectRange('number', 10, 20);

textarea()

echo Form::textarea('comment');

text()

echo Form::text('username');

Form Validation

Not implemented yet! - Planned release: 0.6.7

  • validate sent data both client-side (JavaScript) and server-side

jQuery Form Validator, licensed under MIT License.

TODO


(edit on GitHub)