user379888
user379888

Reputation:

Laravel 5.2 custom validator not giving error

I get no errors when the validator fails. I have a function where I want to validate the request URL.

public function update(Request $request, RequestUser $user, $id)
    {
        $this->validate($request, [
            'integration_domain' => 'required',
        ]);
        //other stuff
    }

Below is where I have created the validator,

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \Validator::extend('integration_domain', function($attribute, $value, $parameters, $validator) {
            if((strpos($parameters->input('url'), 'localhost') !== false) || 
                (strpos($parameters->input('url'), 'http://localhost') !== false) ||  
                (strpos($parameters->input('url'), 'https://localhost') !== false) ||  
                (strpos($parameters->input('url'), '127.0.0.1') !== false) ||
                (strpos($parameters->input('url'), 'http://127.0.0.1') !== false) ||
                (strpos($parameters->input('url'), 'http://127.0.0.1') !== false))
                return false;
            return true;
        });
    }
}

I have followed this answer.

Upvotes: 1

Views: 35

Answers (1)

Abhay Maurya
Abhay Maurya

Reputation: 12277

integration_domain is supposed to be the input field name, not the rule and if its the rule then you should concatenate it with required like so:

public function update(Request $request, RequestUser $user, $id)
    {
        $validatedData = $request->validate([
            'input_variable_name' => 'required|integration_domain',
        ]);
        //other stuff
    }

Its basic understanding of validation. For more details please see here

Upvotes: 2

Related Questions