Issues

ZF-7660: Zend_Dojo does not set required="false" if explicitly set

Description

There is a minor problem with the decorator in Zend_Dojo_Form_Decorator_DijitElement:

Some Dijit form elements - like the number spinner - need an explicit "required=false" in order to accept blank values. This might be necessary if an input is optional and the user enters the input box but decides to exit it without entering a value. Therefore, the value is still empty but e.g. the number spinner validates and rejects the blank value.

If one sets "required=false" this problem does not exist.

The abovementioned decorator already sets required to true if explicitly set, but does not do so for false. Therefore, we need to change this behaviour in order to get the desired behaviour.

Careful: The decorator needs to set the value "false" as a String, not a boolean one. If set to boolean, it will be evaluated to 0 or "" (empty string) which will not be recognized by dojo.

I will attach my modified file. Please let me know whether this bug will be addressed in one of the next patches.

Comments

The numberspinner issue is a Dojo issue see http://trac.dojotoolkit.org/ticket/9643. Will be fixed in 1.4

Resolved by patching Dijit view helper; "required" attribute is always cast to a string now. Patch applied to trunk and 1.9 release branch.

Reopened as per request (irc)

I am still experiencing this on 1.9.6 with a Dojo Subform and a Filtering Select. I pass 'required' => false, in the initialization array for the form field, but it is still being required.

Isn't this just a matter of an incorrectly set fix version?

The fix attached to the bug works, while the fix that was committed does not.

Confirmed that the required="false" attribute will never be set (when set from the Element), since it does not set $attribs["required"] when $_required = false. This is because the parameter 'required' given to the Element calls 'setRequired()' rather than set the value in the $_attributes. Which is what happens when you give the exact same parameter from a Dojo ViewHelper like Zend_Dojo_View_Helper_NumberSpinner.

This would normally not be a problem if Dojo, just like ZF, assumes that an element is not required when the required attribute is not set. Unfortunately, like stated, this is not the case: FilteredSelect, NumberSpinner, TimeTextBox and maybe others assume that the element is required unless specifically told to. Might get fixed in Dojo 1.4 though?

So, imho, the attached patch is required to always set the required attribute on dojo elements. This might however, change the behavior of some elements (for the better; same required attribute for Zend_Form_Element and its Dojo counterpart, eliminating dependencies on Dojo's defaults), so not sure if it's possible to patch into 1.9 release. Maybe in time for 1.10??

Found a workaround: instead of passing required => false in the initialization (which calls setOptions, which in turn calls setRequired()), use the Element::setAttrib() function, like so

  • {{$dojoElement->setAttrib('required', false);}}
  • or: {{$dojoElement->setDijitParam('required', false);}}
  • or even: {{$dojoElement->setAttrib('dijitParams', array('required' => false));}}

Fixed on trunk and 1.10 release branch.