Auto Read is a feature of Larastrap Forms that makes it possible to validate and store informations with a single line of code in your Laravel Controllers.
The essential idea is:
To enable Auto Read for any Larastrap Form, just add the boolean autoread parameter.
This will append to the form itself a type="hidden"
input with the full representation of each field and each attribute relevant for validation and interpretation of the submitted values.
On submit, all fields in the Form will be processed to be assigned to the reference Model (or to a new instance, if none was assigned) accordingly to their name. If you want to exclude one or more fields, not intended to be reassigned to the Model (such as: an hidden input used for internal purposes), you can add them the skip_autoread parameter.
All server-side functions for Auto Read are implemented by the LarastrapStack
singleton, registered by Larastrap's service provider.
The autoreadValidate()
method reads the incoming Request and validates it against the proper model previously generated when rendering the form.
@php
/*
The "autoread_validate" route is implemented by the following method:
public function autoreadValidation(Request $request)
{
app()->make('LarastrapStack')->autoreadValidate($request, true);
return back()->withInput();
}
*/
@endphp
<x-larastrap::form route="autoread_validate" error_bag="validate" novalidate autoread>
<x-larastrap::text name="text" label="A Text" required />
<x-larastrap::textarea name="textarea" label="A Textarea" required />
<x-larastrap::number name="number" label="A Number" required min="5" max="50" help="Must be between 5 and 50" />
<x-larastrap::email name="email" label="An Email" required />
<x-larastrap::password name="password" label="A Password" required minlength="8" help="At least 8 characters" />
<x-larastrap::url name="url" label="An URL" required />
<x-larastrap::tel name="phone" label="A Phone Number" required />
<x-larastrap::date name="date" label="A Date" required />
<x-larastrap::time name="time" label="A Time" required />
<x-larastrap::datetime name="datetime" label="Both Date and Time" required />
<x-larastrap::month name="month" label="A Month" required />
<x-larastrap::week name="week" label="A Week" required />
<x-larastrap::check name="boolean" label="A Boolean" required />
<x-larastrap::checks name="choose" label="At least one" :options="['red' => 'Red', 'green' => 'Green', 'blue' => 'Blue']" required />
</x-larastrap::form>
<form id="form-d3d9446802a44259755d38e6d163e820" class="" method="post" action="https://larastrap.madbob.org/autoread_validate" novalidate="" name="form-d3d9446802a44259755d38e6d163e820">
<input type="hidden" name="_token" value="y0syY0Rjc1xJ3D6a5yxGWt7zWFUtHrLTLZijPknL" autocomplete="off">
<div class="row mb-3" novalidate="">
<label for="input-c68571f85f230504963c4934c3465b84" class="col-4 col-form-label">A Text</label>
<div class="col-8">
<input id="input-c68571f85f230504963c4934c3465b84" type="text" class="form-control" name="text" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="textarea-4f8201d400f2f3b5f5f678c4c454be86" class="col-4 col-form-label">A Textarea</label>
<div class="col-8">
<textarea id="textarea-4f8201d400f2f3b5f5f678c4c454be86" class="form-control" name="textarea" required="" novalidate=""></textarea>
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-dfaab28994a93e07ea8c79f3b82b02e7" class="col-4 col-form-label">A Number</label>
<div class="col-8">
<input id="input-dfaab28994a93e07ea8c79f3b82b02e7" type="number" class="form-control" name="number" value="" min="5" max="50" required="" novalidate="" aria-describedby="help-input-dfaab28994a93e07ea8c79f3b82b02e7">
<div class="form-text" id="help-input-dfaab28994a93e07ea8c79f3b82b02e7">
Must be between 5 and 50
</div>
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-ddc0effe1ad6166dd5901cb3f5404c13" class="col-4 col-form-label">An Email</label>
<div class="col-8">
<input id="input-ddc0effe1ad6166dd5901cb3f5404c13" type="email" class="form-control" name="email" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-75814610aa93f338df0871d014a534a3" class="col-4 col-form-label">A Password</label>
<div class="col-8">
<input id="input-75814610aa93f338df0871d014a534a3" type="password" class="form-control" name="password" value="" minlength="8" required="" novalidate="" aria-describedby="help-input-75814610aa93f338df0871d014a534a3">
<div class="form-text" id="help-input-75814610aa93f338df0871d014a534a3">
At least 8 characters
</div>
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-c011d5dcc7cd3ca2b942817c375dd78c" class="col-4 col-form-label">An URL</label>
<div class="col-8">
<input id="input-c011d5dcc7cd3ca2b942817c375dd78c" type="url" class="form-control" name="url" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-ec084ac2fd653630861606b4157fe1d2" class="col-4 col-form-label">A Phone Number</label>
<div class="col-8">
<input id="input-ec084ac2fd653630861606b4157fe1d2" type="tel" class="form-control" name="phone" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-525068bfbbdd584df92d84f43cd32de3" class="col-4 col-form-label">A Date</label>
<div class="col-8">
<input id="input-525068bfbbdd584df92d84f43cd32de3" type="date" class="form-control" name="date" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-4a7e65bd0f4ec419f01368172516d95a" class="col-4 col-form-label">A Time</label>
<div class="col-8">
<input id="input-4a7e65bd0f4ec419f01368172516d95a" type="time" class="form-control" name="time" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-394d8af278f87581bf7a66813fefeabc" class="col-4 col-form-label">Both Date and Time</label>
<div class="col-8">
<input id="input-394d8af278f87581bf7a66813fefeabc" type="datetime-local" class="form-control" name="datetime" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-89abca6e4a4d3e210189cf8917abd889" class="col-4 col-form-label">A Month</label>
<div class="col-8">
<input id="input-89abca6e4a4d3e210189cf8917abd889" type="month" class="form-control" name="month" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-1e9966c0fbdd2f68b4174ae6e8383dfd" class="col-4 col-form-label">A Week</label>
<div class="col-8">
<input id="input-1e9966c0fbdd2f68b4174ae6e8383dfd" type="week" class="form-control" name="week" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="check-e3d799e23ffea127f448b9cfa1adfb10" class="col-4 col-form-label">A Boolean</label>
<div class="col-8">
<div class="form-check mt-1">
<input id="check-e3d799e23ffea127f448b9cfa1adfb10" type="checkbox" class="form-check-input" name="boolean" required="" novalidate="">
</div>
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="checks-6f5075196d2576e0026921c580200adb" class="col-4 col-form-label">At least one</label>
<div class="col-8">
<div id="checks-6f5075196d2576e0026921c580200adb" class="btn-group" role="group" novalidate="">
<input type="checkbox" class="btn-check" id="checks-6f5075196d2576e0026921c580200adb-ee38e4d5dd68c4e440825018d549cb47" name="choose[]" value="red" autocomplete="off"> <label class="btn btn-outline-primary" for="checks-6f5075196d2576e0026921c580200adb-ee38e4d5dd68c4e440825018d549cb47">Red</label> <input type="checkbox" class="btn-check" id="checks-6f5075196d2576e0026921c580200adb-d382816a3cbeed082c9e216e7392eed1" name="choose[]" value="green" autocomplete="off"> <label class="btn btn-outline-primary" for="checks-6f5075196d2576e0026921c580200adb-d382816a3cbeed082c9e216e7392eed1">Green</label> <input type="checkbox" class="btn-check" id="checks-6f5075196d2576e0026921c580200adb-9594eec95be70e7b1710f730fdda33d9" name="choose[]" value="blue" autocomplete="off"> <label class="btn btn-outline-primary" for="checks-6f5075196d2576e0026921c580200adb-9594eec95be70e7b1710f730fdda33d9">Blue</label>
</div>
</div>
</div><input id="hidden-2bae1bb76d33aa2976f0c01a40aedea0" type="hidden" class="" name="larastrap_autoread" value=
"eyJpdiI6Im90WDNmYnlyY3JtVVBCWmFtTTZPeXc9PSIsInZhbHVlIjoiSG9iWjRGZnEreGdtbEFUTTBqdjhQbkV4eU8xRzZvc1hnbk03T2xaRWpTWU9jTm9naWF4c3hHN2RLT1ZibENBbVFkbWtzdXI0NDg5OHVqVFBWYm5jeVdtNUNtazNobUk5SFZwK0t5aE9HTG0wWktkY0dyQzYrR2xCckFWbG9mcHY2RXJVT2p2bXRUYm82U3M5anlOd2VqbVRVUEtvYjhMa3lGM3Z6Ui9Hb1d1MnlSRFBSU045YlZQVWlSM3Y1S2JZY3N3R1F2UDh4aVNiVktuR2loK3lHK0xTa2gwSXhnTHNnTlQ5TFg5dnpneXlRTGJ1UDJTSElIZDlrOGxrUlA4YzU1RFBnTmQ3emkxMDNxSGx1Q1RWK2JlcHBtS1BDRDFlYVdFK3hMZmFSbHRXOHZLU3JWS243Q3RPMjluTUlPY3VNO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XQys3U2JqVlBnSmxWa1FKUW9XV0dJanFYYlNkUHFsdlo5Q1hhTGtCbk1vSEdXcXREUTc3R2VKMXk3NmFQcytDNkw2ZU51MXJ3YTJGQ1l5VDFLRDEwWjUvUmFORnZGRitwM213K0lrWXdFNmxkdWsrdG42WGFHT1IrOXVseDRZQ0IxMW54VEJUQ0lsN3FEMXNBd0dremhVWUtSeWUreVFlbE1ZQ3QyRDg0Z1RBRXlSN2lJb2RaOVp4MU8waTROVDlFL0owUGh2T2ZjNTR6UkFrZ1FlM091cktsdWNhUFNJK24rNUN0YkVsbzh2QTRPcHRYd3cyRmFhVlJkRHArTGtCWHVuMTRTQVhFNW5pOXczU1hzZjdJZis0UmtoZEdabFd5aG9CRDVhRlErM3V4SjgvbGJiektIckY5dFBBaVRqZzFtMnVtOFVWT0ZWSzBZaHQvcWNrS0hORFplUzY2WU52b01nSUZPUHhadDZjcTdRbks0em1tSEFUejNHNHpSanMzRGxlZUNQSkhqbzhtdDdGeHBKcStEdHJzZm5vV25MR2h5a2JCZEFjdmVPUGtPTC9XQk9hUjF3ME1wRG1VTVUzUWxNUDE0Rm9ueGJhdmRvL0x2V3Mwa05oMlYrL3MyV2Ewc1lkc2l0RVdUd1lCL0VNWFdEOElZckYybHNyem12TmM4OGhvQitKTlJLMllBMW1TSlJkVDJVam5YdmovZnU3c201bDdQbllienlOOEYvTFlXSFJEK0lBS0R3NytvenNkWlFyR2pibDNtM2cvdz09IiwibWFjIjoiZWIxOGYxMTY2NWQ3MDlmNTExODE0ZjFkOTRkMDhmN2JhY2Y4YjYyOTI5NmY3OGYzODZmM2E5ZTY0M2ZiOGIyYyIsInRhZyI6IiJ9"
novalidate="">
<div class="col-12 text-end">
<button id="button-58e25d7cc3169aa40314fa2a2facdad5" class="btn btn-primary" novalidate="" type="submit">Save</button>
</div>
</form>
When the second parameter of autoreadValidate()
is set to true
, classic validation behavior of Laravel is applied: in case of error, the proper error bag is filled with informations about errors and the user is redirected to the previous page.
Otherwise, when set to false
, the function just return true
or false
accordingly to the validation result, and permits you to proceed with your own business login.
public function autoreadValidation(Request $request)
{
$test = app()->make('LarastrapStack')->autoreadValidate($request, false);
if ($test === false) {
Log::debug('Invalid input!');
return redirect()->route('home');
}
/* The rest of your Controller function */
}
You can also use the autoreadValidationRules()
function to retrieve the generated array of validation rules, to be extended with your own custom rules or just to be debugged.
The autoreadSave()
function in LarastrapStack
can be used to automatically transfer the data payload of the Request into the Eloquent Model assigned to the form.
"Normal" inputs are just transferred by plain value, and "model" variants (as Selects and Checklists) translate in Eloquent relations.
Intentionally, autoreadSave()
does not actually save()
the model instance to permit you further elaboration.
public function store(Request $request)
{
app()->make('LarastrapStack')->autoreadValidate($request, true);
/*
It retrieves the Model assigned to the Form, or creates a new instance.
Eventually you can use the same Controller method both to store or
update Models of the same type
*/
$user = app()->make('LarastrapStack')->autoreadSave($request, User::class);
/*
Do whatever you want before actual saving
*/
$user->save();
return redirect()->route('users.index');
}
The Controller's method used in the following example is a bit more complex due his usage in this part of the documentation, but yet a few lines of code permit to manage different use cases.
@php
/*
The "autoread_save" route is implemented by the following method:
public function autoreadSaving(Request $request)
{
app()->make('LarastrapStack')->autoreadValidate($request, true);
DB::beginTransaction();
$user = app()->make('LarastrapStack')->autoreadSave($request, User::class);
$user->save();
$user->load('boss', 'friends');
DB::rollback();
$type = $request->input('type');
$feedback = match($type) {
'save' => 'saved_attributes',
'create' => 'created_attributes',
'file' => 'file_attributes',
};
return back()->with($feedback, [
'name' => $user->name,
'email' => $user->email,
'password' => $user->password,
'boss' => $user->boss?->name,
'friends' => $user->friends->map(fn($f) => $f->name)->join(','),
])->withInput();
}
It is not actually saved on the database as this is just for demo purpose...
*/
$user = App\Models\User::inRandomOrder()->has('boss')->first();
$bosses = App\Models\User::doesntHave('boss')->orderBy('name', 'asc')->get();
$users = App\Models\User::orderBy('name', 'asc')->get();
@endphp
@if(Session::has('saved_attributes'))
<div class="alert alert-info">
<p>
Saved attributes:
</p>
<ul>
@foreach(Session::get('saved_attributes') as $name => $value)
<li>{{ $name }} = {{ $value }}</li>
@endforeach
</ul>
</div>
@else
<x-larastrap::form route="autoread_save" error_bag="save" :obj="$user" novalidate autoread>
<x-larastrap::hidden name="type" value="save" skip_autoread />
<x-larastrap::text nprefix="save_test_" name="name" label="Name" required />
<x-larastrap::email nprefix="save_test_" name="email" label="E-Mail" required />
<x-larastrap::radios-model nprefix="save_test_" name="boss" label="Select a Boss" :options="$bosses" required />
<x-larastrap::checklist-model nprefix="save_test_" name="friends" label="Select friends" :options="$users" />
</x-larastrap::form>
@endif
<form id="form-735b90b4568125ed6c3f678819b6e058" class="" method="post" action="https://larastrap.madbob.org/autoread_save" novalidate="" name="form-735b90b4568125ed6c3f678819b6e058">
<input type="hidden" name="_token" value="y0syY0Rjc1xJ3D6a5yxGWt7zWFUtHrLTLZijPknL" autocomplete="off"> <input id="hidden-8b5bc07e7404e6df6a5f5ad85d0061d4" type="hidden" class="" name="type" value="save" novalidate="">
<div class="row mb-3" novalidate="">
<label for="input-0f6acad989f210b9b207b3633cd9dd63" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-0f6acad989f210b9b207b3633cd9dd63" type="text" class="form-control" name="save_test_name" value="Ebba Olson I" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-bbb19d0319ab78a9d86a1c596d9c5af4" class="col-4 col-form-label">E-Mail</label>
<div class="col-8">
<input id="input-bbb19d0319ab78a9d86a1c596d9c5af4" type="email" class="form-control" name="save_test_email" value="ebert.jeremy@example.net" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="radios-59e414c1e8f352688eff856ceabea66e" class="col-4 col-form-label">Select a Boss</label>
<div class="col-8">
<div id="radios-59e414c1e8f352688eff856ceabea66e" class="btn-group" role="group" novalidate="">
<input type="radio" class="btn-check" id="radios-59e414c1e8f352688eff856ceabea66e-d3d3570e50c3a7b56579c72c44c3bd8c" name="save_test_boss" value="7" autocomplete="off" checked> <label class="btn btn-outline-primary" for="radios-59e414c1e8f352688eff856ceabea66e-d3d3570e50c3a7b56579c72c44c3bd8c">Eliezer Luettgen</label> <input type="radio" class="btn-check" id="radios-59e414c1e8f352688eff856ceabea66e-1669d9a0bc26b289ae113d7621d70c9b" name="save_test_boss" value="9" autocomplete="off"> <label class="btn btn-outline-primary" for="radios-59e414c1e8f352688eff856ceabea66e-1669d9a0bc26b289ae113d7621d70c9b">Erica Krajcik</label> <input type="radio" class="btn-check" id="radios-59e414c1e8f352688eff856ceabea66e-4f0c0cbead6ae07c8be71951fc29d12e" name="save_test_boss" value="8" autocomplete="off"> <label class="btn btn-outline-primary" for="radios-59e414c1e8f352688eff856ceabea66e-4f0c0cbead6ae07c8be71951fc29d12e">Gerard McGlynn</label> <input type="radio" class="btn-check" id="radios-59e414c1e8f352688eff856ceabea66e-631e06390b23157fab1c3cce98e4b9d9" name="save_test_boss" value="6" autocomplete="off"> <label class="btn btn-outline-primary" for="radios-59e414c1e8f352688eff856ceabea66e-631e06390b23157fab1c3cce98e4b9d9">Prof. Elmo Rosenbaum</label> <input type="radio" class="btn-check" id="radios-59e414c1e8f352688eff856ceabea66e-d5ffd3be773ccd80ad616da04e9f8016" name="save_test_boss" value="10" autocomplete="off"> <label class="btn btn-outline-primary" for="radios-59e414c1e8f352688eff856ceabea66e-d5ffd3be773ccd80ad616da04e9f8016">Shany Bosco</label>
</div>
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="checklist-7c399e72a2cd9f8b128fba8a15817f93" class="col-4 col-form-label">Select friends</label>
<div class="col-8">
<div class="form-control-plaintext">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-1bb08e28e93bfd7b7ad823611325d50d" name="save_test_friends[]" value="2" autocomplete="off"> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-1bb08e28e93bfd7b7ad823611325d50d">Ebba Olson I</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-9d31a793d380387c12b3dc978b819a7b" name="save_test_friends[]" value="5" autocomplete="off"> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-9d31a793d380387c12b3dc978b819a7b">Edwina Hettinger</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-d3d3570e50c3a7b56579c72c44c3bd8c" name="save_test_friends[]" value="7" autocomplete="off"> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-d3d3570e50c3a7b56579c72c44c3bd8c">Eliezer Luettgen</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-1669d9a0bc26b289ae113d7621d70c9b" name="save_test_friends[]" value="9" autocomplete="off"> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-1669d9a0bc26b289ae113d7621d70c9b">Erica Krajcik</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-0a2a1c575a4e367b62bf138c10549edd" name="save_test_friends[]" value="4" autocomplete="off" checked> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-0a2a1c575a4e367b62bf138c10549edd">Geraldine Abshire IV</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-4f0c0cbead6ae07c8be71951fc29d12e" name="save_test_friends[]" value="8" autocomplete="off"> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-4f0c0cbead6ae07c8be71951fc29d12e">Gerard McGlynn</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-2492875cb979dac7a20a4b6834f1f28e" name="save_test_friends[]" value="3" autocomplete="off" checked> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-2492875cb979dac7a20a4b6834f1f28e">Jessyca Goyette</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-66096f75ef0bcff89ac90afc09cc0810" name="save_test_friends[]" value="1" autocomplete="off" checked> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-66096f75ef0bcff89ac90afc09cc0810">Jules Wunsch</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-631e06390b23157fab1c3cce98e4b9d9" name="save_test_friends[]" value="6" autocomplete="off"> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-631e06390b23157fab1c3cce98e4b9d9">Prof. Elmo Rosenbaum</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-7c399e72a2cd9f8b128fba8a15817f93-d5ffd3be773ccd80ad616da04e9f8016" name="save_test_friends[]" value="10" autocomplete="off" checked> <label class="form-check-label" for="checklist-7c399e72a2cd9f8b128fba8a15817f93-d5ffd3be773ccd80ad616da04e9f8016">Shany Bosco</label>
</div>
</div>
</div>
</div><input id="hidden-29c2a1f08434f93b0b5824282fe33b00" type="hidden" class="" name="larastrap_autoread" value="eyJpdiI6Iko5MlNkRG9vTE1xMzl5bEtTNFZ0V0E9PSIsInZhbHVlIjoid1JMVmFwOHFialNPNS84bHFYWjRIMnBZUTY0eGVhMWNEK0dodmsxNTIvVDRzTVZjbFkzYnlQRWlMQXdXWnpYMk1HdjJSaVhHQ3FvbFpLcWI5VEdJNHI4b3B2bERodkpWVlVjejNKcm0wSzQySFFvdXdRa1FaOFF5RTJSY1REd0FtTS9NbVN1ZXRveVVxSlVnRmpIZ05lcEJjelFNd0s0RHVsRDQ5R0lCeFkrYzB0Z0JSLzBSR3dGT25lOVdqUVdKeUdGUUM0TDIzSWhmcmxKai9US0NOWm5jSnUyMjJwYzgwajhJL3VFMitEY1dGTlFteVQ5ZzNnV3E1RVdoWk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JBSElMc0k5dHhOMExsRHc9PSIsIm1hYyI6ImVjMGVhMjNjMDI2NjRmNjdiZmU4Mjk1N2QwMjgzMjQ1ZDg5MjllYjQ4Yzc1ZmVkNGQ0ZjdkZTcwMGRmYThkNzIiLCJ0YWciOiIifQ==" novalidate="">
<div class="col-12 text-end">
<button id="button-bf077f3126753fbdafa75cff46edfbdc" class="btn btn-primary" novalidate="" type="submit">Save</button>
</div>
</form>
The optional second argument of autoreadSave()
is a Model's class: it is used when the initial form is not built on an existing instance, but refers to a "create" action.
@php
/*
Note that here we use the same "autoread_save" route already involved in the
previous example, even if the form has not explicit Model assigned and input
names as different.
*/
$bosses = App\Models\User::doesntHave('boss')->orderBy('name', 'asc')->get();
$users = App\Models\User::orderBy('name', 'asc')->get();
@endphp
@if(Session::has('created_attributes'))
<div class="alert alert-info">
<p>
Saved attributes:
</p>
<ul>
@foreach(Session::get('created_attributes') as $name => $value)
<li>{{ $name }} = {{ $value }}</li>
@endforeach
</ul>
</div>
@else
<x-larastrap::form route="autoread_save" error_bag="create" novalidate autoread>
<x-larastrap::hidden name="type" value="create" skip_autoread />
<x-larastrap::text nprefix="create_test_" name="name" label="Name" required />
<x-larastrap::email nprefix="create_test_" name="email" label="E-Mail" required />
<x-larastrap::password nprefix="create_test_" name="password" label="Password" required />
<x-larastrap::radios-model nprefix="create_test_" name="boss" label="Select a Boss" :options="$bosses" required />
<x-larastrap::checklist-model nprefix="create_test_" name="friends" label="Select friends" :options="$users" />
</x-larastrap::form>
@endif
<form id="form-3ef815416f775098fe977004015c6193" class="" method="post" action="https://larastrap.madbob.org/autoread_save" novalidate="" name="form-3ef815416f775098fe977004015c6193">
<input type="hidden" name="_token" value="y0syY0Rjc1xJ3D6a5yxGWt7zWFUtHrLTLZijPknL" autocomplete="off"> <input id="hidden-749ca74e169175620c914e89c4617754" type="hidden" class="" name="type" value="create" novalidate="">
<div class="row mb-3" novalidate="">
<label for="input-3ee36e77a8331bd1f676ffd1a10f9dd6" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-3ee36e77a8331bd1f676ffd1a10f9dd6" type="text" class="form-control" name="create_test_name" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-621f8c486bfc383a4d4d4a3442f65ec2" class="col-4 col-form-label">E-Mail</label>
<div class="col-8">
<input id="input-621f8c486bfc383a4d4d4a3442f65ec2" type="email" class="form-control" name="create_test_email" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-18bf55cd41c79c6434b298c03c905b26" class="col-4 col-form-label">Password</label>
<div class="col-8">
<input id="input-18bf55cd41c79c6434b298c03c905b26" type="password" class="form-control" name="create_test_password" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="radios-2ccd63a09163c4eb39b5bf61071ac9f8" class="col-4 col-form-label">Select a Boss</label>
<div class="col-8">
<div id="radios-2ccd63a09163c4eb39b5bf61071ac9f8" class="btn-group" role="group" novalidate="">
<input type="radio" class="btn-check" id="radios-2ccd63a09163c4eb39b5bf61071ac9f8-d3d3570e50c3a7b56579c72c44c3bd8c" name="create_test_boss" value="7" autocomplete="off"> <label class="btn btn-outline-primary" for="radios-2ccd63a09163c4eb39b5bf61071ac9f8-d3d3570e50c3a7b56579c72c44c3bd8c">Eliezer Luettgen</label> <input type="radio" class="btn-check" id="radios-2ccd63a09163c4eb39b5bf61071ac9f8-1669d9a0bc26b289ae113d7621d70c9b" name="create_test_boss" value="9" autocomplete="off"> <label class="btn btn-outline-primary" for="radios-2ccd63a09163c4eb39b5bf61071ac9f8-1669d9a0bc26b289ae113d7621d70c9b">Erica Krajcik</label> <input type="radio" class="btn-check" id="radios-2ccd63a09163c4eb39b5bf61071ac9f8-4f0c0cbead6ae07c8be71951fc29d12e" name="create_test_boss" value="8" autocomplete="off"> <label class="btn btn-outline-primary" for="radios-2ccd63a09163c4eb39b5bf61071ac9f8-4f0c0cbead6ae07c8be71951fc29d12e">Gerard McGlynn</label> <input type="radio" class="btn-check" id="radios-2ccd63a09163c4eb39b5bf61071ac9f8-631e06390b23157fab1c3cce98e4b9d9" name="create_test_boss" value="6" autocomplete="off"> <label class="btn btn-outline-primary" for="radios-2ccd63a09163c4eb39b5bf61071ac9f8-631e06390b23157fab1c3cce98e4b9d9">Prof. Elmo Rosenbaum</label> <input type="radio" class="btn-check" id="radios-2ccd63a09163c4eb39b5bf61071ac9f8-d5ffd3be773ccd80ad616da04e9f8016" name="create_test_boss" value="10" autocomplete="off"> <label class="btn btn-outline-primary" for="radios-2ccd63a09163c4eb39b5bf61071ac9f8-d5ffd3be773ccd80ad616da04e9f8016">Shany Bosco</label>
</div>
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="checklist-3c08825ec08433c362d1357e85eab8b0" class="col-4 col-form-label">Select friends</label>
<div class="col-8">
<div class="form-control-plaintext">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-1bb08e28e93bfd7b7ad823611325d50d" name="create_test_friends[]" value="2" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-1bb08e28e93bfd7b7ad823611325d50d">Ebba Olson I</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-9d31a793d380387c12b3dc978b819a7b" name="create_test_friends[]" value="5" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-9d31a793d380387c12b3dc978b819a7b">Edwina Hettinger</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-d3d3570e50c3a7b56579c72c44c3bd8c" name="create_test_friends[]" value="7" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-d3d3570e50c3a7b56579c72c44c3bd8c">Eliezer Luettgen</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-1669d9a0bc26b289ae113d7621d70c9b" name="create_test_friends[]" value="9" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-1669d9a0bc26b289ae113d7621d70c9b">Erica Krajcik</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-0a2a1c575a4e367b62bf138c10549edd" name="create_test_friends[]" value="4" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-0a2a1c575a4e367b62bf138c10549edd">Geraldine Abshire IV</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-4f0c0cbead6ae07c8be71951fc29d12e" name="create_test_friends[]" value="8" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-4f0c0cbead6ae07c8be71951fc29d12e">Gerard McGlynn</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-2492875cb979dac7a20a4b6834f1f28e" name="create_test_friends[]" value="3" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-2492875cb979dac7a20a4b6834f1f28e">Jessyca Goyette</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-66096f75ef0bcff89ac90afc09cc0810" name="create_test_friends[]" value="1" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-66096f75ef0bcff89ac90afc09cc0810">Jules Wunsch</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-631e06390b23157fab1c3cce98e4b9d9" name="create_test_friends[]" value="6" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-631e06390b23157fab1c3cce98e4b9d9">Prof. Elmo Rosenbaum</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checklist-3c08825ec08433c362d1357e85eab8b0-d5ffd3be773ccd80ad616da04e9f8016" name="create_test_friends[]" value="10" autocomplete="off"> <label class="form-check-label" for="checklist-3c08825ec08433c362d1357e85eab8b0-d5ffd3be773ccd80ad616da04e9f8016">Shany Bosco</label>
</div>
</div>
</div>
</div><input id="hidden-0728f693290cc1826027a7cd1ac8e15c" type="hidden" class="" name="larastrap_autoread" value=
"eyJpdiI6IitLWHdvbVNXamRBaW5vdVpuVUROVUE9PSIsInZhbHVlIjoiQ283MWN1SXRwYXFrSjFqQkNPODRHMEx2TmpPTVFuSWpVY3VlMVVxaDhHMFB2NW85TWFoZDdwcW84VWFZaG1UenRsNlkweVNROXdQdnJIcmJ6VXRRZHlzVTV1bXZTT2poMUI2MGVhZ1Nad056UXFnL1hKMmdrK0F4MGlpbWFPNGlKNzU4T2lSdzhCQ1p1cGJ1dTZuUFFPUEJTS0Y4bVBwQVVmWkF1SW9zTnc2MmhZSDhadDNsbWNPS25ZNVovdTR2NzdYUnl6WFVqanVkSllSbTdyWWtDVmhwd2N0YnFzYXpqYy91Sy81QTNzekN2cGx1cFEvVW54RldycmkzOHo3MnYyWFZWSGp1VEI3S3dJeTFMTUVtV1J3aHg1NzBJWGVTN1JSRWlIcXVyeHphdnJZZ2c2Y2xXTGFVV3hiZHFJRWE3WWsrMDRRa1JGQVVpUFg5Z2NiR2tqREkzMUhjbndwT3pqcmNGOExIdGZmcWpyeU9RMzc1bjgyVWw4T0o5MWwvU1l2QmFFM1ZPVE00YjgrU3Fyb3lEVmY2WDRuNFFpNldDQjlZUkZQK3BWTW9ZMGFqcHBlUWxaM3g4QXAzdEk5NEVmMHpsYXNkd3VmVXBDbityYnJJQU9xTGN6cU1GcHdJZG92T1l1NThtTHdDWVJORnRDNVB2MGVpWHNSOUhHR2kraW44RDF1MnoyNk1mb29haFpXaW9FVzlBYkk1TjVUbzEvZlJ3SGtZUE1lS0V1WitybG1QVTJTZmU4cTJGSGVqNC9uRnVaa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RmODIxOTM3IiwidGFnIjoiIn0=" novalidate="">
<div class="col-12 text-end">
<button id="button-4ae8163493877a430d3483e7e90a0e20" class="btn btn-primary" novalidate="" type="submit">Save</button>
</div>
</form>
The reference Model class intended to be saved may implement the AutoReadsFields
interface for improved control over actual management of each submitted field. When this interface is used, each matching field retrieved in the incoming Request is first processed by your own implementation of the autoreadField()
method and, only when it returns AutoReadOperation::Auto
, the field is automatically managed.
This is in particular required for File input types, as Larastrap does not makes any assumption on how and where to store uploaded files.
@php
/*
namespace App\Models;
use MadBob\Larastrap\Integrations\AutoReadsFields;
use MadBob\Larastrap\Integrations\AutoReadOperation;
class User implements AutoReadsFields
{
public function autoreadField(string $name, Request $request): AutoReadOperation
{
if ($name == 'avatar') {
Session::flash('avatar_file', $request->file('avatar')->getClientOriginalName());
return AutoReadOperation::Managed;
}
return AutoReadOperation::Auto;
}
}
*/
@endphp
@if(Session::has('avatar_file'))
<div class="alert alert-info">
Uploaded the file {{ Session::get('avatar_file') }}<br>
Nothing has been saved on the server!
</div>
@else
<x-larastrap::form route="autoread_save" error_bag="file" novalidate autoread>
<x-larastrap::hidden name="type" value="file" skip_autoread />
<x-larastrap::text name="name" label="Name" required />
<x-larastrap::email name="email" label="E-Mail" required />
<x-larastrap::password name="password" label="Password" required />
<x-larastrap::file name="avatar" label="Avatar" required />
</x-larastrap::form>
@endif
<form id="form-a3c65c2974270fd093ee8a9bf8ae7d0b" class="" method="post" action="https://larastrap.madbob.org/autoread_save" enctype="multipart/form-data" novalidate="" name="form-a3c65c2974270fd093ee8a9bf8ae7d0b">
<input type="hidden" name="_token" value="y0syY0Rjc1xJ3D6a5yxGWt7zWFUtHrLTLZijPknL" autocomplete="off"> <input id="hidden-b2df40ebb725b0981d253edd85cc4e94" type="hidden" class="" name="type" value="file" novalidate="">
<div class="row mb-3" novalidate="">
<label for="input-d151ad9860e85126174315060658775e" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="input-d151ad9860e85126174315060658775e" type="text" class="form-control" name="name" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-864f84ef7742b949693110a060dcf903" class="col-4 col-form-label">E-Mail</label>
<div class="col-8">
<input id="input-864f84ef7742b949693110a060dcf903" type="email" class="form-control" name="email" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-397e5d69f76632d57e76009be3173e87" class="col-4 col-form-label">Password</label>
<div class="col-8">
<input id="input-397e5d69f76632d57e76009be3173e87" type="password" class="form-control" name="password" value="" required="" novalidate="">
</div>
</div>
<div class="row mb-3" novalidate="">
<label for="input-6091a743cb32331608b371790b148bc2" class="col-4 col-form-label">Avatar</label>
<div class="col-8">
<input id="input-6091a743cb32331608b371790b148bc2" type="file" class="form-control" name="avatar" value="" required="" novalidate="">
</div>
</div><input id="hidden-01ad71fa827756d7f41e29de8814d249" type="hidden" class="" name="larastrap_autoread" value="eyJpdiI6IjBBUDdQZENRcjRkTEZ6RlBFdSs3N1E9PSIsInZhbHVlIjoieGlRZUxrWGxwZURkYk1xUXluRFZjVlFvUkhLSWNvOXlrNWVIMGhLNUxGVlVBazdCYkJNbnpzTU1TS0J6N00ydFJWUzVzdC9uSEtTTU1xRWFiUkNFR3lPeU9xall4aWRIR3p1L1NGWDZvVWova1pLRUtFVzlDTFBmSXlsc0NBYlhwME9WU1l1cGdqSjBLaklCZTB4RXBOOC82TWp1Q0ZPT0hORmJMZkhyWEk5NlB2VTgzM1ROZEdzN3E1UTk5Vk9mOGN1eUlXbWdWOGpSYnFka1Z6T2VEbnVJYTBRVHJrVUl4a2tOTWFzRFRJUTJjNUJIbTNqNktoRHlNNURDZGFkWkZaRXVxeU1TN3p0K1p0RkdvVVk3TUc1ZzZXaGcvQ0ZwbUpTYk55RU1BYWlONHA0K1cvQkZYcVM5YVdleFdMS1pkSmV5TmliYkJnMlVUVGlyd0NWWklpRDlrbnk3OGRzWjZYdlpoZU5CZlYxSmE0NlBJNFZUUzRXenYrUUVFaWJHS3FZNzl4RDNaR3dtZ2VFRDdGRjFOajlSc3NLb1R5Tk9xbkJleVV1RjdPYXB4M1piR3YrUGtsM0xocmNYeXdrWVNqL0Q1eitGQjNNRGJvc0NBT2hrQWlJcVZzY0xxdzJkaUE0VkpWQmJ5RzJ3eDBXZGY0dVJhNlVvOXpTTHAzdkoway9vTUk3Uk5iRkdvZVdveFVhSEw4YVdjVG1yZmRnNUFTSWVrcHFIMmVjbjhhWUdIWmFqdjE5eEQ4ZmJEYkgxSzJxT0ZtRVhsUXdjSDNnT2hWM2ZCUThuRUM2Qk8yRUIvd0xqTzlyejJaMG5VQXVKN1lVNGpLREdwbVZXNVN0enJ5NDFkaGdtb3BJaUZ5UGE0L3pFYzVCZXBVdFY1MUxMUHA3Y282Smh0OXJLcmFSVG45TGhSa09LMHZLUFNGeVJ3cHFqYVdvUlREL1hFSG1GS01RTXJrdHVWUnpPOWVvVWoxNFZudDBRVmlBYS9yYzBZU1NsdGtvK0k0aE5WL2ZxTGkwWTdqemowUEdtTE4zT3NjQTh5UlRhUWxTL1gvb1djdVAzMFQzeXZiblhrNCtYV1hTVnNoVVYrUzRCMEJEaVUyaVB5Sjhvblk3d0FrRGduSlQ4Z0xCSHVXNitQY3RFM2R5bmZkYWt3VGtUZW5FbEFYSWJKUmZMT21vZVF1SktMblV1RTA2emtRTHZmWkxneXI3eFZ3PT0iLCJtYWMiOiI2YzUwYzBmOTllZTNlZmM1NGQ2MWQ5ODM4YWFlYTZmMWZkMWFlM2I0NDAyMjkyN2NhNDhmNzdiMTZkZmI3OThjIiwidGFnIjoiIn0=" novalidate="">
<div class="col-12 text-end">
<button id="button-ff891f50d9e9d60f5fd087dcd05094ab" class="btn btn-primary" novalidate="" type="submit">Save</button>
</div>
</form>
When testing a Controller method based on LarastrapStack::autoreadSave()
, it is important to note that the submitted form requires to contain his own autoread representation to be correctly handled. To easy this, it is provided the LarastrapStack::autoreadRender()
function intended to render a template and obtain the relevant parts of the request to be submitted.
$request = LarastrapStack::autoreadRender('my.template', $data_for_the_template);
$request = array_merge($request, $data_for_the_request);
$response = $this->post('/the/endpoint/for/the/rendered/form', $request);