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