On Wed, May 6, 2009 at 11:11 AM, Eugene Morgan <[email protected]
> wrote:

> This is how I do it:
>
> $field->addMultiOption('', '(choose one)');
>

The problem I ran into with this approach is now the '' is considered a
valid value from the auto-added InArray validator.

The route I took, which works, but hardly elegant is:

      $valid_states = array ();
      while ( ($row = $stmnt->fetch( Zend_Db::FETCH_ASSOC )) ) {
         $valid_states[$row['COUNTRY_SUBDIV_CD']] =
$row['COUNTRY_SUBDIV_NM'];
      }

      $field = new Zend_Form_Element_Select( 'restaurant_state', array (
         'multiOptions' => array ('') + $valid_states,
         'required' => true,
         'label' => 'State',
         'registerInArrayValidator' => false,  // Added this.
         'filters' => array ( 'StringTrim',
            'StringToUpper' ) ) );

      /* Adding in InArray manually using my state keys as the hay stack */
      $field->addValidator( 'InArray', false, array (
         'haystack' => array_keys( $valid_states ),
         'messages' => array (
            Zend_Validate_InArray::NOT_IN_ARRAY => 'Invalid state selected.'
) ) );
------------------

It kind of defeats the purpose of the built-in automation you come to
appreciate. I would like a more elegant solution, but I don't think there
would be one. Adding an option to make first option empty isn't exactly
a functionality configuration option. More decorator/view related.

---
Philip
[email protected]
http://www.gpcentre.net/

Reply via email to