duanbo2048 2011-04-18 06:45
浏览 33

Zend Elements和setRequired,setAllowEmpty,NotEmpty

I am validating a data array against a Zend Form where some elements are required, i.e. setRequired(true), and others are optional, i.e. setRequired(false).

When the data array contains keys matching the optional elements, I need the validation to run on this data. When there are no such keys, the validation should NOT run.

This sounds pretty simple but it has got me stumped. On the optional elements I used setAllowEmpty(false), thinking that Zend Validate would force validation when there was a matching key in the array, and ignore it otherwise. What actually happens is that Zend Validate shows an error even if the element does not exist in the array.

Any ideas?

UPDATE:

I have run some tests with all possible combinations of the element settings, and a range of different $data[element] types. Log file is below.

I'm not sure that the results square up with the documentation, and I suspect also that the usage of setAutoInsertNotEmptyValidator() affects the way that a simple Alnum validator operates. (See tests 3, 4, 13 and 14 below).

Does someone want to check my code? I'm happy to send it on.

2011-04-19T13:06:59+10:00 DEBUG (7): ALLOWED: org/testform (kim@kimprince.com, admin) 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): Element Settings: 2011-04-19T13:06:59+10:00 DEBUG (7): ================= 2011-04-19T13:06:59+10:00 DEBUG (7): 1: setRequired() [default: false] 2011-04-19T13:06:59+10:00 DEBUG (7): 2: setAllowEmpty() [default: true] 2011-04-19T13:06:59+10:00 DEBUG (7): 3: setAutoInsertNotEmptyValidator() [default: true] 2011-04-19T13:06:59+10:00 DEBUG (7): 4: add Alnum Validator 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): Data Conditions: 2011-04-19T13:06:59+10:00 DEBUG (7): ================ 2011-04-19T13:06:59+10:00 DEBUG (7): A: data[element] = Foo 2011-04-19T13:06:59+10:00 DEBUG (7): B: data[element] is BLANK 2011-04-19T13:06:59+10:00 DEBUG (7): C: data[element] does NOT EXIST 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): # 1===2===3===4=====A=======B=======C===== 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 1 T | T | T | T : pass FAIL FAIL
2011-04-19T13:06:59+10:00 DEBUG (7): Error B: Value is required and can't be empty 2011-04-19T13:06:59+10:00 DEBUG (7): Error C: Value is required and can't be empty 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 2 T | T | T | F : pass FAIL FAIL
2011-04-19T13:06:59+10:00 DEBUG (7): Error B: Value is required and can't be empty 2011-04-19T13:06:59+10:00 DEBUG (7): Error C: Value is required and can't be empty 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 3 T | T | F | T : pass FAIL FAIL
2011-04-19T13:06:59+10:00 DEBUG (7): Error B: '' is an empty string 2011-04-19T13:06:59+10:00 DEBUG (7): Error C: Invalid type given. String, integer or float expected 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 4 T | T | F | F : pass pass pass
2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 5 T | F | T | T : pass FAIL FAIL
2011-04-19T13:06:59+10:00 DEBUG (7): Error B: Value is required and can't be empty 2011-04-19T13:06:59+10:00 DEBUG (7): Error C: Value is required and can't be empty 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 6 T | F | T | F : pass FAIL FAIL
2011-04-19T13:06:59+10:00 DEBUG (7): Error B: Value is required and can't be empty 2011-04-19T13:06:59+10:00 DEBUG (7): Error C: Value is required and can't be empty 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 7 T | F | F | T : pass FAIL FAIL
2011-04-19T13:06:59+10:00 DEBUG (7): Error B: '' is an empty string 2011-04-19T13:06:59+10:00 DEBUG (7): Error C: Invalid type given. String, integer or float expected 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 8 T | F | F | F : pass pass pass
2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 9 F | T | T | T : pass pass pass
2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 10 F | T | T | F : pass pass pass
2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 11 F | T | F | T : pass pass pass
2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 12 F | T | F | F : pass pass pass
2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 13 F | F | T | T : pass FAIL FAIL
2011-04-19T13:06:59+10:00 DEBUG (7): Error B: '' is an empty string 2011-04-19T13:06:59+10:00 DEBUG (7): Error C: Invalid type given. String, integer or float expected 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 14 F | F | T | F : pass pass pass
2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 15 F | F | F | T : pass FAIL FAIL
2011-04-19T13:06:59+10:00 DEBUG (7): Error B: '' is an empty string 2011-04-19T13:06:59+10:00 DEBUG (7): Error C: Invalid type given. String, integer or float expected 2011-04-19T13:06:59+10:00 DEBUG (7):
2011-04-19T13:06:59+10:00 DEBUG (7): 16 F | F | F | F : pass pass pass

  • 写回答

2条回答 默认 最新

  • doudiza9154 2011-04-18 07:06
    关注

    setRequired() seems to be a redundant feature because setAllowEmpty is pretty the same. I think this is discuessed somewhere as an issue.

    However in addition to validators you can use setRequired(flag) to specify that an element is required. By default this flag is false. In combination with setAllowEmpty(flag), which is true by default, and setAutoInsertNotEmptyValidator(flag), which is true by default, you can modify the behavior of your validation process:

    • by default, using the default flags, when you pass no value or an empty string, validators are skipped and true is returned
    • leaving all flags by default and using setAllowEmpty(false) will validate against the validator for this element
    • leaving all flags by default and using setRequired(true) will add a NotEmpty validator. So if no value is passed, we notify the user and prevent other validators from being executed, because we know this is invalid data

    So setAllowEmpty(false) leaving all flags by default will be the same as setRequired(true) and setAutoInsertNotEmptyValidator(false); this is confusing because of the naming

    Hope that helps ;)

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图