如何验证关联模型
我的应用程序中有 Product
和 Category
模型。一种产品只能属于一个类别。这些显示在“新产品”表单的下拉列表中。
我想做的是,当我创建新产品时,应用程序也会验证类别。如何使用关联的模型验证来做到这一点?
提前致谢。
狮子座
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将在每个模型(产品和类别)中定义适用于该模型的验证规则。
You would define the validation rules in each Model (Product and Category) that applies to that model.
您应该这样在您的产品中放置一个验证变量:
You should place a validate variable in your Product this way: