Testing Validation Rules in A Laravel Livewire Component

Testing Validation Rules in A Laravel Livewire Component

Testing validation rules can become quite tiresome pretty quickly if you have to write each test manually. Testing validation rules in a Laravel Livewire component. Tagged with php, laravel, livewire. In Livewire you can define validation rules by setting a rules array (shown below) and then validate using $this->validate().

Testing validation rules can become quite tiresome pretty quickly if you have to write each test manually. Luckily I’ve found a nice method that allows you to simplify your validation rule tests when using Laravel Livewire components. For this example we have a form that allows a user to update their profile information with a Livewire ProfileForm component.

In Livewire you can define validation rules by setting a rules array (shown below) and then validate using $this->validate(). In this example we are  binding to a User model that has a name, email and bio.

protected $rules = [
    'user.name' => 'required|max:200',
];

In our example we need to do something a bit more complicated so instead of using the rules array we are going to create a rules function instead, allowing us to use the Illuminate\Validation\Rule::class. Here are the rules we have set and the save() method that will validate the input.

// App\Http\Livewire\ProfileForm.php

use Illuminate\Validation\Rule;

protected function rules() 
{
    return [
        'user.id' => 'required',
        'user.name' => 'required|max:200',
        'user.email' => ['required', 'email', Rule::unique('users')->ignore($this->user->id)],
        'user.bio' => 'required|min:20|max:1000',
    ];
}

public function save()
{
    $this->validate();

    // Save the user data
    $this->user->save();
}

So how can we go about testing each of these rules? There are 8 different validation rules in this simple example, but you don’t want to have to write 8 separate tests for each rule. Instead, we can make use of a data provider to pass the data to be tested into the test, along with the expected validation rule.

php laravel livewire validation

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

How to validate mobile phone number in laravel with example

Data validation is a very important thing for a web application. In this article i will let you know how to validate mobile phone number in laravel.

Laravel 7/6 Image Validation

Laravel image validation. Here you will learn how to validate image mime type, size, and dimension in laravel app before uploading image to DB and folder.

Image Validation In Laravel 7

Laravel image validation. Here you will learn how to validate image mime type, size, and dimension in laravel app before uploading image to DB and folder.

Laravel 8 Livewire Form Wizard Tutorial Example

Form wizard in laravel 8 app with livewire. In this tutorial, you will learn how to implement form wizard using livewire in laravel 8 app.

Laravel 8 Form Validation Tutorial

Laravel 8 form validation example. In this post, i will show you how to use laravel 8 validation on form. And add custom validation message