Web Development Guides and Tutorials

  • facebook
  • twitter
  • github

Building Forms With Nova Forms Helper

5th May 2016 10:20:44 NOVA by Kerry Owston

Nova forms helper

Use the Forms helper in Nova framework to help you build a form quickly.

Get started, find the Form.php helper in directory Nova > System > Helpers and select a view where you would like to build a form.

In the view where you are using the Form Helper echo the open method of the form helper and then echo close method

echo Form::open();
echo Form::close();

This creates the basic open and closing form tags.

The markup for this form on your page will look like this:

<form method="post">
<input type='hidden' name='csrfToken' value='45068cfe65393285f8c5ce600801beb3'>
</form>

By Default the form method is post. An input is also added so this can be used to pass security checks later on.

The form open method can accept parameters which are passed to it in an array. The parameters for this and other form building methods can be found listed in the Forms.php helper.

The form open parameters the ones shown below:

public static function open($params = array())
{
    $o = '<form';
    $o .= (isset($params['id']))        ? " id='{$params['id']}'"                       : '';
    $o .= (isset($params['name']))      ? " name='{$params['name']}'"                   : '';
    $o .= (isset($params['class']))     ? " class='{$params['class']}'"                 : '';
    $o .= (isset($params['onsubmit']))  ? " onsubmit='{$params['onsubmit']}'"           : '';
    $o .= (isset($params['method']))    ? " method='{$params['method']}'"               : ' method="post"';
    $o .= (isset($params['action']))    ? " action='{$params['action']}'"               : '';
    $o .= (isset($params['files']))     ? " enctype='multipart/form-data'"              : '';
    $o .= (isset($params['style']))     ? " style='{$params['style']}'"                 : '';
    $o .= (isset($params['role']))      ? " role='{$params['role']}'"                   : '';
    $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : '';
    $o .= '>';
    //create a token append the form name to the end of the token name if provided
    $o .= "\n<input type='hidden' name='csrfToken".ucwords($params['name'])."' value='".Csrf::makeToken('csrfToken'.ucwords($params['name']))."'>\n";
    return $o."\n";
}

By default only the method is included, it is initially set to post but can easily be overridden. 

To make use of these pass an array to the where the form open method is called, this array should contain key-value pairs. The key is the parameter and the value is the parameters value. This works in practice as shown below:

echo Form::open(array('name' => 'myform', 'files' => true));

Now see how this affects the markup generated by adding these parameters:

<form name='myform' method="post" enctype='multipart/form-data'>

The call to the open method now includes a name for the form and gives files parameter a true value, this gives the form a name and includes the enctype parameter so everything the form opening tag needs to be able to process files submitted as part of the form is now included.

When building the parts of the form you will find this works in a similar way. Below is the input method from the Forms.php helper

public static function input($params = array())
{
     $o  = '<input ';
     $o .= (isset($params['type']))      ? " type='{$params['type']}'"                    : 'type="text"';
     $o .= (isset($params['id']))        ? " id='{$params['id']}'"                        : '';
     $o .= (isset($params['name']))      ? " name='{$params['name']}'"                    : '';
     $o .= (isset($params['class']))     ? " class='{$params['class']}'"                  : '';
     $o .= (isset($params['onclick']))   ? " onclick='{$params['onclick']}'"              : '';
     $o .= (isset($params['onkeypress']))? " onkeypress='{$params['onkeypress']}'"        : '';
     $o .= (isset($params['value']))     ? ' value="' . $params['value'] . '"'            : '';
     $o .= (isset($params['length']))    ? " maxlength='{$params['length']}'"             : '';
     $o .= (isset($params['width']))     ? " style='width:{$params['width']}px;'"         : '';
     $o .= (isset($params['disabled']))  ? " disabled='{$params['disabled']}'"            : '';
     $o .= (isset($params['placeholder']))  ? " placeholder='{$params['placeholder']}'"   : '';
     $o .= (isset($params['accept']))     ? " accept='{$params['accept']}'"               : '';
     $o .= (isset($params['maxlength']))     ? " maxlength='{$params['maxlength']}'"      : '';
     $o .= (isset($params['style']))     ? " style='{$params['style']}'"                  : '';
     $o .= (isset($params['required']))     ? " required='required'"                      : '';
     $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : '';
     $o .= (isset($params['autofocus'])) ? " autofocus"                                   : '';
     $o .= " />\n";
     return $o;
}

This can be passed parameters too but based on specific parameters that can be used with input form tag

This is used in practice in the examples below, the first showing several  input tag possibilities in practice and the second taking advantage of the  enctype  tag used by setting the type to file showing several more input tag possibilities in practice:

echo Form::input(array('name' => 'applicant', 'type' => 'file', 'class' => 'application-form', 'required' => 'required'));
echo Form::input(array('name' => 'application', 'type' => 'text'));

The inclusion of ‘type’ => ‘text’ was not actually necessary as it is included as the default type in the input method.

Lets see this as markup:

<input  type='file' name='applicant' class='application-form' required='required' />
<input  type='text' name='application' />

Another type of form element is select, this is different as it handles the options that can be passed to it as an array.  

This is handled by the form by passing the options as key and value pairs, see example 1:

echo Form::select(array('class' => 'form-control', 'name' => 'years-expereince', 'value' => $_POST['years-expereince'], 'data' => array('one' => 'One', 'two' => 'Two', 'three' => 'Three or more')));
<select name='years-expereince' class='form-control'>
  <option value=''>Select</option>
  <option value='one'>One</option>
  <option value='two'>Two</option>
  <option value='three'>Three or more</option>
</select>

Just passing it keys in the array is possible too, these will be the options of the select menu and the each options value will be generated in numerical order see example 2:

echo Form::select(array('class' => 'form-control', 'name' => 'years-expereince', 'value' => $_POST['years-expereince'], 'data' => array('One', 'Two', 'Three or more')));
<select name='years-expereince' class='form-control'>
  <option value=''>Select</option>
  <option value='0'>One</option>
  <option value='1'>Two</option>
  <option value='2'>Three or more</option>
</select>

A form needs to be able to submit and there is a method for a submit button too.

echo Form::submit(array('value' => 'Submit'));