Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I some code with a different approach, but you can have a label set for the row and both month and year appear on the same row. They are also validated together as one unit with a custom validation class, I'll post the part you asked here.</p> <pre><code>$expMonth = new Zend_Form_Element_Select('exp_month'); $expMonth-&gt;setLabel('Card Expiry Date:') -&gt;addMultiOptions( array('1' =&gt; '01', '2' =&gt; '02', '3' =&gt; '03', '4' =&gt; '04 shortened') -&gt;setDescription('/'); $this-&gt;addElement($expMonth); // Generate the Expiry Year options $expYearOptions = array(); $thisYear = date('Y'); for ($i = 0; $i &lt; 15; ++$i) { $val = $thisYear + $i; $expYearOptions[$val] = $val; } // The Expiry Year field $expYear = new Zend_Form_Element_Select('exp_year'); $expYear-&gt;removeDecorator('label') -&gt;addMultiOptions($expYearOptions) -&gt;setDescription(' (Month / Year)'); $this-&gt;addElement($expYear); // Setup Expiry Month decorators $expMonth-&gt;setDecorators(array( // Show form element 'ViewHelper', // This opens the wrapping DD tag but doesn't close it, we'll close it on // the year field decorator later array(array('data' =&gt; 'HtmlTag'), array('tag' =&gt; 'dd', 'id' =&gt; 'card-expire', 'openOnly' =&gt; true)), // Using this to slip in a visual seperator "/" between both fields array('Description', array('tag' =&gt; 'span', 'class' =&gt; 'seperator')), // Show the label tag displayed for exp_month array('Label', array('tag' =&gt; 'dt')) )); // Now for the Expiry Year field decorators $expYear-&gt;setDecorators(array( 'ViewHelper', // Inserting the "(Month / Year)" line using Description array('Description', array('tag' =&gt; 'small', 'class' =&gt; 'greyout')), // "row" is normally used to wrap a whole row, label + form element. // I'm "misusing" it to close off the DD tag we opened in the month field earlier // If you are already using "row", you might choose to echo the form line by line, // where you close the dd tag manually like: echo // $this-&gt;form-&gt;getElement('exp_year').'&lt;/dd&gt;'; array(array('row' =&gt; 'HtmlTag'), array('tag' =&gt; 'dd', 'closeOnly' =&gt; true)) )); </code></pre> <p>The full code with expiry validation can be found on my blog: <a href="http://hewmc.blogspot.com/2010/08/validating-month-and-year-fields-as-one.html" rel="nofollow noreferrer">http://hewmc.blogspot.com/2010/08/validating-month-and-year-fields-as-one.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload