如何验证关联模型

发布于 2024-12-27 20:28:33 字数 175 浏览 3 评论 0原文

我的应用程序中有 ProductCategory 模型。一种产品只能属于一个类别。这些显示在“新产品”表单的下拉列表中。

我想做的是,当我创建新产品时,应用程序也会验证类别。如何使用关联的模型验证来做到这一点?

提前致谢。

狮子座

I've got Product and Category models in my application. A product can belong to only one category. These are shown within a drop down list in the "New Product" form.

What I would like to do is that when I create a new product, the app validates the category as well. How can I do that using associated model validations?

Thanks in advance.

Leo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2025-01-03 20:28:33

您将在每个模型(产品和类别)中定义适用于该模型的验证规则。

You would define the validation rules in each Model (Product and Category) that applies to that model.

墨落画卷 2025-01-03 20:28:33

您应该这样在您的产品中放置一个验证变量:

  // declares a package for a class
    App::uses('AppModel', 'Model');

class Product extends AppModel {

    // validate
    $validate = array(
        'category_id' => array(
            'rule' => array('numeric'),
            'allowEmpty' => false,
            'message' => __('You should select a category'),
        ),

    );
}

You should place a validate variable in your Product this way:

  // declares a package for a class
    App::uses('AppModel', 'Model');

class Product extends AppModel {

    // validate
    $validate = array(
        'category_id' => array(
            'rule' => array('numeric'),
            'allowEmpty' => false,
            'message' => __('You should select a category'),
        ),

    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文