Reputation: 1
i am diving deeper into kohana and i like it verry much. especialy the hmvc stuff and much more. at the moment i have problems with adding my own rules to the validation instance.
validation runs well, and i also think that my own function get called correctly. but the problem is that the error message for my own validation function is not be shown.
maybe someone can look into the code to see what i did wrong. thanks!
here is my code i deleted some stuff to shorten it up a bit:
class Controller_Bookmarks extends Controller_DefaultTemplate
{
public function action_create_bookmark()
{
$posts = new Model_Post();
if($_POST){
$post = new Validate($_POST,$_FILES);
//attaching rules
$post ->rule('bookmark_title', 'not_empty')
->rule('bookmark_image', 'Model_Post::email_change');
//add error for custom functionm
$post->error('bookmark_image', 'email_change');
if ($post->check())
{
echo 'yeah';
}else{
print_r($post->errors('validate'));
}
}else{
}
$this->template->content = View::factory('pages/create_bookmark');
}
}
my model:
class Model_Post extends Kohana_Model
{
public function email_change($str)
{
return false;
}
}
my error message definition messages/validate.php (just for testing):
<?php defined('SYSPATH') or die('No direct script access.');<br />
return array(
'alpha' => ':field must contain only letters',
'alpha_dash' => ':field must contain only letters and dashes',
'alpha_numeric' => ':field must contain only letters and numbers',
'color' => ':field must be a color',
'credit_card' => ':field must be a credit card number',
'date' => ':field must be a date',
'decimal' => ':field must be a decimal with :param1 places',
'digit' => ':field must be a digit',
'email' => ':field must be a email address',
'email_domain' => ':field must contain a valid email domain',
'exact_length' => ':field must be exactly :param1 characters long',
'in_array' => ':field must be one of the available options',
'ip' => ':field must be an ip address',
'matches' => ':field must be the same as :param1',
'min_length' => ':field must be at least :param1 characters long',
'max_length' => ':field must be less than :param1 characters long',
'phone' => ':field must be a phone number',
'not_empty' => ':field rrrrrrrrrrrrrrrrrrrrr must not be empty',
'range' => ':field must be within the range of :param1 to :param2',
'regex' => ':field does not match the required format',
'url' => ':field must be a url',
'email_change' => ':field gdffddfgdfhgdfhdfhhdfhdfhdfhd',
);
Upvotes: 0
Views: 1655
Reputation: 5483
You should add errors inside ->check()
call. Note that after you called $post->check()
, Validate object clears existing errors! You can see it by using $post->errors()
(without params) - there will be no such error message.
Upvotes: 3