Skip to content
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

Open
LocalHeroPro opened this issue Mar 10, 2022 · 4 comments
Open

Enums #835

LocalHeroPro opened this issue Mar 10, 2022 · 4 comments

Comments

@LocalHeroPro
Copy link

Subject of the issue

Add ability to use Enums validation rule: https://laravel.com/docs/9.x/validation#rule-enum

Your environment

  • version of this package: 4.7.0
  • version of Laravel: 9.4.1

Steps to reproduce

public funciton rules(): array
{
   return [
      'awesome_enum_type' => ['required', 'integer', new Enum(AwesomeEnumType::class)],
   ];
}

Expected behaviour

Validation work like as Rule::in() or something.

Actual behaviour

I see on browser: Whoops, looks like something went wrong.

@rrivera-ixis
Copy link

Hi @LocalHeroPro

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...

@luckydonald
Copy link

luckydonald commented Jul 27, 2022

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 new Enum(AwesomeEnumType::class) is only represented as a useless {}.

@luckydonald
Copy link

luckydonald commented Jul 27, 2022

My current workaround is the following trait:

app/Enums/Traits/EnumToValidation.php

<?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 use … statement:

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(),
        ], 
   ];
}

@CodeliaNet
Copy link

My current workaround is the following trait:

app/Enums/Traits/EnumToValidation.php

<?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 use … statement:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants