Custom validation rules in Laravel 5.5

Laravel provides powerful validation rules. Laravel provides lists of the validations rules that help us to validation incoming data before saving it in the database. We can easily print validation messages on blade file. Laravel provide manually creating validation request and Automatic redirection to the form.

Validation is the most important part of any website. every website required validation at least on the contact form. Laravel provides best validation rules for validating incoming data before saving it on the database. If you want to store the valid email address in the database then you should validate the email string before saved it in the database. If you stored email string without validating it then user enter any kind of data instead of email address.

Every incoming request must be validated before going to save it on the database. So we collect the exact data as we want to get from the user. otherwise, the user never enters proper data. if the user enters anything wrong on input fields then sometimes it messes up our application.

So, Today we learn custom validation rule for validating string entered by the user is not the container with HTML tags.

Let’s start, Open command prompt and fire below command for custom validation rule.

php artisan make:rule Thecodingstuff

Our Thecodingstuff validation class file is created at “app/rules/” folder. Let’s open the file and check the string entered by the user is not contain with any HTML tags.

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Thecodingstuff implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if($value != strip_tags($value)) {
            return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute must be string.';
    }
}

Our custom validation rule is ready. Now we can check the string entered by the user is not contain with HTML tags.

Let’s How we can use our custom validation.

use App\Rules\Thecodingstuff;

$request->validate([
    'name' => ['required', new Thecodingstuff],
]);

Our validation rules are ready. Now, If found any HTML tags on name field the validation is fire like “the name must be the string.”.

If you face any issue on Larave5.5 custom validation writes the comment below.

Published by

Sherya

Hello there, I am developer currently working with the company at Pune and part-time freelancer. Always I welcome a new opportunity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s