Form builder

Walrus form builder is a built-in tool to simplify your development. The form builder help you to:

  • Create the form structure
  • Verify each input and display error messages
  • Reroute to another action if the form is validated

All configuration YAML files in walrus are parsed to PHP array, you can create all the files you want in the correct configuration directory

Defining a form
form_signup:
  form:
    method: POST
    action: /my-form
    submit: Send
    structure: list

  fields:
    email:
      type: text
      required: true
      label: Your email
      check:
        validate: /^\w{3,10}$/
    password:
      type: password
      id: helloworld
      check:
        required: true
    sexe:
      type: radio
      options:
        male:
          label: Male
        female:
          label: Female
      check:
        required: true
    newsletter:
      type: checkbox
      options:
        1:
          label: 'Yes'
        0:
          label: 'No'
    categories:
      type: select
      function: HelloController::getSelect
      check:
        required: true
    transport:
      type: select
      options:
        - train
        - cars
        - plane
    message:
      type: textarea
      check:
        min: 50

This is a simple form example to show the biggest part of the form builder features.

  • First you define a name to recognise your form, here "form_signup".
  • Second, you can define some basic form information such as the:
    • method
    • action
    • submit
    • structure
  • Then you can declare all your fields, by default the key of the field is used as the input name, all elements of the input's array are interpreted as html attributes, instead of label used to add a label to the input and check used to validate the input. Possible check attributes are:
    • required: true validate if the input value is set
    • blank: true validate if the input value is blank
    • empty: true/false validate if the input value is set and empty
    • equal_to: value validate if the input value is equal to the string value (can be an int or float too)
    • equal_to: %field validate if the input value is equal to an other field named field
    • even: true validate if the input value is even
    • odd: true validate if the input value is odd
    • greater_than: number validate if the input value is greater than a number
    • greater_than_or_equal_to: number validate if the input value is greater than or equal to a number
    • number: true validate if the input value is a number
    • integer: true validate if the input value is an integer
    • max: number validate if the input value length is equal or smaller to a number
    • min: number validate if the input value length is equal or greater to a number
    • length: number validate if the input value as a length equal to a number
    • max_selected: number validate if the checked checkbox number is less or equal to a number
    • min_selected: number validate if the checked checkbox number is more or equal to a number
    • selected: number validate if the checked checkbox number are is equal to a number

The checkbox, select and radio input fields can have an "options" or "function" attribute describing the input's datas. The options param is a simple key value array, the function is a call to one of your method who must return a PHP array if you want dynamic inputs data

Using a form

To use a form after describing it into a YAML file you need to instantiate the form:

    $form = new WalrusForm('form_signup');

Then you must check the user's data with the check method, this method return null if no data add been submitted, true if the form data are totally valid and an array of error if the form data are invalid, you can automatically reroute to a new function if the form data are valid by giving parameters to the check function

    $form = new WalrusForm('form_signup');

    // simply check the form
    $form->check();

    // if the form data are valid reroute to the controller test and method success with some params
    $form->check('test', 'success', array('hello' => 'world'));

To display the form, use the render method wich return a HTML output

    $form = new WalrusForm('form_signup');
    $form->check();

    echo $form->render();
Errors handling

All errors messages are handled automatically, you can changes the messages in the file config/i18ns/default.yml which is the future internationalisation module of Walrus Framework