简体   繁体   English

Plone:根据其他字段的值验证字段

[英]Plone: Validate field based on other field's value

Let's say we have a dexterity content type with two fields: field_a and field_b . 假设我们有一个敏捷性内容类型,其中包含两个字段: field_afield_b

In edit mode: 在编辑模式下:

  • if field_a has the category_1 value selected then field_b is not required 如果field_a选择了category_1值,则field_b
  • if field_b has other value selected then field_b is required 如果field_b选择了其他值,则需要field_b

It seems a simple validation is not possible here. 在这里似乎无法进行简单的验证。 I tried: 我试过了:

from zope.interface import Invalid

def validate_field_b(value):
    # value is a string here. I can't create a condition based on field_a.
    raise Invalid(_(u"Value in field b is required."))

used here: 在这里使用:

field_b = schema.TextLine(
    title=_(u"Some field B"),
    required=False,
    constraint=validate_field_b
)

The result is field_b required all the time. 结果始终是field_b :) :)

Any solution here? 有什么解决办法吗? How the get the form / context / request / other fields value in my validator? 如何在验证器中获取表单/上下文/请求/其他字段的值?

The solution is to use an invariant instead of constraint : https://docs.plone.org/external/plone.app.dexterity/docs/advanced/validators.html#invariants 解决方案是使用invariant而不是constrainthttps : //docs.plone.org/external/plone.app.dexterity/docs/advanced/validators.html#invariants

@invariant
def validate_field_b(data):
    if data.field_a != 'category_1' and data.field_b is None:
        raise Invalid(_("Missing input for field_b."))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果当前内容的相同字段为空,如何从父容器中获取字段值? Plone 4 - How do I get a field value from parent containers if the current content's same field is empty? Plone 4 使用FSS获取Plone 3.x中图像字段的值 - Getting the value of an image field in Plone 3.x with FSS 我怎么知道一个字段是否更改了PL对象中的AT对象? - How can I tell if a field has changed value in an AT object in plone? 如何在 Plone 的灵巧行为中覆盖字段的默认值? - How do you override the default value of a field in a dexterity behavior in Plone? 如何在Plone 5中创建字段和对字段执行操作? - How to create a field and an action on field in Plone 5? 向Plone组添加自定义字段 - Adding a custom field to Plone groups Plone-如何让验证器从行为中验证特定内容类型的字段? - Plone - How can I make a validator validate a field from a behavior for a specific content type? 使用z3c.form和Plone隐藏和显示字段取决于其他字段值 - Hiding and showing fields depending on other field values with z3c.form and Plone 克隆4-在plone.app.blob.field.FileField中获取文件的url - Plone 4 - Get url of a file in a plone.app.blob.field.FileField Plone:processForm()删除我的原型字段 - Plone: processForm() deletes my Archetype field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM