-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[11.x] Define global validation logic for Laravel Prompts #49497
[11.x] Define global validation logic for Laravel Prompts #49497
Conversation
Side notes for @jessarcher: I may have found an alternative solution to aliases for setting the field name and allowing a complete control of developers over the console output. By default, when we pass the validation rules to the Prompts, the field name text('What is your name', validate: 'required|min:2');
// or
text('What is your name', validate: ['required', 'min:2']); In the above example, we would get the error If we pass an associative array - instead - the key of the array is used as the field name: text('What is your name', validate: ['name' => 'required|min:2']);
// or
text('What is your name', validate: ['name' => ['required', 'min:2']]); In the above example, we would get the error This allows us to have full control over the output by customising the error messages or the attributes, without having to introduce aliases in Prompts. This PR has been updated with this logic, please let me know your thoughts :) |
Please note that draft PR's aren't reviewed. Tests seem to fail here as well. |
Thanks @driesvints. As per description, this PR is in draft because it depends on the code of this Laravel Prompts PR. Once a new version of Laravel Prompts is tagged including this merged PR, I'll make sure to require the new version in this PR. That will fix the tests and I'll then set this PR as ready for review 👍 |
Sorry missed that 👍 |
This PR is now ready for review 👍 // we can pass a callable to `validate` as we were doing before (non-breaking)
$name = text('What is your name?', validate: fn ($value) => $value == '' ? 'Required!' : null);
// we can pass Laravel validation rules to `validate` and get a validation error like `The answer field is required.`
$name = text('What is your name?', validate: 'required');
// we can pass an associative array to `validate` and get a custom validation error like `The name field is required.`
$name = text('What is your name?', validate: ['name' => 'required']);
// we can also define the methods messages() and attributes() in our Command for a total control over the console output
protected function messages()
{
return ['name.required' => 'Your :attribute is mandatory.'];
}
protected function attributes()
{
return ['name' => 'full name'];
}
// the above will print `Your full name is mandatory.` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested this locally and it works great!
This PR is currently a draft because it depends on this PR on Laravel Prompts.
The end goal is to define a global validation logic for Laravel Prompts that leverages the Laravel validation rules, while keeping Laravel Prompts framework-agnostic.
Developers can then reuse the existing validation rules when calling Laravel Prompts:
This PR aims to take full advantage of the Laravel validator features by optionally letting developers define custom error messages or attributes, e.g.:
Update: description updated to reflect this comment.