2009年1月8日星期四

Re: [fw-mvc] Zend_Form: dl not output unless a display group is created

-- Nathan Garlington <garlinto@gmail.com> wrote
(on Thursday, 08 January 2009, 11:37 AM -0700):
> Thanks for the reply Matthew. It sounds like the problem is with my
> implementation of the extended class. Here's my complete form code.
>
> Also, I'm still trying to improve my implementation of the Zend_Form
> and related classes, so if you see any needless redundancy or areas of
> improvement, please let me know!
>
> /*********************** Form code ***********************/
> class Form_ViewTrailersDojo extends Zend_Dojo_Form
> {
> protected $_fowlerTrailers = null;
> protected $_puebloTrailers = null;
> protected $_trailerTypes = null;
>
> /**
> * @constructor
> */
> public function __construct(
> Zend_Db_Table_Rowset $fowlerTrailers,
> Zend_Db_Table_Rowset $puebloTrailers,
> Zend_Db_Table_Rowset $trailerTypes,
> $options = null )

First off, you're going to run into some warnings with PHP if you change
the constructor signature. You should likely cherry-pick these objects
from the $options array prior to passing the options array to the parent
constructor:

public function __construct($options = null)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
if (!is_array($options)
|| (!isset($options['fowlerTrailers'])
|| (!$options['fowlerTrailers'] instanceof Zend_Db_Table_Rowset))
|| (!isset($options['puebloTrailers'])
|| (!$options['puebloTrailers'] instanceof Zend_Db_Table_Rowset))
|| (!isset($options['trailerTypes'])
|| (!$options['trailerTypes'] instanceof Zend_Db_Table_Rowset))
) {
throw new Exception('Missing or invalid arguments');
}
$this->_fowlerTrailers = $options['fowlerTrailers'];
$this->_puebloTrailers = $options['puebloTrailers'];
$this->_trailerTypes = $options['trailerTypes'];
unset($options['fowlerTrailers']);
unset($options['puebloTrailers']);
unset($options['trailerTypes']);

return parent::__construct($options);
}

It's more verbose, but it ensures there are no potential PHP problems
due to inheritance, and also gives you a nice exception if any arguments
are missing.

> {
> $this->_fowlerTrailers = $fowlerTrailers;
> $this->_puebloTrailers = $puebloTrailers;
> $this->_trailerTypes = $trailerTypes;
>
> parent::__construct($options);
> }
>
> /**
> * 'fowlerTrailerId' text element
> *
> * Format the types array using the
> * database result passed to the constructor
> *
> */
> public function init()
> {
> /**
> * configure form output and fieldset
> */
> $this->setDecorators( array(
> 'FormElements',
> array('DijitForm', array('id' => 'myTrailerSelectForm', 'method' => 'post')),
> array('BorderContainer', array('id' => 'myBorderContainer', 'style'
> => 'width: 100%; height: 100%;', 'dijitParams' => array('gutters' =>
> 'true') ) ),
> ));

Well, the problem with the missing <dl> tags is solved -- you're
overriding the decorators with the above, and never giving it the
HtmlTag decorator... (should be between FormElements and DijitForm).

>
> //$this->setElementDecorators(array('DijitElement', 'Label'));
>
> // fowlerTrailers element
> $trailers = array('' => '');
> foreach( $this->_fowlerTrailers as $row ) {
> ( $row->is_new ) ? $isNew = 'N' : $isNew = 'U';
> $trailers[$row->trailer_id] = "$isNew (" . substr( $row->vin, -4, 4
> ) . ') ' .$row->year . ' ' . $row->mfr . ' ' .
> $row->width . 'X' . $row->length . ' ' . $row->type;
> }
> $this->addElement('FilteringSelect', 'fowlerTrailerId', array(
> 'label' => 'Fowler (' . count( $this->_fowlerTrailers ) . '):',
> 'multiOptions' => $trailers,
> 'style' => 'width: 450px;'));
>
> /**
> * 'puebloTrailerId' text element
> *
> * First, format the types array using the
> * database result passed to the constructor
> *
> * @param array $puebloTrailers
> *
> */
> $trailers = array('' => '');
> foreach( $this->_puebloTrailers as $row ) {
> ( true == $row->is_new ) ? $isNew = 'N' : $isNew = 'U';
> $trailers[$row->trailer_id] = "$isNew (" . substr( $row->vin, -4, 4
> ) . ') ' .$row->year . ' ' . $row->mfr . ' ' .
> $row->width . 'X' . $row->length . ' ' . $row->type;
> }
> $this->addElement('FilteringSelect', 'puebloTrailerId', array(
> 'label' => 'Pueblo (' . count( $this->_puebloTrailers ) . '):',
> 'multiOptions' => $trailers,
> 'style' => 'width: 450px;'));
>
> /**
> * 'type' text element
> *
> * First, format the types array using the
> * database result passed to the constructor
> *
> * @param array $trailerTypes
> *
> */
> $types = array( '' => '' );
> foreach( $this->_trailerTypes as $row ) {
> $types[$row->type] = $row->type;
> }
>
> $this->addElement('FilteringSelect', 'type', array(
> 'label' => 'Type:',
> 'multiOptions' => $types));
>
> /**
> * 'vin' text element
> */
> $this->addElement('TextBox', 'vin', array(
> 'label' => 'Last 4 of VIN:',
> 'attribs' => array('maxlength' => 4, 'style' => 'width: 60px;') ) );
>
> /**
> * 'trailer_id' text element
> */
> $this->addElement('TextBox', 'trailer_id', array(
> 'label' => 'Stock #:',
> 'attribs' => array('maxlength' => 5, 'style' => 'width: 60px;') ) );
>
> /**
> * 'submit' submit button element
> */
> $this->addElement('SubmitButton', 'submitButton', array(
> 'label' => 'View'));
> $this->submitButton->addDecorator('HtmlTag', array('tag' => 'div',
> 'class' => 'buttons', 'placement' => 'prepend', 'openOnly' => true))
> ->removeDecorator('DtDdWrapper');
>
> /**
> * 'back' submit button element
> */
> $this->addElement('Button', 'resetButton', array(
> 'label' => 'Reset',
> 'type' => 'button',
> 'onclick' =>
> "window.location.href='http://www.trtrailer.org/trailers/index/requestId/"
> . mt_rand(50000, 70000) . "/'"));
> $this->resetButton->addDecorator('HtmlTag', array('tag' => 'div',
> 'placement' => 'append', 'closeOnly' => true))
> ->removeDecorator('DtDdWrapper');
>
> $this->addDisplayGroup(array( 'fowlerTrailerId', 'puebloTrailerId',
> 'type', 'vin', 'trailer_id', 'submitButton', 'resetButton'),
> 'myElements');
> $this->myElements->addDecorator('Fieldset', array('legend' =>
> 'Trailer Selector'));
>
> }
> }

Overall, this looks fine.

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: