Conditional Enum Validation Rules in Laravel

Conditional Enum Validation Rules in Laravel

In Laravel applications, validation is a crucial aspect to ensure data integrity and consistency. Often, we need to validate data based on certain conditions, such as user roles or other contextual factors. In this tutorial, we’ll explore how to implement conditional enum validation rules in Laravel, allowing us to validate data based on the value of an enum attribute and additional conditions.

Prerequisites:

  • Basic understanding of Laravel framework
  • Familiarity with enums and validation rules in Laravel

Understanding Enums in Laravel:

Enums (enumerations) in Laravel are a convenient way to define a set of named constants. They provide a clear and semantic way to work with predefined values, such as status codes, types, or options.

Example: Creating a PostStatus Enum

namespace App\Enums;

use BenSampo\Enum\Enum;

final class PostStatus extends Enum
{
    const Draft = 'Draft';
    const Pending = 'Pending';
    const Approved = 'Approved';
}

Implementing Conditional Validation Rules:

Scenario:

We want to validate a form field based on the user’s role. For example, if the user is an admin, they can select any post status. However, non-admin users can only select ‘Pending’ or ‘Approved’ status.

Using the Conditionable Trait:

In Laravel, we can use the Conditionable trait to define conditional validation rules. This trait provides methods like when and unless for conditional validation.

Code Example:

use Illuminate\Validation\Rule;
use App\Enums\PostStatus;

// Inside the validation rules array
'status' => [
    'required',
    Rule::in([PostStatus::Draft, PostStatus::Pending, PostStatus::Approved])
        ->when(auth()->user()->is_admin, function ($rule) {
            $rule->only([PostStatus::Draft, PostStatus::Pending, PostStatus::Approved]);
        }, function ($rule) {
            $rule->only([PostStatus::Pending, PostStatus::Approved]);
        })
]

Creating a Form Request:

To encapsulate the validation logic, we can create a Form Request class.

Example:

php artisan make:request PostRequest

Now, let’s apply the logic.

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Enums\PostStatus;

class PostRequest extends FormRequest
{
    public function authorize()
    {
        return true; // Authorization logic
    }

    public function rules()
    {
        return [
            // Other rules...
            'status' => [
                'required',
                Rule::in([PostStatus::Draft, PostStatus::Pending, PostStatus::Approved])
                    ->when(auth()->user()->is_admin, function ($rule) {
                        $rule->only([PostStatus::Draft, PostStatus::Pending, PostStatus::Approved]);
                    }, function ($rule) {
                        $rule->only([PostStatus::Pending, PostStatus::Approved]);
                    })
            ]
        ];
    }
}

You can get more details about this topic from here.

To get to know more about Laravel, you can check these articles too.

Conclusion:

In this tutorial, we learned how to implement conditional enum validation rules in Laravel using the `Conditionable` trait and the `when` method. By defining validation rules based on user roles and other conditions, we can ensure that our application’s data remains consistent and valid.

Please follow and like us:

Related Posts

Leave a Reply

Share