Fork me on GitHub

Forms

Uses Symfony2 components, that facilitate the creation, display, and validation of forms.

|-- app/
    |-- Forms/          # Forms
      |-- Constraints/  # Rules form validation
      |-- PostForm.php  # Form for adding/editing user posts
      `-- RegForm.php   # Form for the registration of a new user

Consider the main features of the form through the creation of a new message (post).

Creating forms

With the help of action newAction in the controller app/Controllers/BlogController.php we are creating a new message (post) and store it in the database.

An example is shown below.

...
// Create object NewPost and set values
$ormPost = new Post();
$ormPost->setCreated(date($format));
// Create form
$form = $this->createForm(new PostForm(), $ormPost, array('action' => "/blog/new"));
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    $user = $this->getUser();
    $username = $user->getUsername();
    // Create new post
    $models->load('Post', 'newPost', array('username' => $username, 'new_post' => $ormPost));
    ...
}

Creating a form occurs with the function:

// Create form
$form = $this->createForm(new PostForm(), $ormPost, array('action' => "/blog/new"));

The first parameter is the form object of class PostForm app/Forms/PostForm.php, that defines the form characteristics:

  • Method (post or get)
  • Action (/blog/new)
  • Form fields (field type, field options)
  • Default options (data_class,csrf_protection, csrf_field_name, intention etc.)

The second parameter is the object data of class Post app/Models/ORM/Post.php, that defines the characteristics of post table in the database app/Resources/db/app.db:

  • Column (name, type etc. )
  • Relations between tables (ManyToOne, JoinColumn etc.)
  • Validation rules table fields

The third parameter are form parameters set manually.

array('action' => "/blog/new")

Validation of form

Validation of form data takes place on two levels:

  • At the server level
  • At the client level

At the server level, we check the sign of the user to send data and do data validation. Validation rules are defined in the object data of class Post app/Models/ORM/Post.php.

...
if ($form->isSubmitted() && $form->isValid()) {
    ...
}

At the client level in the template app/Views/Controller/blog/new.html.twig we add service FormValidation and validation rules for this service.

...
<script type="text/javascript">
    BSA.ScriptResources.push('FormValidation');
    ...
    addScriptParams('FormValidation',
            {
                form: 'form[name="post"]',
                rules: {
                    "post[created]": {
                        required: true
                    },
                    "post[title]": {
                        required: true,
                        minlength: 5
                    },
                    "post[body]": {
                        required: true
                    }
                }
            });
    ...
</script>
...

Displaying the form

View display the form, you can specify when the service configuration Twig in method _iniProviders of class Bootstrap app/Bootstrap.php

...
// Twig
$app->register(
        new Silex\Provider\TwigServiceProvider(), array(
    ...
    #Global form templates
    'twig.form.templates' => array("Form/bootstrap_3_horizontal_layout.html.twig"),
    ...
));
...

or in the configuration file app/Resources/Config/config.yml

service_providers:
    ...
    twig:
        class: Silex\Provider\TwigServiceProvider
        construct_parameters: ~
        parameters:
            ...
            twig.form.templates:    
                #Global form templates
                - Form/bootstrap_3_horizontal_layout.html.twig
            ...
    ...

where the parameter #Global form templates is set .

Templates that define the appearance of the form are in app/Views/Form.