Standalone form validation

im having another brain fart. When doing a standalone form with a submit button that is tied to submit(), how can ensure rules() within the form are valid before it will submit? simple things like required halt it using a standard html5 response, but a custom rules() does not. I am using
->rules([
function ($attribute, $value, $fail) {
if ($value !== auth()->user()->name) {
$fail('Your signature must match your name on file.');
}
},
])
->rules([
function ($attribute, $value, $fail) {
if ($value !== auth()->user()->name) {
$fail('Your signature must match your name on file.');
}
},
])
16 Replies
LeandroFerreira
->rules([
function () {
return function (string $attribute, $value, Closure $fail) {
if ($value !== auth()->user()->name) {
$fail("Your signature must match your name on file.");
}
};
},
])
->rules([
function () {
return function (string $attribute, $value, Closure $fail) {
if ($value !== auth()->user()->name) {
$fail("Your signature must match your name on file.");
}
};
},
])
Mark Chaney
Mark ChaneyOP2y ago
@leandro_ferreira no dice
Dan Harrin
Dan Harrin2y ago
BEFORE submit only happens with browser validation php functions are not part of browser validation only a limited set of rules are supported like required, maxlength, numeric
LeandroFerreira
sorry, I assumed that your function wasn't working
Mark Chaney
Mark ChaneyOP2y ago
so with php then and doing a submit() method, is there a way to enforce those per field rules in some fashion or am i forced to create all the validation within the submit using the normal laravel methods?
awcodes
awcodes2y ago
Html5 validation only works with certain attributes on certain types of inputs. Given your example there would be no way to do a native validation against a server side variable without a request.
Mark Chaney
Mark ChaneyOP2y ago
i fully understand that
LeandroFerreira
While JavaScript validation is an option as well, it requires duplicating your frontend/backend validation rules. In my opinion, utilizing Laravel's validation is the superior approach.
Mark Chaney
Mark ChaneyOP2y ago
Im really confused here. Obviously rules() has a place outside of just html5 validation, correct? I mean, i dont care if it does a request or not
awcodes
awcodes2y ago
Of course it does, but since it needs a request it can’t disable the submit.
Mark Chaney
Mark ChaneyOP2y ago
ok, so with the submit(), how do you utilize those rules....
awcodes
awcodes2y ago
Once it’s submitted running $this->form->getState() will run the validation and should kick back with any errors.
Mark Chaney
Mark ChaneyOP2y ago
thanks. thats all i needed. This form isnt for saving any data from the actual form, so i wasnt doing that, thus the validation wasnt happeing.
Mark Chaney
Mark ChaneyOP2y ago
yep, i see that at the bottom. my problem is that i wasnt looking at the docs for getting data, i had looked at the validation section
Want results from more Discord servers?
Add your server