-
-
Notifications
You must be signed in to change notification settings - Fork 175
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
Enums #835
Comments
Not sure if this would be an option or even possible but it looks like you can create a JsValidator from a Laravel Validator instance https://github.com/proengsoft/laravel-jsvalidation/wiki/Basic-Usage#create-javascript-validations-using-validator-instance I'm still trying to get it working and not even sure if the new validation rules would be included from the base validator or not... |
For the time being the Laravel enum handling is very wonky in the implementation it seems, and not yet very mature. For Example, json_encode(['awesome_enum_type' => ['required', 'integer', new Enum(AwesomeEnumType::class)]]) returns {"awesome_enum_type": ["required", "integer", {}]} So |
My current workaround is the following trait:
<?php
namespace App\Enums\Traits;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
trait EnumToValidation
{
/**
* Return an validation object
*
* @return array
*/
public static function toValidation(): In
{
return Rule::in(array_column(self::cases(), 'value'));
}
} which I embed in my enum with the enum AwesomeEnumType: string {
use App\Enums\Traits\EnumToValidation;
case Fruit => 'mango';
case Website => 'https://4458.rocks';
} And finally using it in the verification like this: public funciton rules(): array
{
return [
'awesome_enum_type' => [
'required',
'integer',
- new Enum(AwesomeEnumType::class),
+ AwesomeEnumType::toValidation(),
],
];
} |
Thanks for this & worked with me, but & have another issue with how to make another input as required based on a value in this array. |
Subject of the issue
Add ability to use Enums validation rule: https://laravel.com/docs/9.x/validation#rule-enum
Your environment
Steps to reproduce
Expected behaviour
Validation work like as
Rule::in()
or something.Actual behaviour
I see on browser:
Whoops, looks like something went wrong.
The text was updated successfully, but these errors were encountered: