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="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" 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">
<div class="form-text">
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="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" 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="Gerard McGlynn">
</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="fLdO0fA19S">
</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="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" 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="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" 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 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-28dd2c7955ce926456240b2ff0100bde" class="" method="post" action="https://larastrap.madbob.org/user" name="form-28dd2c7955ce926456240b2ff0100bde">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="alert alert-info">
This form creates a new model.
</div>
<div class="row mb-3">
<label for="input-106603305c686e0e10a29ca7a761ff46" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-106603305c686e0e10a29ca7a761ff46" type="text" class="form-control" name="name" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-b8b44930f0b834e1d423ded3404b5bfb" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-b8b44930f0b834e1d423ded3404b5bfb" type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="col-12 text-end">
<button id="button-f2f204e13ea4f196bce1eceece7aefd9" class="btn btn-primary" type="submit">Save</button>
</div>
</form><br>
<form id="form-3ef815416f775098fe977004015c6193" class="" method="post" action="https://larastrap.madbob.org/user/123" name="form-3ef815416f775098fe977004015c6193">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" 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-8627522b343d092ef4c24bd9a27e2136" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-8627522b343d092ef4c24bd9a27e2136" type="text" class="form-control" name="name" value="Foo Bar">
</div>
</div>
<div class="row mb-3">
<label for="input-b6257c2417bbb73aafe7ce72895b15a0" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-b6257c2417bbb73aafe7ce72895b15a0" type="email" class="form-control" name="email" value="foobar@example.com">
</div>
</div>
<div class="col-12 text-end">
<button id="button-4a90fb9aaca9060e016e43148efe95a9" 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-26657d5ff9020d2abefe558796b99584" class="" method="post" name="form-26657d5ff9020d2abefe558796b99584">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="row mb-3">
<label for="input-4150a0886b0c5555bd33ae83509b4b11" class="col-4 col-form-label">First Name</label>
<div class="col-8">
<input id="input-4150a0886b0c5555bd33ae83509b4b11" type="text" class="form-control" name="firstname" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-010f2003c6dabe9dd3c94f09f188468c" class="col-4 col-form-label">Last Name</label>
<div class="col-8">
<input id="input-010f2003c6dabe9dd3c94f09f188468c" type="text" class="form-control" name="lastname" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-6fca9f2f847e596ddceffee15eab81ab" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-6fca9f2f847e596ddceffee15eab81ab" type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="col-12 text-end">
<button id="button-18625b8c45fc3853134d10567147c9db" class="btn btn-success" type="submit">Submit</button> <button id="button-0ceb2fca984269b2942ce90e29f26f18" 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-698d51a19d8a121ce581499d7b701668" class="" method="post" name="form-698d51a19d8a121ce581499d7b701668">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="row mb-3">
<label for="input-b99ff79a1584dc4c5d1f31a52d6d5d28" class="col-4 col-form-label">First Name</label>
<div class="col-8">
<input id="input-b99ff79a1584dc4c5d1f31a52d6d5d28" type="text" class="form-control" name="firstname" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-a4dc887d63c24ba4c0151dfe490383b5" class="col-4 col-form-label">Last Name</label>
<div class="col-8">
<input id="input-a4dc887d63c24ba4c0151dfe490383b5" type="text" class="form-control" name="lastname" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-953f178b0d16986f689df60717ba292f" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-953f178b0d16986f689df60717ba292f" type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="col-12 text-end">
<a id="link-0df6af37975a973a473c08bced599f85" class="btn btn-primary" href="https://larastrap.madbob.org/">This is a Link</a> <button id="button-e85c20734590ce3c54bfeaa2a4182bad" 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-c8ffe9a587b126f152ed3d89a146b445" class="" method="post" name="form-c8ffe9a587b126f152ed3d89a146b445">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="row mb-3">
<label for="input-44a71045dd2d048a6f0e56b13c0d4fe4" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-44a71045dd2d048a6f0e56b13c0d4fe4" type="text" class="form-control" name="name" value="Gerard McGlynn">
</div>
</div>
<div class="row mb-3">
<label for="input-310c50edfd6ca47efef4d7641873e7a9" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-310c50edfd6ca47efef4d7641873e7a9" type="email" class="form-control" name="email" value="xwelch@example.org">
</div>
</div>
<hr>
<div class="col-12 text-end">
<small id="basenode-7ba2698a81bf911cc7d247ebea8a4140" class="float-start">Last update: 2024-08-04 08:08:24</small> <button id="button-778e828dba47d25ced59a26e88a107a3" 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-e00da03b685a0dd18fb6a08af0923de0" class="" method="post" name="form-e00da03b685a0dd18fb6a08af0923de0">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="mb-3">
<label for="input-ed438ebd7fc5227581992e642e9aab4b" class="form-label">First Name</label> <input id="input-ed438ebd7fc5227581992e642e9aab4b" type="text" class="form-control" name="firstname" value="">
</div>
<div class="mb-3">
<label for="input-c7856552a87a29b2652294e196c9a8c0" class="form-label">Last Name</label> <input id="input-c7856552a87a29b2652294e196c9a8c0" type="text" class="form-control" name="lastname" value="">
</div>
<div class="mb-3">
<label for="input-94483eaae80bcfd139e095cdc6673e5c" class="form-label">EMail</label> <input id="input-94483eaae80bcfd139e095cdc6673e5c" type="email" class="form-control" name="email" value="">
</div>
<div class="col-12 text-start">
<button id="button-0a2845321b91c59d4c9caa745d4eacfc" 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-1d7f7abc18fcb43975065399b0d1e48e" class="row row-cols-lg-auto g-3 align-items-center" method="post" name="form-1d7f7abc18fcb43975065399b0d1e48e">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="col-auto">
<input id="input-5dc26ae9afcf1f158b95427da76c1c66" type="text" class="form-control" name="firstname" value="" placeholder="First Name">
</div>
<div class="col-auto">
<input id="input-c1c4c4f7b83c1581f9006b375550b958" type="text" class="form-control" name="lastname" value="" placeholder="Last Name">
</div>
<div class="col-12 text-end">
<button id="button-b3405de5ac5ee23c519441f44cc040b8" 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-82aa4b0af34c2313a562076992e50aa3" class="" method="post" name="form-82aa4b0af34c2313a562076992e50aa3">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<table class="table">
<tbody>
<tr>
<td>
<input id="input-986506565f2d05a77569931adf6185b8" type="text" class="form-control" name="name" value="Jules Wunsch">
</td>
<td>
<input id="input-4eeac831e52b793831934d28aac3573e" type="text" class="form-control" name="email" value="daniel.arlie@example.net">
</td>
</tr>
<tr>
<td>
<input id="input-fb4d9d10696e87e9056e574c7b746bb8" type="text" class="form-control" name="name" value="Ebba Olson I">
</td>
<td>
<input id="input-be790fd17b31617f44270fe18dff3673" type="text" class="form-control" name="email" value="ebert.jeremy@example.net">
</td>
</tr>
<tr>
<td>
<input id="input-93e8e0dea855488b669b0307bef86c50" type="text" class="form-control" name="name" value="Jessyca Goyette">
</td>
<td>
<input id="input-0ddceccfe0b000424f2ae7f4461dddb5" 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-47af2a02a8f9f72b1110deaea06684b5" 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-045117b0e0a11a242b9765e79cbf113f" class="" method="post" name="form-045117b0e0a11a242b9765e79cbf113f">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="mb-3">
<label for="input-6403458d9b41420113db1addf46ec9e3" class="form-label">First Name</label> <input id="input-6403458d9b41420113db1addf46ec9e3" type="text" class="form-control" name="firstname" value="">
</div>
<div class="mb-3">
<label for="input-8f2f963af24bbeab5bc6a95bbaad6434" class="form-label">Last Name</label> <input id="input-8f2f963af24bbeab5bc6a95bbaad6434" type="text" class="form-control" name="lastname" value="">
</div>
<div class="mb-3">
<label for="input-9af15ae23739f719137b8ad575644a43" class="form-label">EMail</label> <input id="input-9af15ae23739f719137b8ad575644a43" type="email" class="form-control" name="email" value="">
</div>
<div class="mb-3">
<div class="form-check mt-1">
<input id="check-f55adcf1b2c0bf77e58074cd278022ea" type="checkbox" class="form-check-input" name="check"> <label for="check-f55adcf1b2c0bf77e58074cd278022ea" class="">Check</label>
</div>
</div>
<div class="mb-3">
<label for="checks-90f320ef1af65d76f008e32d7b3faec8" class="form-label">Checks</label>
<div class="d-block btn-group" role="group">
<input type="checkbox" class="btn-check" name="checks[]" id="checks-90f320ef1af65d76f008e32d7b3faec8-06c2cea18679d64399783748fa367bdd" autocomplete="off" value="0"> <label class="btn btn-outline-primary" for="checks-90f320ef1af65d76f008e32d7b3faec8-06c2cea18679d64399783748fa367bdd">One</label> <input type="checkbox" class="btn-check" name="checks[]" id="checks-90f320ef1af65d76f008e32d7b3faec8-aada29daee1d64ed0fe907043855cb7e" autocomplete="off" value="1"> <label class="btn btn-outline-primary" for="checks-90f320ef1af65d76f008e32d7b3faec8-aada29daee1d64ed0fe907043855cb7e">Two</label> <input type="checkbox" class="btn-check" name="checks[]" id="checks-90f320ef1af65d76f008e32d7b3faec8-ca8a2087e5557e317599344687a57391" autocomplete="off" value="2"> <label class="btn btn-outline-primary" for="checks-90f320ef1af65d76f008e32d7b3faec8-ca8a2087e5557e317599344687a57391">Three</label>
</div>
</div>
<div class="col-12 text-end">
<button id="button-6fc20b790e9214469673fd4017243a00" 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-a4f0611a2ae0f3c664e9bba9b19a05df" class="" method="post" action="/validated" name="form-a4f0611a2ae0f3c664e9bba9b19a05df">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="row mb-3">
<label for="input-5e96d06d5b17fd3ed6998b48443937b1" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-5e96d06d5b17fd3ed6998b48443937b1" type="text" class="form-control" name="name" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-0ec449c0b5b7c1fb6e6e0874d3b6443d" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-0ec449c0b5b7c1fb6e6e0874d3b6443d" type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="row mb-3">
<label for="textarea-814bbd496881efb1a14e230183f9947d" class="col-4 col-form-label">Description</label>
<div class="col-8">
<textarea id="textarea-814bbd496881efb1a14e230183f9947d" class="form-control" name="description"></textarea>
</div>
</div>
<div class="row mb-3">
<label for="select-d6bc3d2dd128c24ebcaf131f805b8e27" class="col-4 col-form-label">Select</label>
<div class="col-8">
<select id="select-d6bc3d2dd128c24ebcaf131f805b8e27" 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-9d38f81b5c830473e2b2c9f777749d6b" class="col-4 col-form-label">Check</label>
<div class="col-8">
<div class="form-check mt-1">
<input id="check-9d38f81b5c830473e2b2c9f777749d6b" type="checkbox" class="form-check-input" name="check">
</div>
</div>
</div>
<div class="row mb-3">
<label for="checks-9f9d69717bc32e691d32469c77fedd81" 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-9f9d69717bc32e691d32469c77fedd81-06c2cea18679d64399783748fa367bdd" autocomplete="off" value="0"> <label class="btn btn-outline-primary" for="checks-9f9d69717bc32e691d32469c77fedd81-06c2cea18679d64399783748fa367bdd">One</label> <input type="checkbox" class="btn-check" name="checks[]" id="checks-9f9d69717bc32e691d32469c77fedd81-aada29daee1d64ed0fe907043855cb7e" autocomplete="off" value="1"> <label class="btn btn-outline-primary" for="checks-9f9d69717bc32e691d32469c77fedd81-aada29daee1d64ed0fe907043855cb7e">Two</label> <input type="checkbox" class="btn-check" name="checks[]" id="checks-9f9d69717bc32e691d32469c77fedd81-ca8a2087e5557e317599344687a57391" autocomplete="off" value="2"> <label class="btn btn-outline-primary" for="checks-9f9d69717bc32e691d32469c77fedd81-ca8a2087e5557e317599344687a57391">Three</label>
</div>
</div>
</div>
<div class="row mb-3">
<label for="radios-25cb85ad2223e090997cc14b6b38ba24" 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-25cb85ad2223e090997cc14b6b38ba24-06c2cea18679d64399783748fa367bdd" autocomplete="off" value="0"> <label class="btn btn-outline-primary" for="radios-25cb85ad2223e090997cc14b6b38ba24-06c2cea18679d64399783748fa367bdd">One</label> <input type="radio" class="btn-check" name="radios" id="radios-25cb85ad2223e090997cc14b6b38ba24-aada29daee1d64ed0fe907043855cb7e" autocomplete="off" value="1"> <label class="btn btn-outline-primary" for="radios-25cb85ad2223e090997cc14b6b38ba24-aada29daee1d64ed0fe907043855cb7e">Two</label> <input type="radio" class="btn-check" name="radios" id="radios-25cb85ad2223e090997cc14b6b38ba24-ca8a2087e5557e317599344687a57391" autocomplete="off" value="2"> <label class="btn btn-outline-primary" for="radios-25cb85ad2223e090997cc14b6b38ba24-ca8a2087e5557e317599344687a57391">Three</label>
</div>
</div>
</div>
<div class="row mb-3">
<label for="checklist-539d2454249267027752fe35747d56e0" 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-539d2454249267027752fe35747d56e0-06c2cea18679d64399783748fa367bdd" value="0"> <label class="form-check-label" for="checklist-539d2454249267027752fe35747d56e0-06c2cea18679d64399783748fa367bdd">One</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="checklist[]" id="checklist-539d2454249267027752fe35747d56e0-aada29daee1d64ed0fe907043855cb7e" value="1"> <label class="form-check-label" for="checklist-539d2454249267027752fe35747d56e0-aada29daee1d64ed0fe907043855cb7e">Two</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="checklist[]" id="checklist-539d2454249267027752fe35747d56e0-ca8a2087e5557e317599344687a57391" value="2"> <label class="form-check-label" for="checklist-539d2454249267027752fe35747d56e0-ca8a2087e5557e317599344687a57391">Three</label>
</div>
</div>
</div>
</div>
<div class="col-12 text-end">
<button id="button-aed881704c303a2f81b4417fc06a56e3" 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="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="row mb-3" novalidate="">
<label for="input-6e5dcc1450c542089a0312657145b53a" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-6e5dcc1450c542089a0312657145b53a" type="text" class="form-control" name="name" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-7406c1f6eb3e3a217d033a9a63e0ecc8" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-7406c1f6eb3e3a217d033a9a63e0ecc8" type="email" class="form-control" name="email" value="" required="" novalidate="">
</div>
</div>
<div class="col-12 text-end">
<button id="button-38a2f7282dedf2ad367facbce8302fc4" 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>
Please note 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-1f58236cef2d63ee709d1e3f55934905" class="" method="post" action="/validated_dot" name="form-1f58236cef2d63ee709d1e3f55934905">
<input type="hidden" name="_token" value="CexmHoeKQpAb8drdK0pLsOxgmWNMd4KCqqhi2zm8" autocomplete="off">
<div class="row mb-3">
<label for="input-06bf0819d4735f9856243a4a30b5a113" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-06bf0819d4735f9856243a4a30b5a113" type="text" class="form-control" name="person[name]" value="">
</div>
</div>
<div class="row mb-3">
<label for="input-663e19739fa176b4f7fb471db39ef0f2" class="col-4 col-form-label">EMail</label>
<div class="col-8">
<input id="input-663e19739fa176b4f7fb471db39ef0f2" type="email" class="form-control" name="person[email]" value="">
</div>
</div>
<div class="col-12 text-end">
<button id="button-bafb394d52809a634a8ffb20448b403f" class="btn btn-primary" type="submit">Save</button>
</div>
</form>