arun
arun

Reputation: 4815

Laravel Form Request validation with custom validation Rule, didn't work to find all space characters

This is my register controllers, register method:

public function register(StoreUser $request)
    {
            $user = $this->create($request->all());

            $user = User::findOrFail($user->id);

            Mail::to($user->email)->queue(new EmailVerification($user));

            return view('auth.verify-email');

    }

Here is the StoreUser Request:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use App\Rules\allAreSpaces;

class StoreUser extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        return [
            'email' => 'required|email|max:64',
            'phone' => 'bail|required|numeric|phone_min|phone_max|unique:users|not_in:0',
            'password' => [new allAreSpaces, 'required', 'min:8', 'max:16', 'confirmed'],
            'password_confirmation' => 'required',
        ];

    }

    public function messages()
    {
        return [
            'email.required'     => 'Email address cannot be empty',
            'email.email'     => 'Enter a valid Email address',
            'email.max'     => 'Email address cannot exceed 64 characters',
            'email.unique'     => 'Email already exists',


            'phone.required'     => 'Mobile number cannot be empty',
            'phone.numeric'     => 'Mobile number has to be numeric',
            'phone.phone_min'     => 'Mobile number should contain a minimum 5 characters',
            'phone.phone_max'     => 'Mobile number cannot exceed 11 characters',
            'phone.unique'     => 'Mobile number already exists',
            'phone.not_in'     => 'Enter a valid mobile number ',


            'password.required'     => 'Password cannot be empty',
            'password.min'     => 'Password should contain minimum 8 characters',
            'password.max'     => 'Password cannot exceed 16 characters',
            'password.confirmed'     => 'Password mismatch.Retry',

            'password_confirmation.required'     => 'Confirm Password cannot be empty',
        ];
    }


    public function withValidator(Validator $validator)
    {


        $email = $this->request->get( 'email' );  // Start with 

        $user = \App\User::Where('email', $email)->first();


        if($user && $user->activated == 0){

            $row = \App\User::find($user->id);
            $row->delete();

        }else{

            $validator->sometimes('email', 'unique:users', function ($input) {
                return true;
            });
        }


    }

}

Here is that custom Rule named 'allAreSpaces':

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class allAreSpaces 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(strlen($value) > 0){
            if (strlen(trim($value)) == 0){
                return false;   
            }
        }
        return true;

    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'Password cannot contain space character alone';
    }
}

What i want is if user type only the spaces in all password field, i want to throw the 'Password cannot contain space character alone' message. It is working when user didn't type anything. if they typed space its not working.

how can i achieve this?

is there any other easy way to show error message if all are spaces?

Upvotes: 1

Views: 2496

Answers (1)

arun
arun

Reputation: 4815

@foo.ar,
In App\Http\Kernel.php, you can see TrimStrings middleware is applied. it will trim all white spaces. if u enter only spaces in any input, it will trim those spaces and u will end up with no values in that input.

so if u want to validate spaces, u have to add the below code in

\App\Http\Middleware\TrimStrings.php

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;

class TrimStrings extends Middleware
{
    /**
     * The names of the attributes that should not be trimmed.
     *
     * @var array
     */
    protected $except = [
        'password',
        'password_confirmation',
    ];
}

Upvotes: 1

Related Questions