-
Notifications
You must be signed in to change notification settings - Fork 94
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
Support for custom validation and aliases #102
Support for custom validation and aliases #102
Conversation
@jessarcher please note that there is a PHPStan error on the |
Hey @cerbero90, I dig this approach for adding Laravel's validation rules without increasing the dependencies. What are your thoughts on leaving out the |
Thanks for your quick reply @jessarcher :) I'm always in favour of simplicity 👍 the reason for the aliases is merely for being able to also define custom error messages and attributes for each prompt individually. Having
An alternative to the current default aliasing may be converting the label into snake case. That would work well when we use something like If you prefer, we can get rid of the aliases altogether but we would encounter the 2 problems listed above 🤔 |
I think I'm okay with those problems, at least for the initial version of this feature. We can always add them in a follow-up. But just to make sure I fully understand, can you please elaborate a little more on the problems and specifically what type of customization you'd want to make that wouldn't be possible? Are you just wanting to be able to make the message say "The full name field is required" instead of "the answer field is required"? I've also fixed the PHPStan error in |
Sure, no worries :) and yes, the issue with dropping the aliases is only related to the possibility to customise the name of the field and the related error messages. If we want to have an error completely different from the default provided by the validator, we can't. Without aliases we lose the ability to completely being able to customise our output. Another drawback is for all the validation rules that depend on other fields, like the following:
But if you are ok with that at this stage, I'll remove the aliases from this PR 👍 |
Thanks! Would interdependent validation even make sense when each prompt is validated in isolation? |
Yes you are right, I didn't think about that :) |
Aliases removed 👍 |
@jessarcher if it's possible to bump the version of Prompts once this PR is merged, I'll make sure to require the new version in this PR :) |
Sure thing - thanks! |
Following up the improvement for checking the Prompts validation when testing, this PR introduces the ability to globally set a custom validation logic for Laravel Prompts.
The end goal is to be able to validate Laravel Prompts by leveraging the existing Laravel validation rules, while keeping Laravel Prompts framework agnostic.
To achieve this, the static method
Prompt::validateUsing()
can be called to determine the global logic to validate Laravel Prompts. If a prompt has its own validation logic set via the parametervalidate
, such logic has the precedence over the global validation logic.If this PR gets merged, we can then set the global validation logic in the Laravel framework by updating the
ConfiguresPrompts
trait like this draft PR is currently doing:The above logic would let the user pass the Laravel validation rules to the
validate
parameter like this:and take full advantage of the validator features like setting custom error messages or custom attributes.
This PR also introduces aliases so that each prompt can be uniquely differentiated. Useful for both validation and any future use-case that needs to distinguish a prompt in a short and unique way.
The alias can be set via the
as
parameter, e.g.:Otherwise the alias is automatically inferred by counting each prompt i.e.
prompt_1
,prompt_2
, etc.Ultimately this PR is designed to keep Laravel Prompts framework agnostic and let the frameworks decide their own preferred way to validate Laravel Prompts.