Skip to content
Pascal Boucher edited this page Oct 8, 2019 · 38 revisions

Shortcuts

Create a Form - Add Input - Add Node After - Add Node at Rank - Add Node Before - Get Label - Get Option - Get All Options - Get Node - Get All Nodes - Disable All Inputs - Enable All Inputs - Form Request Validation - API Resources

Create a Form

Create a new form and save it in the database.

$form = new \Belvedere\FormMaker\Models\Form\Form();

$form->fill(['name' => 'test', 'description' => 'Optional description'])
    ->method('post')
    ->action('/post_route')
    ->save();

In HTML

<form method="post" action="/post_route">
</form>

You are free to implement the view the way you like. The snippet above is just an example.

Add Input

Add an input of type text to the form. The input will automatically have a random name and id value assign to it.

$form->add('text')->save();

Unless you specify a name and/or id yourself.

$form->add('text')
    ->withHtmlAttributes(['name' => 'test', 'id' => 1])
    ->save();

In HTML

<input type="text" name="test" id="1">

You are free to implement the view the way you like. The snippet above is just an example.

Add Node After

Add a new input after another node specified by a key attribute.

$form->addAfter('second_input', 'text')
    ->withHtmlAttributes(['name' => 'new_input'])
    ->save();

The key attribute can either be an html attribute id, a name, or a value.

In HTML

<!-- Before -->
<input type="text" name="first_input">
<input type="text" name="second_input">
<input type="text" name="third_input">

<!-- After -->
<input type="text" name="first_input">
<input type="text" name="second_input">
<input type="text" name="new_input"> <!-- Inserted here. -->
<input type="text" name="third_input">

You are free to implement the view the way you like. The snippet above is just an example.

Add Node at Rank

Add a new input at a specific rank in the nodes list.

$form->addAtRank(2, 'text')
    ->withHtmlAttributes(['name' => 'new_input'])
    ->save();

In HTML

<!-- Before -->
<input type="text" name="first_input">
<input type="text" name="second_input">
<input type="text" name="third_input">

<!-- After -->
<input type="text" name="first_input">
<input type="text" name="new_input"> <!-- Inserted here. -->
<input type="text" name="second_input">
<input type="text" name="third_input">

You are free to implement the view the way you like. The snippet above is just an example.

Add Node Before

Add a new node before another one specified by a key attribute.

$form->addBefore('second_input', 'text')
    ->withHtmlAttributes(['name' => 'new_input'])
    ->save();

The key attribute can either be an html attribute id, a name, or a value.

In HTML

<!-- Before -->
<input type="text" name="first_input">
<input type="text" name="second_input">
<input type="text" name="third_input">

<!-- After -->
<input type="text" name="first_input">
<input type="text" name="new_input"> <!-- Inserted here. -->
<input type="text" name="second_input">
<input type="text" name="third_input">

You are free to implement the view the way you like. The snippet above is just an example.

Get Label

Get the input label.

$label = $input->label()->first();

$label = $input->label;

The label method is an eloquent relationship, so you can use the shortcut accessing the property directly.

Get Option

Get the option with the specified key property.

$option = $selectInput->option('id');

The key attribute can either be an html attribute id or value.

Get All Options

Get the input options sorted by their position in the ranking.

$options = $input->options();

Get Node

Get the node with the specified key property.

$node = $form->node('id');

The key attribute can either be an html attribute id, name, or value.

Get All Nodes

Get all the form nodes sorted by their position in the ranking.
You can optionally filter them by their input type or even their node type.

$form->nodes(); // all nodes unfiltered 

$form->nodes('inputs'); // all inputs

$form->nodes('siblings'); // all siblings

$form->nodes('text'); // all nodes of type text 

Disable All Inputs

Disable all the form inputs.

$form->disabled();

In HTML

<!-- Before -->
<input type="text" name="first_input">
<input type="text" name="second_input">
<input type="text" name="third_input">

<!-- After -->
<input type="text" name="first_input" disabled>
<input type="text" name="second_input" disabled>
<input type="text" name="third_input" disabled>

You are free to implement the view the way you like. The snippet above is just an example.

Enable All Inputs

Enable all the form inputs.

$form->enabled();

In HTML

<!-- Before -->
<input type="text" name="first_input" disabled>
<input type="text" name="second_input" disabled>
<input type="text" name="third_input" disabled>

<!-- After -->
<input type="text" name="first_input">
<input type="text" name="second_input">
<input type="text" name="third_input">

You are free to implement the view the way you like. The snippet above is just an example.

Form Request Validation

Get the rules associated with the form in the array format expected by Laravel.

$rules = $form->rules();

The returned rules array will have the following format :

[
    'input_name' => 'required|max:10',
    ...
]

You can use this method in the FormRequest rules method

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return $this->form->rules();
}

API Resources

Easily transform all form and nodes model into JSON.

$form->toApi();

$node->toApi();

Behind the scene, laravel-form-maker uses Eloquent: API Resources

Clone this wiki locally