通过 ENUM 值验证或验证幼虫

我要在 Laravel 启动。 我搜索并没有找到如何使用某些 ENUM 值验证数据。在下面的代码,我需要的 type必须只是 DEFAULTSOCIAL。这样或那样:

$validator = Validator::make(Input::only(['username', 'password', 'type']), [
'type' => '', // DEFAULT or SOCIAL values
'username' => 'required|min:6|max:255',
'password' => 'required|min:6|max:255'
]);

有可能吗?

103062 次浏览

in:DEFAULT,SOCIAL
The field under validation must be included in the given list of values.

not_in:DEFAULT,SOCIAL
The field under validation must not be included in the given list of values.

$validator = Validator::make(Input::only(['username', 'password', 'type']), [
'type' => 'in:DEFAULT,SOCIAL', // DEFAULT or SOCIAL values
'username' => 'required|min:6|max:255',
'password' => 'required|min:6|max:255'
]);

The accepted answer is OK, but I want to add how to set the in rule to use existing constants or array of values.

So, if you have:

class MyClass {
const DEFAULT = 'default';
const SOCIAL = 'social';
const WHATEVER = 'whatever';
...

You can make a validation rule by using Illuminate\Validation\Rule's in method:

'type' => Rule::in([MyClass::DEFAULT, MyClass::SOCIAL, MyClass::WHATEVER])

Or, if You have those values already grouped in an array, you can do:

class MyClass {
const DEFAULT = 'default';
const SOCIAL = 'social';
const WHATEVER = 'whatever';
public static $types = [self::DEFAULT, self::SOCIAL, self::WHATEVER];

and then write the rule as:

'type' => Rule::in(MyClass::$types)

You can use the Rule class as te documentation indicates. For example, having the following definition in a migration:

$table->enum('letter',['a','b','c']);

Now your rules for your FormRequest should put:

class CheckInRequest extends FormRequest
{
public function authorize()
{
return true;
}




public function rules()
{
return [
'letter'=>[
'required',
Rule::in(['a', 'b','c']),
],
];
}
}

Where Rule::in (['a', 'b', 'c']), must contain the values of your field of type "enun"

This is working fine for me on Laravel 8.x

Laravel 9+

https://laravel.com/docs/9.x/validation#rule-enum

use App\Enums\ServerStatus;
use Illuminate\Validation\Rules\Enum;
 

$request->validate([
'status' => [new Enum(ServerStatus::class)],
]);

Enum:

namespace App\Enums;


enum ServerStatus: string {
case ACTIVE = 'active';
case INACTIVE = 'inactive';
}

Required PHP 8.1+

Laravel 9 php8.1

$request->validate([
'type' => [new Enum(TypeEnum::class)],
]);

Less or Equal php 8

you can own enum

class BaseEnum
{
/**
* Returns class constant values
* @return array
*/
public static function toArray(): array
{
$class = new \ReflectionClass(static::class);


return array_values($class->getConstants());
}


/**
* @return string
*/
public function __toString(): string
{
return implode(',', static::toArray());
}
}

Child enum

class TypeEnum extends BaseEnum
{
public const DEFAULT = 'default';
public const SOCIAL = 'social';
}

in validation u can use it in two different ways

first

$request->validate([
'type' => 'in:' . new TypeEnum(),
]);

second

use Illuminate\Validation\Rule;


$request->validate([
'type' => Rule::in(TypeEnum::toArray())
]);

Laravel

//migration
$table->enum('type',[true, false])->default(true);


//controller
$request->validate([
'type' => ['required','in:true,false']
]);