1

I'm trying to create a custom validation rule for laravel but I keep getting the following error: Method [validateOriginalAlt] does not exist.

I have 2 classes.

FormValidator.php

abstract class FormValidator extends Validator
{

    protected $validator;
    protected $validation;
    protected $messages = [];
    protected $rules = [];

    function __construct(Validator $validator)
    {
        $this->validator = $validator;
    }


    public function validate(array $formData)
    {
        $this->validation = $this->validator->make($formData, $this->getValidationRules(), $this->getValidationMessages());

        if ($this->validation->fails()) {
            throw new FormValidationException('Validation Failed', $this->getValidationErrors());
        }

        return true;
    }

    public function setRules($rules)
    {
        $this->rules = $rules;
        return $this;
    }

    protected function getValidationRules()
    {
        return $this->rules;
    }

    protected function getValidationMessages()
    {
        return $this->messages;
    }

    protected function getValidationErrors()
    {
        return $this->validation->errors();
    }
}

Show.php

class Show extends FormValidator
{

    protected $rules = [
        'title' => 'required|unique:shows,title',
        'slug' => 'unique:shows,slug',
        'flexget' => 'boolean',
        'airing' => 'boolean',
        'flexget_titles' => 'original_alt',
    ];


    public function validateOriginalAlt($string)
    {
        dd($string);
    }
}

In my controller I call the validation method like so:

    try {
        $this->showValidator->setRules([
            'title' => 'required',
            'slug' => 'unique:shows,slug,' . $id,
            'flexget_titles' => 'original_alt',
        ])->validate($data);
    } catch (Animekyun\Forms\FormValidationException $e) {
        return Redirect::back()->withErrors($e->getErrors())->withInput();
    }

Any ideas on what I could be doing wrong?

1
  • It turns out I need to register my validation extension to the IoC container. Not sure how to do that in a clean way without using facades like in the docu Commented Apr 19, 2015 at 11:55

1 Answer 1

1

It turns out I need to register my validation extension to the IoC container. Not sure how to do that in a clean way without using facades like in the docu

The best place for such things is a Service Provider. You can use the existing App\Providers\AppServiceProvider or create a new one (like ValidatorServiceProvider)

Anyways you should then register the validator in the boot() method of your provider:

public function boot()
{
    $this->app['validator']->resolver(function($translator, $data, $rules, $messages)
    {
        return new Show($translator, $data, $rules, $messages);
    });
}

As you can see it's pretty much the same as with the Facade example from the docs. That's because a Facade is just an accessor to the underlying class, which can be accessed with app['validator'].

One last thing, consider renaming Show to something more expressive like ShowValidator.

Update

For your case I'd suggest something like this:

class Show extends FormValidator {
    public function __construct(Validator $validator){
        $validator->extend('original_alt', 'Show@validateOriginalAlt');
        parent::__construct($validator);
    }
}

You could also do that in a service provider or globally, but that would mean that is available everywhere and I get the feeling you don't want that.

Sign up to request clarification or add additional context in comments.

10 Comments

The problem I am facing now is that when the Show validator is instantiated, 4 arguments are passed. My FormValidator constructor only accepts the Factory class for the Validator. And your last tip is a good one, I was already in the process of refactoring! Thanks!
Hmm I see. What Validator class are you even extending? I personally have only done it like in the docs (extend Illuminate\Validation\Validator) and therefore inherit the constructor with those arguments....
I followed Jeffrey Way's tutorial on form validation and I'm extending Illuminate\Validation\Factory. The reason for that is because then I can actually make the validator and pass in my rules and messages
It is indeed! [I have to add more text or i can't comment]
The problem is that $this->validator is still the default Validator class and not your custom one with the validateOriginalAlt() method. Can you give me the link of that tutorial?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.