x-larastrap::form is one of the most complex (and useful) components in Larastrap. It plays well with Field components to provide a powerful tool to organize, populate and handle the forms of your Laravel application.
As all other Containers, forms have an obj parameter which can be populated with an object (eventually: an Eloquent model). This parameter is inherited by all child input fields, which values are then inited with the proper attributes matched by their name.
When obj
is null
, all child inputs are left with their explicit value
(if set at all).
@php
$obj = (object) ['id' => 1234, 'name' => 'Foo', 'surname' => 'Baz', 'bio' => 'A very smart guy!', 'email' => 'foo@bar.baz', 'country' => 'it', 'colors' => ['red', 'blue']];
$countries = ['fr' => 'France', 'de' => 'Germany', 'it' => 'Italy'];
@endphp
<x-larastrap::form :obj="$obj">
<x-larastrap::hidden name="id" />
<x-larastrap::number name="id" label="ID" disabled="true" />
<x-larastrap::text name="name" label="Name" />
<x-larastrap::text name="surname" label="Surname" value="Overwritten value (should be 'Baz')" />
<x-larastrap::textarea name="bio" label="Bio" />
<x-larastrap::email name="email" label="EMail" help="Must be a valid email address" />
<x-larastrap::check name="check" label="Check" />
<x-larastrap::select name="country" label="Country (as select)" :options="$countries" />
<x-larastrap::radios name="country" label="Country (as radio)" :options="$countries" color="danger" />
<x-larastrap::checks name="colors" label="Colors" :options="['red' => 'Red', 'green' => 'Green', 'blue' => 'Blue']" color="warning" />
</x-larastrap::form>
<form id="form-45c48cce2e2d7fbdea1afc51c7c6ad26" class="" method="post" name="form-45c48cce2e2d7fbdea1afc51c7c6ad26">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off"> <input id="hidden-6491723061ac371035d589cd7e8670bb" type="hidden" class="" name="id" value="1234">
<div class="row mb-3">
<label for="input-b179cf12ac2ab3d577b8374737b085e7" class="col-4 col-form-label">ID</label>
<div class="col-8">
<input id="input-b179cf12ac2ab3d577b8374737b085e7" type="number" class="form-control-plaintext" name="id" value="1234" disabled>
</div>
</div>
<div class="row mb-3">
<label for="input-df7fa243a58c27c3b11111bb558d5335" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-df7fa243a58c27c3b11111bb558d5335" type="text" class="form-control" name="name" value="Foo">
</div>
</div>
<div class="row mb-3">
<label for="input-9d665b0eaca757f4e9c7c29a3fb02010" class="col-4 col-form-label">Surname</label>
<div class="col-8">
<input id="input-9d665b0eaca757f4e9c7c29a3fb02010" type="text" class="form-control" name="surname" value="Overwritten value (should be 'Baz')">
</div>
</div>
<div class="row mb-3">
<label for="textarea-e1cdd82d34a1dcbf3a7a219eaea693ec" class="col-4 col-form-label">Bio</label>
<div class="col-8">
<textarea id="textarea-e1cdd82d34a1dcbf3a7a219eaea693ec" class="form-control" name="bio">A very smart guy!</textarea>
</div>
</div>
<div class="row mb-3">
<label for="input-bceaf0a7c9348a3757a5dd36c46504e1" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-bceaf0a7c9348a3757a5dd36c46504e1" type="email" class="form-control" name="email" value="foo@bar.baz" aria-describedby="help-input-bceaf0a7c9348a3757a5dd36c46504e1">
<div class="form-text" id="help-input-bceaf0a7c9348a3757a5dd36c46504e1">
Must be a valid email address
</div>
</div>
</div>
<div class="row mb-3">
<label for="check-cd6e8e4285c9c1458a8ac68a1263aeb3" class="col-4 col-form-label">Check</label>
<div class="col-8">
<div class="form-check mt-1">
<input id="check-cd6e8e4285c9c1458a8ac68a1263aeb3" type="checkbox" class="form-check-input" name="check">
</div>
</div>
</div>
<div class="row mb-3">
<label for="select-dad989e78c01aa0138bee756384017b8" class="col-4 col-form-label">Country (as select)</label>
<div class="col-8">
<select id="select-dad989e78c01aa0138bee756384017b8" class="form-select" name="country">
<option value="fr">
France
</option>
<option value="de">
Germany
</option>
<option value="it" selected>
Italy
</option>
</select>
</div>
</div>
<div class="row mb-3">
<label for="radios-a9ece4e3141b8ba2bebdecf7b7a90497" class="col-4 col-form-label">Country (as radio)</label>
<div class="col-8">
<div class="btn-group" role="group">
<input type="radio" class="btn-check" name="country" id="radios-a9ece4e3141b8ba2bebdecf7b7a90497-0309a6c666a7a803fdb9db95de71cf01" autocomplete="off" value="fr"> <label class="btn btn-danger" for="radios-a9ece4e3141b8ba2bebdecf7b7a90497-0309a6c666a7a803fdb9db95de71cf01">France</label> <input type="radio" class="btn-check" name="country" id="radios-a9ece4e3141b8ba2bebdecf7b7a90497-d8b00929dec65d422303256336ada04f" autocomplete="off" value="de"> <label class="btn btn-danger" for="radios-a9ece4e3141b8ba2bebdecf7b7a90497-d8b00929dec65d422303256336ada04f">Germany</label> <input type="radio" class="btn-check" name="country" id="radios-a9ece4e3141b8ba2bebdecf7b7a90497-1007e1b7f894dfbf72a0eaa80f3bc57e" autocomplete="off" value="it" checked> <label class="btn btn-danger" for="radios-a9ece4e3141b8ba2bebdecf7b7a90497-1007e1b7f894dfbf72a0eaa80f3bc57e">Italy</label>
</div>
</div>
</div>
<div class="row mb-3">
<label for="checks-a9f38d87ed8a1c284ef7372204b298ca" class="col-4 col-form-label">Colors</label>
<div class="col-8">
<div class="btn-group" role="group">
<input type="checkbox" class="btn-check" name="colors[]" id="checks-a9f38d87ed8a1c284ef7372204b298ca-ee38e4d5dd68c4e440825018d549cb47" autocomplete="off" value="red" checked> <label class="btn btn-warning" for="checks-a9f38d87ed8a1c284ef7372204b298ca-ee38e4d5dd68c4e440825018d549cb47">Red</label> <input type="checkbox" class="btn-check" name="colors[]" id="checks-a9f38d87ed8a1c284ef7372204b298ca-d382816a3cbeed082c9e216e7392eed1" autocomplete="off" value="green"> <label class="btn btn-warning" for="checks-a9f38d87ed8a1c284ef7372204b298ca-d382816a3cbeed082c9e216e7392eed1">Green</label> <input type="checkbox" class="btn-check" name="colors[]" id="checks-a9f38d87ed8a1c284ef7372204b298ca-9594eec95be70e7b1710f730fdda33d9" autocomplete="off" value="blue" checked> <label class="btn btn-warning" for="checks-a9f38d87ed8a1c284ef7372204b298ca-9594eec95be70e7b1710f730fdda33d9">Blue</label>
</div>
</div>
</div>
<div class="col-12 text-end">
<button id="button-ee00f453bfc9aedabb3fb92d923cd14a" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
When using obj , note that the name of the children Fields can be expressed in "dot notation" to access arrays casted by the Eloquent model or, anyway, the keys of the used associative array.
@php
/*
class User
{
protected $casts = [
'attributes' => 'array',
];
}
*/
$user = App\Models\User::inRandomOrder()->first();
$arbitrary = (object) [
'name' => 'John Doe',
'attributes' => [
'external_id' => '1234567890',
]
];
@endphp
<x-larastrap::form :obj="$user">
<div class="alert alert-info">
Example with Eloquent model
</div>
<x-larastrap::text name="name" label="Name" />
<x-larastrap::text name="attributes.external_id" label="External ID" />
</x-larastrap::form>
<hr>
<x-larastrap::form :obj="$arbitrary">
<div class="alert alert-info">
Example with plain object
</div>
<x-larastrap::text name="name" label="Name" />
<x-larastrap::text name="attributes.external_id" label="External ID" />
</x-larastrap::form>
<form id="form-17e62166fc8586dfa4d1bc0e1742c08b" class="" method="post" name="form-17e62166fc8586dfa4d1bc0e1742c08b">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="alert alert-info">
Example with Eloquent model
</div>
<div class="row mb-3">
<label for="input-03b706b090cf1b3eaebb1d587203cd64" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-03b706b090cf1b3eaebb1d587203cd64" type="text" class="form-control" name="name" value="Erica Krajcik">
</div>
</div>
<div class="row mb-3">
<label for="input-1b80a9797ba464dc07f00ecfbb1393f0" class="col-4 col-form-label">External ID</label>
<div class="col-8">
<input id="input-1b80a9797ba464dc07f00ecfbb1393f0" type="text" class="form-control" name="attributes[external_id]" value="BWNDIEjL0f">
</div>
</div>
<div class="col-12 text-end">
<button id="button-cb220d667c9086fc1146cfac55d4637c" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
<hr>
<form id="form-2838023a778dfaecdc212708f721b788" class="" method="post" name="form-2838023a778dfaecdc212708f721b788">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="alert alert-info">
Example with plain object
</div>
<div class="row mb-3">
<label for="input-1952a99a88df2f54cc3cf7536c3ea7bc" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-1952a99a88df2f54cc3cf7536c3ea7bc" type="text" class="form-control" name="name" value="John Doe">
</div>
</div>
<div class="row mb-3">
<label for="input-bde78eda54f22b298278110073682b8b" class="col-4 col-form-label">External ID</label>
<div class="col-8">
<input id="input-bde78eda54f22b298278110073682b8b" type="text" class="form-control" name="attributes[external_id]" value="1234567890">
</div>
</div>
<div class="col-12 text-end">
<button id="button-b10d50b908b0ae004b8dde654b090a60" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
action
and method
parameters are equivalent to the classic HTML attributes for forms. But you can specify once a default method
in your configuration file (by default it is POST
).
Any method
different than POST
or GET
translates to @method('XYZ')
, as supported by Laravel.
<x-larastrap::form method="DELETE" :buttons="[['label' => 'Delete!', 'color' => 'danger', 'type' => 'submit']]">
<div class="alert alert-danger">This is a delete form!</div>
</x-larastrap::form>
<form id="form-3295c76acbf4caaed33c36b1b5fc2cb1" class="" method="post" name="form-3295c76acbf4caaed33c36b1b5fc2cb1">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off"> <input type="hidden" name="_method" value="DELETE">
<div class="alert alert-danger">
This is a delete form!
</div>
<div class="col-12 text-end">
<button id="button-6fb000041d480425bfedc404ffef02fa" class="btn btn-danger">Delete!</button>
</div>
</form>
A convenient shortcut for action is route , where you specify just the name of the intended route which doesn't require addictional parameters.
<x-larastrap::form route="user.store">
<x-larastrap::text name="name" label="Name" />
<x-larastrap::email name="email" label="EMail" />
</x-larastrap::form>
<form id="form-32bb90e8976aab5298d5da10fe66f21d" class="" method="post" action="https://larastrap.madbob.org/user" name="form-32bb90e8976aab5298d5da10fe66f21d">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="row mb-3">
<label for="input-c1dff2800b164e7e85e49a1c26c930db" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-c1dff2800b164e7e85e49a1c26c930db" type="text" class="form-control" name="name" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-7b938b40f3711abfa04da219ef92d465" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-7b938b40f3711abfa04da219ef92d465" type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="col-12 text-end">
<button id="button-c8a994c1c309bb098344edf922f9c3e9" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
A bit more articulated is baseaction : this is intended to be the prefix of a Laravel's Resource Controller.
In the following example, when obj
is set to NULL
it is assigned as action
the value route('user.store')
; otherwise, if obj
is a valid object, it will become route('user.update', $obj->id)
. This is handy to have a single edit form to both create and update the same kind of model.
Of course, when in update mode, the proper PUT
method
is used.
<x-larastrap::form :obj="null" baseaction="user">
<div class="alert alert-info">
This form creates a new model.
</div>
<x-larastrap::text name="name" label="Name" />
<x-larastrap::email name="email" label="EMail" />
</x-larastrap::form>
<br>
@php
$obj = (object) [
'id' => 123,
'name' => 'Foo Bar',
'email' => 'foobar@example.com',
];
@endphp
<x-larastrap::form :obj="$obj" baseaction="user">
<div class="alert alert-info">
This form updates the existing model.
</div>
<x-larastrap::text name="name" label="Name" />
<x-larastrap::email name="email" label="EMail" />
</x-larastrap::form>
<form id="form-2a38a4a9316c49e5a833517c45d31070" class="" method="post" action="https://larastrap.madbob.org/user" name="form-2a38a4a9316c49e5a833517c45d31070">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="alert alert-info">
This form creates a new model.
</div>
<div class="row mb-3">
<label for="input-7285f82548c83a2ebe70e8157c7fcaaf" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-7285f82548c83a2ebe70e8157c7fcaaf" type="text" class="form-control" name="name" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-5902123e1a8dc335c932e7d58a068b80" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-5902123e1a8dc335c932e7d58a068b80" type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="col-12 text-end">
<button id="button-4bdf0fedd9b0340ba0e7403769af43ab" class="btn btn-primary" type="submit">Save</button>
</div>
</form><br>
<form id="form-26657d5ff9020d2abefe558796b99584" class="" method="post" action="https://larastrap.madbob.org/user/123" name="form-26657d5ff9020d2abefe558796b99584">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off"> <input type="hidden" name="_method" value="PUT">
<div class="alert alert-info">
This form updates the existing model.
</div>
<div class="row mb-3">
<label for="input-d3a3b5c6a23e61e045bc4535b6f1d01c" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-d3a3b5c6a23e61e045bc4535b6f1d01c" type="text" class="form-control" name="name" value="Foo Bar">
</div>
</div>
<div class="row mb-3">
<label for="input-8a855d337bb7306e7790d76f85a45e23" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-8a855d337bb7306e7790d76f85a45e23" type="email" class="form-control" name="email" value="foobar@example.com">
</div>
</div>
<div class="col-12 text-end">
<button id="button-6ce414a037819d5f98fe524fa72d12f2" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
The form's buttons (notably: the submit button) are generally not included within the x-larastrap::form node but are defined using the buttons parameter, which has to be an array of arrays, each rappresenting a button and with the parameters for the relevant x-larastrap::button.
<x-larastrap::form :buttons="[['color' => 'success', 'label' => 'Submit', 'attributes' => ['type' => 'submit']], ['color' => 'danger', 'label' => 'Reset', 'attributes' => ['type' => 'reset']]]">
<x-larastrap::text name="firstname" label="First Name" />
<x-larastrap::text name="lastname" label="Last Name" />
<x-larastrap::email name="email" label="EMail" />
</x-larastrap::form>
<form id="form-a97da629b098b75c294dffdc3e463904" class="" method="post" name="form-a97da629b098b75c294dffdc3e463904">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="row mb-3">
<label for="input-0c271a374420f5395baf2f28ffe4864d" class="col-4 col-form-label">First Name</label>
<div class="col-8">
<input id="input-0c271a374420f5395baf2f28ffe4864d" type="text" class="form-control" name="firstname" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-2fd784561caf87399046d6840c628ad5" class="col-4 col-form-label">Last Name</label>
<div class="col-8">
<input id="input-2fd784561caf87399046d6840c628ad5" type="text" class="form-control" name="lastname" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-bdfca9f84bc10fadc82c2713f3a2aded" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-bdfca9f84bc10fadc82c2713f3a2aded" type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="col-12 text-end">
<button id="button-b438430b123a309a85f31ace101b72b3" class="btn btn-success" type="submit">Submit</button> <button id="button-5a68beff6500d9fc715223a2fddee4dc" class="btn btn-danger" type="reset">Reset</button>
</div>
</form>
A peculiar parameter which can be used for those buttons is element
, that permits to use a different node type for the element. By default it is of course larastrap::button
, but can be any other Blade's Component identifier (remember to prefix Larastrap's widgets with larastrap::
).
@php
$buttons = [
['element' => 'larastrap::link', 'label' => 'This is a Link', 'href' => 'https://larastrap.madbob.org/'],
['color' => 'success', 'label' => 'Submit', 'attributes' => ['type' => 'submit']]
];
@endphp
<x-larastrap::form :buttons="$buttons">
<x-larastrap::text name="firstname" label="First Name" />
<x-larastrap::text name="lastname" label="Last Name" />
<x-larastrap::email name="email" label="EMail" />
</x-larastrap::form>
<form id="form-a0a080f42e6f13b3a2df133f073095dd" class="" method="post" name="form-a0a080f42e6f13b3a2df133f073095dd">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="row mb-3">
<label for="input-dec08a7bf8a08a3e1767c7a8ead42fc3" class="col-4 col-form-label">First Name</label>
<div class="col-8">
<input id="input-dec08a7bf8a08a3e1767c7a8ead42fc3" type="text" class="form-control" name="firstname" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-aa4bc72f024aaafea26d799fc3a027da" class="col-4 col-form-label">Last Name</label>
<div class="col-8">
<input id="input-aa4bc72f024aaafea26d799fc3a027da" type="text" class="form-control" name="lastname" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-9a6626643d2525d8f82d093aff770857" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-9a6626643d2525d8f82d093aff770857" type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="col-12 text-end">
<a id="link-198a5c1c884e86132b0c2e7928ac02f6" class="btn btn-primary" href="https://larastrap.madbob.org/">This is a Link</a> <button id="button-7e7950e299e5e557d2c59ed42d40ee20" class="btn btn-success" type="submit">Submit</button>
</div>
</form>
Leveraging the Typography widget, you are actually able to append in the Form's footer any kind of text. Even better: any kind of text generated from the contextual obj .
@php
$user = App\Models\User::inRandomOrder()->first();
$buttons = [
['element' => 'larastrap::t', 'node' => 'small', 'name' => 'updated_at', 'prelabel' => 'Last update: ', 'classes' => ['float-start']],
['color' => 'success', 'label' => 'Submit', 'attributes' => ['type' => 'submit']]
];
@endphp
<x-larastrap::form :buttons="$buttons" :obj="$user">
<x-larastrap::text name="name" label="Name" />
<x-larastrap::email name="email" label="EMail" />
<hr>
</x-larastrap::form>
<form id="form-7f1de29e6da19d22b51c68001e7e0e54" class="" method="post" name="form-7f1de29e6da19d22b51c68001e7e0e54">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="row mb-3">
<label for="input-c4dce9d2464104f8cec369e7d23e3549" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-c4dce9d2464104f8cec369e7d23e3549" type="text" class="form-control" name="name" value="Erica Krajcik">
</div>
</div>
<div class="row mb-3">
<label for="input-adedbffcb8e7945424df002650953d90" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-adedbffcb8e7945424df002650953d90" type="email" class="form-control" name="email" value="genevieve.reinger@example.com">
</div>
</div>
<hr>
<div class="col-12 text-end">
<small id="basenode-f960ab348a12ac28a9b02dd63623b3bb" class="float-start">Last update: 2024-08-04 08:08:24</small> <button id="button-60ed6c7026b3ab3f967fc2d8147896c6" class="btn btn-success" type="submit">Submit</button>
</div>
</form>
The buttons_align
parameter permit to define where to put buttons
within the form: by default their are aligned to end
, but can also be at start
or center
.
<x-larastrap::form formview="vertical" buttons_align="start">
<x-larastrap::text name="firstname" label="First Name" />
<x-larastrap::text name="lastname" label="Last Name" />
<x-larastrap::email name="email" label="EMail" />
</x-larastrap::form>
<form id="form-7ef605fc8dba5425d6965fbd4c8fbe1f" class="" method="post" name="form-7ef605fc8dba5425d6965fbd4c8fbe1f">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="mb-3">
<label for="input-c71eb4841e6f9fb39469851abbae2965" class="form-label">First Name</label> <input id="input-c71eb4841e6f9fb39469851abbae2965" type="text" class="form-control" name="firstname" value="">
</div>
<div class="mb-3">
<label for="input-d29e9d912d3604bfa0bf98cb4035115b" class="form-label">Last Name</label> <input id="input-d29e9d912d3604bfa0bf98cb4035115b" type="text" class="form-control" name="lastname" value="">
</div>
<div class="mb-3">
<label for="input-c1cef4a5c00042e3bba25db38628993e" class="form-label">EMail</label> <input id="input-c1cef4a5c00042e3bba25db38628993e" type="email" class="form-control" name="email" value="">
</div>
<div class="col-12 text-start">
<button id="button-4b0def0aba2ccaad0d9496b6cf5c4d5c" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
It is possible to organize the layout of the Form in multiple ways, using the formview
parameter. horizontal
is the default value and behavior, but it is possible to opt for inline
; it this case, please note that labels are converted in placeholders to accomodate into the row.
<x-larastrap::form formview="inline">
<x-larastrap::text name="firstname" label="First Name" />
<x-larastrap::text name="lastname" label="Last Name" />
</x-larastrap::form>
<form id="form-9766527f2b5d3e95d4a733fcfb77bd7e" class="row row-cols-lg-auto g-3 align-items-center" method="post" name="form-9766527f2b5d3e95d4a733fcfb77bd7e">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="col-auto">
<input id="input-d39c61805759e798dece8f28d26fab61" type="text" class="form-control" name="firstname" value="" placeholder="First Name">
</div>
<div class="col-auto">
<input id="input-ef1415d0514d15a4c171c4d990e27ea5" type="text" class="form-control" name="lastname" value="" placeholder="Last Name">
</div>
<div class="col-12 text-end">
<button id="button-e4e201659911a7863b5d1b54ae3de6c0" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
Similar, but not equal, is the squeeze
alternative, which enforces the same named attribute on all children Fields and, as a conseguence, will omit the wrapping markup for each input element.
This is in particular suggested when your form is mostly formatted into a table, and you want to omit explicit label for each single field.
<x-larastrap::form formview="squeeze">
<table class="table">
<tbody>
@foreach(App\Models\User::take(3)->get() as $user)
<x-larastrap::enclose :obj="$user">
<tr>
<td>
<x-larastrap::text name="name" />
</td>
<td>
<x-larastrap::text name="email" />
</td>
</tr>
</x-larastrap::enclose>
@endforeach
</tbody>
</table>
</x-larastrap::form>
<form id="form-f7e6c85504ce6e82442c770f7c8606f0" class="" method="post" name="form-f7e6c85504ce6e82442c770f7c8606f0">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<table class="table">
<tbody>
<tr>
<td>
<input id="input-ceb290e0a40fdd95262863b88b3d2c0e" type="text" class="form-control" name="name" value="Jules Wunsch">
</td>
<td>
<input id="input-514153036bb05103e24ee89da906168d" type="text" class="form-control" name="email" value="daniel.arlie@example.net">
</td>
</tr>
<tr>
<td>
<input id="input-7f46088c30da5d47d119723bbb1192f8" type="text" class="form-control" name="name" value="Ebba Olson I">
</td>
<td>
<input id="input-3e1f08e6e1f0f66a919a538eb3753601" type="text" class="form-control" name="email" value="ebert.jeremy@example.net">
</td>
</tr>
<tr>
<td>
<input id="input-0cfddc3baafb12f82ab9b7f610134c3d" type="text" class="form-control" name="name" value="Jessyca Goyette">
</td>
<td>
<input id="input-d19c663b916854737c8185b27d05eea8" type="text" class="form-control" name="email" value="casper.lorine@example.net">
</td>
</tr>
</tbody>
</table>
<div class="col-12 text-end">
<button id="button-72bbbf0c63ecb43cbc01389ad05ab7c3" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
Another - and more classical - option is the vertical
value for formview
.
<x-larastrap::form formview="vertical">
<x-larastrap::text name="firstname" label="First Name" />
<x-larastrap::text name="lastname" label="Last Name" />
<x-larastrap::email name="email" label="EMail" />
<x-larastrap::check name="check" label="Check" inline />
<x-larastrap::checks name="checks" label="Checks" :options="['One', 'Two', 'Three']" />
</x-larastrap::form>
<form id="form-0aa1883c6411f7873cb83dacb17b0afc" class="" method="post" name="form-0aa1883c6411f7873cb83dacb17b0afc">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="mb-3">
<label for="input-c55b05ade1ce7e54fa6a2f1208533630" class="form-label">First Name</label> <input id="input-c55b05ade1ce7e54fa6a2f1208533630" type="text" class="form-control" name="firstname" value="">
</div>
<div class="mb-3">
<label for="input-892e15526e41aa875187310043fe7904" class="form-label">Last Name</label> <input id="input-892e15526e41aa875187310043fe7904" type="text" class="form-control" name="lastname" value="">
</div>
<div class="mb-3">
<label for="input-cc875d7ea8253193ed8fec6d360549ae" class="form-label">EMail</label> <input id="input-cc875d7ea8253193ed8fec6d360549ae" type="email" class="form-control" name="email" value="">
</div>
<div class="mb-3">
<div class="form-check mt-1">
<input id="check-64ad8d4d62f8997ee54a95ba92212d26" type="checkbox" class="form-check-input" name="check"> <label for="check-64ad8d4d62f8997ee54a95ba92212d26" class="">Check</label>
</div>
</div>
<div class="mb-3">
<label for="checks-c0fbded03e94ca93f960315c80e6af23" class="form-label">Checks</label>
<div class="d-block btn-group" role="group">
<input type="checkbox" class="btn-check" name="checks[]" id="checks-c0fbded03e94ca93f960315c80e6af23-06c2cea18679d64399783748fa367bdd" autocomplete="off" value="0"> <label class="btn btn-outline-primary" for="checks-c0fbded03e94ca93f960315c80e6af23-06c2cea18679d64399783748fa367bdd">One</label> <input type="checkbox" class="btn-check" name="checks[]" id="checks-c0fbded03e94ca93f960315c80e6af23-aada29daee1d64ed0fe907043855cb7e" autocomplete="off" value="1"> <label class="btn btn-outline-primary" for="checks-c0fbded03e94ca93f960315c80e6af23-aada29daee1d64ed0fe907043855cb7e">Two</label> <input type="checkbox" class="btn-check" name="checks[]" id="checks-c0fbded03e94ca93f960315c80e6af23-ca8a2087e5557e317599344687a57391" autocomplete="off" value="2"> <label class="btn btn-outline-primary" for="checks-c0fbded03e94ca93f960315c80e6af23-ca8a2087e5557e317599344687a57391">Three</label>
</div>
</div>
<div class="col-12 text-end">
<button id="button-56404ee3b72e70099174b4c72179cf56" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
Anyway, you can use the formview attribute on each individual input component, to provide more complex layout.
<x-larastrap::form formview="horizontal">
<x-larastrap::text name="firstname" label="First Name" />
<x-larastrap::text name="lastname" label="Last Name" />
<div class="row">
<div class="col">
<x-larastrap::email name="email" label="EMail" formview="vertical" />
<x-larastrap::tel name="phone" label="Phone" formview="vertical" />
</div>
<div class="col">
<x-larastrap::text name="address" label="Address" formview="vertical" />
<x-larastrap::text name="city" label="City" formview="vertical" />
</div>
</div>
</x-larastrap::form>
<form id="form-6f3ef77ac0e3619e98159e9b6febf557" class="" method="post" name="form-6f3ef77ac0e3619e98159e9b6febf557">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="row mb-3">
<label for="input-2471d45297416ddbbf4f7ff5092149b6" class="col-4 col-form-label">First Name</label>
<div class="col-8">
<input id="input-2471d45297416ddbbf4f7ff5092149b6" type="text" class="form-control" name="firstname" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-587828b2d9ea40099cbf90dfff25b2de" class="col-4 col-form-label">Last Name</label>
<div class="col-8">
<input id="input-587828b2d9ea40099cbf90dfff25b2de" type="text" class="form-control" name="lastname" value="">
</div>
</div>
<div class="row">
<div class="col">
<div class="mb-3">
<label for="input-8a7836bf8bc5142c426853105d001f65" class="form-label">EMail</label> <input id="input-8a7836bf8bc5142c426853105d001f65" type="email" class="form-control" name="email" value="">
</div>
<div class="mb-3">
<label for="input-ae1f0e40a7ffcf4e0724e640e573b1fb" class="form-label">Phone</label> <input id="input-ae1f0e40a7ffcf4e0724e640e573b1fb" type="tel" class="form-control" name="phone" value="">
</div>
</div>
<div class="col">
<div class="mb-3">
<label for="input-d874ea1d0a19c95b1de3fdacdea840b9" class="form-label">Address</label> <input id="input-d874ea1d0a19c95b1de3fdacdea840b9" type="text" class="form-control" name="address" value="">
</div>
<div class="mb-3">
<label for="input-ecb931da2f62def3929ecec1f20260fe" class="form-label">City</label> <input id="input-ecb931da2f62def3929ecec1f20260fe" type="text" class="form-control" name="city" value="">
</div>
</div>
</div>
<div class="col-12 text-end">
<button id="button-17ebada32c2b6974691c4e476ab1f460" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
Larastrap's forms have built-in integration with Laravel's Validation and Bootstrap's Validation, and a few parameters to deal with it.
By default error_handling
is set to true
and error_bag
is set to default
. When validation fails, and the error bag is populated, fields involved in the form but considered as valid are pre-populated with the previously submitted values through the proper Laravel helpers.
false
. To change the name of the "error bag" used by your server-side Laravel's validator, set error_bag
accordingly.
@php
// Server side validation for this form is defined as:
//
// $request->validateWithBag('special', [
// 'name' => 'required',
// 'email' => 'required|email|max:255',
// 'description' => 'required',
// 'select' => 'required',
// 'check' => 'accepted',
// 'checks' => 'required',
// 'radios' => 'required',
// 'checklist' => 'required',
// ]);
@endphp
<x-larastrap::form action="/validated" error_bag="special">
<x-larastrap::text name="name" label="Name" />
<x-larastrap::email name="email" label="EMail" />
<x-larastrap::textarea name="description" label="Description" />
<x-larastrap::select name="select" label="Select" :options="['' => 'Choose One', 'First', 'Second']" />
<x-larastrap::check name="check" label="Check" />
<x-larastrap::checks name="checks" label="Checks" :options="['One', 'Two', 'Three']" />
<x-larastrap::radios name="radios" label="Radio" :options="['One', 'Two', 'Three']" />
<x-larastrap::checklist name="checklist" label="Check List" :options="['One', 'Two', 'Three']" />
</x-larastrap::form>
<form id="form-235c2f7949a684897e2386988b7c04d5" class="" method="post" action="/validated" name="form-235c2f7949a684897e2386988b7c04d5">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="row mb-3">
<label for="input-2dfecc03f03341df171b8f73b74b7135" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-2dfecc03f03341df171b8f73b74b7135" type="text" class="form-control" name="name" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-fd50fcaf3cf58a9eab923c0ca82ba249" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-fd50fcaf3cf58a9eab923c0ca82ba249" type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="row mb-3">
<label for="textarea-b8ebc78a71d478cd464601e37a2b36ba" class="col-4 col-form-label">Description</label>
<div class="col-8">
<textarea id="textarea-b8ebc78a71d478cd464601e37a2b36ba" class="form-control" name="description"></textarea>
</div>
</div>
<div class="row mb-3">
<label for="select-c79a1db4a812121f61f588e110e788a4" class="col-4 col-form-label">Select</label>
<div class="col-8">
<select id="select-c79a1db4a812121f61f588e110e788a4" class="form-select" name="select">
<option value="" selected>
Choose One
</option>
<option value="0">
First
</option>
<option value="1">
Second
</option>
</select>
</div>
</div>
<div class="row mb-3">
<label for="check-f850a996652e0580a520bc47a9351f3c" class="col-4 col-form-label">Check</label>
<div class="col-8">
<div class="form-check mt-1">
<input id="check-f850a996652e0580a520bc47a9351f3c" type="checkbox" class="form-check-input" name="check">
</div>
</div>
</div>
<div class="row mb-3">
<label for="checks-d09223e97375de6a684575e31bd1015d" class="col-4 col-form-label">Checks</label>
<div class="col-8">
<div class="btn-group" role="group">
<input type="checkbox" class="btn-check" name="checks[]" id="checks-d09223e97375de6a684575e31bd1015d-06c2cea18679d64399783748fa367bdd" autocomplete="off" value="0"> <label class="btn btn-outline-primary" for="checks-d09223e97375de6a684575e31bd1015d-06c2cea18679d64399783748fa367bdd">One</label> <input type="checkbox" class="btn-check" name="checks[]" id="checks-d09223e97375de6a684575e31bd1015d-aada29daee1d64ed0fe907043855cb7e" autocomplete="off" value="1"> <label class="btn btn-outline-primary" for="checks-d09223e97375de6a684575e31bd1015d-aada29daee1d64ed0fe907043855cb7e">Two</label> <input type="checkbox" class="btn-check" name="checks[]" id="checks-d09223e97375de6a684575e31bd1015d-ca8a2087e5557e317599344687a57391" autocomplete="off" value="2"> <label class="btn btn-outline-primary" for="checks-d09223e97375de6a684575e31bd1015d-ca8a2087e5557e317599344687a57391">Three</label>
</div>
</div>
</div>
<div class="row mb-3">
<label for="radios-954acfa9334c87a84671f681d263b526" class="col-4 col-form-label">Radio</label>
<div class="col-8">
<div class="btn-group" role="group">
<input type="radio" class="btn-check" name="radios" id="radios-954acfa9334c87a84671f681d263b526-06c2cea18679d64399783748fa367bdd" autocomplete="off" value="0"> <label class="btn btn-outline-primary" for="radios-954acfa9334c87a84671f681d263b526-06c2cea18679d64399783748fa367bdd">One</label> <input type="radio" class="btn-check" name="radios" id="radios-954acfa9334c87a84671f681d263b526-aada29daee1d64ed0fe907043855cb7e" autocomplete="off" value="1"> <label class="btn btn-outline-primary" for="radios-954acfa9334c87a84671f681d263b526-aada29daee1d64ed0fe907043855cb7e">Two</label> <input type="radio" class="btn-check" name="radios" id="radios-954acfa9334c87a84671f681d263b526-ca8a2087e5557e317599344687a57391" autocomplete="off" value="2"> <label class="btn btn-outline-primary" for="radios-954acfa9334c87a84671f681d263b526-ca8a2087e5557e317599344687a57391">Three</label>
</div>
</div>
</div>
<div class="row mb-3">
<label for="checklist-3e5c1e662db6a384564480551dad2d8a" class="col-4 col-form-label">Check List</label>
<div class="col-8">
<div class="form-control-plaintext">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="checklist[]" id="checklist-3e5c1e662db6a384564480551dad2d8a-06c2cea18679d64399783748fa367bdd" value="0"> <label class="form-check-label" for="checklist-3e5c1e662db6a384564480551dad2d8a-06c2cea18679d64399783748fa367bdd">One</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="checklist[]" id="checklist-3e5c1e662db6a384564480551dad2d8a-aada29daee1d64ed0fe907043855cb7e" value="1"> <label class="form-check-label" for="checklist-3e5c1e662db6a384564480551dad2d8a-aada29daee1d64ed0fe907043855cb7e">Two</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="checklist[]" id="checklist-3e5c1e662db6a384564480551dad2d8a-ca8a2087e5557e317599344687a57391" value="2"> <label class="form-check-label" for="checklist-3e5c1e662db6a384564480551dad2d8a-ca8a2087e5557e317599344687a57391">Three</label>
</div>
</div>
</div>
</div>
<div class="col-12 text-end">
<button id="button-2f234ea83b5d8dad1e4351d713f8d6dd" class="btn btn-primary" type="submit">Save</button>
</div>
</form>
To, indeed, leverage Bootstrap's client-side validation functionality, use the client_side_errors parameter to deploy the relevant attributes in the HTML markup (and do not forget to init the proper Javascript code).
<x-larastrap::form id="native-validated" client_side_errors="true">
<x-larastrap::text name="name" label="Name" required />
<x-larastrap::email name="email" label="EMail" required />
</x-larastrap::form>
<script>
(() => {
let form = document.getElementById('native-validated');
form.addEventListener('submit', event => {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false);
})();
</script>
<form id="native-validated" class="needs-validation" method="post" novalidate="" name="native-validated">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="row mb-3" novalidate="">
<label for="input-d683a8e50efc0137f29af4195a703ef8" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-d683a8e50efc0137f29af4195a703ef8" type="text" class="form-control" name="name" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-02eaea1e5c8ddf181a67757c013d7baa" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-02eaea1e5c8ddf181a67757c013d7baa" type="email" class="form-control" name="email" value="" required="" novalidate="">
</div>
</div>
<div class="col-12 text-end">
<button id="button-02976d92b0cd50c0558e6936d669bce6" class="btn btn-primary" novalidate="" type="submit">Save</button>
</div>
</form>
<script>
(() => {
let form = document.getElementById('native-validated');
form.addEventListener('submit', event => {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false);
})();
</script>
Note that Larastrap can also handle nested array inputs for more complex forms.
@php
// Server side validation for this form is defined as:
//
// $request->validate([
// 'person.name' => 'required',
// 'person.email' => 'required|email|max:255',
// ]);
@endphp
<x-larastrap::form action="/validated_dot">
<x-larastrap::text name="person.name" label="Name" />
<x-larastrap::email name="person.email" label="EMail" />
</x-larastrap::form>
<form id="form-4589d897b6805d1710beb022f321e010" class="" method="post" action="/validated_dot" name="form-4589d897b6805d1710beb022f321e010">
<input type="hidden" name="_token" value="TZE0c9qO7cWckP2dxFQVQ4p089TAtnpI21voAwGc" autocomplete="off">
<div class="row mb-3">
<label for="input-aefde5e6e58e2ea63465861c9916a036" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-aefde5e6e58e2ea63465861c9916a036" type="text" class="form-control" name="person[name]" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-855f1d7aaded987444b515d6808e4061" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-855f1d7aaded987444b515d6808e4061" type="email" class="form-control" name="person[email]" value="">
</div>
</div>
<div class="col-12 text-end">
<button id="button-0060b4d08ecaaffab62f991cccb9a520" class="btn btn-primary" type="submit">Save</button>
</div>
</form>