I've been experimenting with Laravel 4 recently and am trying to get a custom validation-class to work.
Validation-Class
<?php
class CountdownEventValidator extends Validator {
public function validateNotFalse($attribute, $value, $parameters) {
return $value != false;
}
public function validateTimezone($attribute, $value, $parameters) {
return !empty(Timezones::$timezones[$value]);
}
}
My rules are setup like this:
protected $rules = [
'title' => 'required',
'timezone' => 'timezone',
'date' => 'not_false',
'utc_date' => 'not_false'
];
I call the Validator inside my model like this:
$validation = CountdownEventValidator::make($this->attributes, $this->rules);
I get the following error:
BadMethodCallException
Method [validateTimezone] does not exist.
I've looked up quite a few tutorials but I couldn't find out what's wrong with my code.
Thank you for your help
Max
Is that function supposed to be static? What if you remove
static
?When you call
Validator::make
you're actually callingIlluminate\Validation\Factory::make
although the method itself is non-static. When you hit theValidator
class you're going through the facade and in the background the call looks something like this.It then returns an instance of
Illuminate\Validation\Validator
.You can register your own rules with
Validator::extend
or you can register your own resolver. I recommend, for now, you just extend the validator with your own rules usingValidator::extend
.You can read more on this and on how to register your own resolver over on the official documentation.