dimanche 19 avril 2015

Custom validation rule in laravel

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?


Aucun commentaire:

Enregistrer un commentaire