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
Try using
This only validates keys, that are present in the $partialData variable. That might do just the thing you need.
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 isfalse
. In combination withsetAllowEmpty(flag)
, which istrue
by default, andsetAutoInsertNotEmptyValidator(flag)
, which istrue
by default, you can modify the behavior of your validation process:setAllowEmpty(false)
will validate against the validator for this elementsetRequired(true)
will add aNotEmpty
validator. So if no value is passed, we notify the user and prevent other validators from being executed, because we know this is invalid dataSo 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 ;)