Kohana 3.2中验证规则的自定义错误消息

时间:2012-10-16 04:16:53

标签: kohana

我有一个controller = product.php,一个model = category.php,以及一个自定义错误消息文件category.php。当我提交空表单时,我收到规则“not_empty的错误消息,但我没有收到规则的错误消息”unique_categoryname“

请帮忙!

class Controller_Product extends Controller_Application
{
    public function action_addcategory()
    {
        $errors='';
        $cat = new Model_Category();

        $validation=Validation::factory($this->request->post())
        ->rule('cat_name',array($cat,'unique_categoryname'));

        if ($validation->check())
        {

            if (HTTP_Request::POST == $this->request->method())
            {
                try
                {

                    $cat_name=$_POST['cat_name'];
                    $cat_description=$_POST['cat_description'];


                    if (isset($_FILES['cat_image']))
                    {
                        $filename = $cat->upload_photo($_FILES['cat_image']);
                    }
                    $cat->InsertCategory($cat_name,$cat_description,$filename);
                    Request::current()->redirect('product/listcategory');
                }


                catch (ORM_Validation_Exception $ex)
                {
                    $errors = $ex->errors('models');
                }
            }
        }

        $view = new View('product/addcategory');
        $view->set("categories",$cat);
        $view->set('errors',$errors);
        $this->template->content=$view;

    }

}

class Model_Category extends ORM
{
public function rules()
    {
        return array(
                'cat_name' => array(
                        array('not_empty'),
                        array('min_length', array(':value', 4)),
                        array('max_length', array(':value', 32)),

                ),


                'cat_description' => array(
                        array('not_empty'),
                        array('min_length', array(':value', 10)),
                ),
        );
    }

}

//Custom error messages page : messages/models/category.php

return array(
        'cat_name' => array(
                'not_empty' => 'You must provide a category name.',
                'min_length' => 'The category name must be at least :param2 characters long.',
                'max_length' => 'The category name must be less than :param2 characters long.',
                'unique_categoryname'=> 'Category Name already exists, please change',

        ),
        'cat_description' => array(
                'not_empty' => 'You must provide category description .',
                'min_length' => 'The category description must be at least :param2 characters long.',
        ),
);

1 个答案:

答案 0 :(得分:1)

您需要将$validation传递给ORM保存/更新方法 假设InsertCategory是保存的包装器,您应该将其传递到那里:

$cat->InsertCategory($cat_name, $cat_description, $filename, $validation);

然后在InsertCategor y方法:

public function InsertCategory(....)
{
   // .....
   $this->save($validation);
}

请注意,Kohana 3.2中存在错误,外部验证消息会转到 _external.php 文件。 您可以在此处找到更多信息:http://kohanaframework.org/3.2/guide/orm/validation#external-validation