ZF-12020: Modify Zend_Form::isValid() to ignore flagged elements
Description
http://framework.zend.com/issues/browse/ZF-2661 http://framework.zend.com/issues/browse/ZF-6909
In both of these older tickets, Matthew Weier O'Phinney states that by setting a form element's ignore status to 'true', both Zend_Form::getValues() and Zend_Form::isValid() will ignore that element. This is indeed the case for Zend_Form::getValues(), but Zend_Form::isValid() does not effectively ignore user input from elements that are supposed to be ignored. A glance at the source code confirms that the phrase 'ignore' appears nowhere in the isValid() function of either Zend_Form or Zend_Form_Element.
Say I have a read-only 'due date' field which is flagged to be ignored -- the user might manipulate this field using a browser such as Chrome, and when the isValid() method is called on the form, the value of 'due date' is set to the user-modified value. Not exactly the behavior one might expect.
Comments
Posted by Derek Gray (dgray90) on 2012-01-24T03:43:18.000+0000
I suppose there are instances where a developer may want to ignore an element when calling getValues() but still validate that element (CSRF, as mentioned by Adam Lundrigan. (http://framework.zend.com/issues/browse/ZF-10056)) Perhaps a new property/getter/setter can be defined? Something along the lines of $element->setProtected(true)?
Or might the populate() method be patched to NOT populate fields flagged as 'ignore'? This way, one might write:
if ($this->_request->getPost()){ $this->form->populate($this->_request->getPost()); if ($this->form->isValid()){ // Do whatever suits you here } }
Posted by Derek Gray (dgray90) on 2012-01-24T03:52:34.000+0000
Oh, that's right, isValid() requires an array. The form can't just check itself after being populated, I guess. Nevermind that idea.
Posted by Derek Gray (dgray90) on 2012-01-25T22:21:20.000+0000
I don't know how to upload patch files or whatever. But I see nobody is apparently checking these issues. Perhaps because they are busy developing ZF which I can greatly appreciate. :)
Anyways, what I have done is patched Zend_Form and Zend_Form_Element as such: In Zend_Form::isValid() and Zend_Form::isValidPartial(), following lines of code have been added:
And then in Zend_Form_Element, this has been added:
If anyone knows how I can turn these into code blocks, please let me know. I hope the development team considers this addition and the functionality it will provide without conflicting with CSRF and other element that may need to be ignored while still validated.
Posted by Frank Brückner (frosch) on 2012-01-26T10:17:53.000+0000
{quote}If anyone knows how I can turn these into code blocks, please let me know.{quote} Press the question mark below the text area and you get the help for the markup.
Here is an example:
`
Posted by Derek Gray (dgray90) on 2012-01-26T16:58:05.000+0000
Thank you so much Frank. Now I will be able to provide a better look at the 'patch'.
Posted by Derek Gray (dgray90) on 2012-01-26T23:03:41.000+0000
Yeah... just checked the ZF 2.0.0 Beta 2 source and this feature is not there either.
Please, ZF team, consider this simple and unobtrusive improvement!
Posted by Frank Brückner (frosch) on 2012-01-27T07:44:43.000+0000
@Derek The work on Zend_Form 2.0 has not started yet!
Can you provide some unit tests for your patch? You must sign the CLA, otherwise your patch is not accepted.
Posted by Derek Gray (dgray90) on 2012-01-27T14:40:10.000+0000
I see. I will read about the CLA and unit testing right away. Thank you!
Posted by Derek Gray (dgray90) on 2012-01-27T16:44:26.000+0000
Okay. I will sign and submit the CLA. And honestly, Zend can have all rights to this. It's nothing. I looked at unit testing, and while intriguing, at this time I must put full focus on the application I am currently developing. I don't know when or if I could get to a unit test -- but this is an extremely simple patch.
Posted by Adam Lundrigan (adamlundrigan) on 2012-02-25T19:17:51.000+0000
Derek: Could you provide a diff against SVN trunk? I can take a look and see if it's something that we can push for ZF 1.12, and if so I don't mind writing the unit tests to go with it.
Posted by Adam Lundrigan (adamlundrigan) on 2012-02-25T19:24:22.000+0000
I believe that this will also be addressed in ZF2's Zend\Form component. The RFC (here) has this as part of it's design goals: {quote} * MUST allow partial validations ** SHOULD allow indicating which specific elements must be valid {quote}
Could you have a quick read-through that RFC to ensure that i'm correct in thinking that it takes into account the situation you're encounting?
Posted by Derek Gray (dgray90) on 2012-02-25T23:19:45.000+0000
That RFC does seem to address it. Instead of saying 'indicating which specific elements must be valid', what I have implemented is 'indicating which specific elements should not be validated/populated with user input.'
Here are this difs for 1.11.11:
Thank you for your response and attention, I greatly appreciate it. :)