I use Laravel 10 and @csrf directive in my forms. I just noticed that this directive generates a w3C validation error:
An “input” element with a “type” attribute whose value is “hidden”
must not have an “autocomplete” attribute whose value is “on” or
“off”.
Because it generates this HTML code:
<input type="hidden" name="_token" value="aOZTBSeC6yErTEjpVskiG9c6YSaileoWLb6CGkrT" autocomplete="off">
I like to have pages with 0 errors (it might be useless). So, how can I no longer have this error?
2
Answers
You can manually generate the CSRF token input without the autocomplete attribute. In your form, replace
@csrf
with…This will generate a hidden input field without the autocomplete attribute, eliminating the validation error. Your form should look something like the following.
you can override the csrf_field function in your helpers.php file.
helpers.php
file in the root folderindex.php
file beforerequire __DIR__.'/../vendor/autoload.php';
addrequire __DIR__.'/../helpers.php';
csrf_field
function will be overridden by your@csrf
and render your code