0

我有一个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.',
        ),
);
4

1 回答 1

1

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

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

然后在InsertCategory 方法中:

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

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

于 2012-10-16T10:51:57.567 回答