Depois de escrever dois posts sobre como setDecorators para diferentes elementos de formulário acho que é chegada a hora de fornecer uma maneira simples de configurar decoradores a todos os elementos de formulário de uma vez.
Dê uma olhada no código a seguir.
classe SimpleForm estende Zend_Form
{
public function __construct ($ options = null)
{
parent:: __construct ($ options);
$ countryList = array (
'Estados Unidos',
'Unidos kindom',
'Paquistão'
); $ firstName = $ this- > createElement ('texto', 'Nome'); $ firstName -> addValidator ('alnum') -> addValidator ('regex', false, array ('/^[ az ]+/')) -> addValidator (' StringLength ', array, false (6, 20)) -> setRequired (true) -> setLabel ('Nome:') -> addFilter ('StringToLower'); $ lastName = $ this-> createElement ('texto', "lastName '); $ lastName -> addValidator ('StringLength', array, false (6)) -> setRequired (true) -> setLabel ("Last Name:"); $ address1 = $ this-> createElement ('text ',' address1 '); $ address1-> setLabel ('Address1:') -> addValidator ('StringLength', false, array (3,50)) -> setValue ('') -> setRequired (true); $ address2 = $ this-> createElement ('text', 'address2'); $ address2-> setLabel ('Endereço2:') -> addValidator ('StringLength', array, false (3,50)) -> setValue (' ') -> setRequired (false); $ postalCode = $ this-> createElement ('text', 'postalCode'); $ postalCode-> setLabel ('CEP:') -> addValidator ('StringLength', array, false ( 3,15)) -> setValue ('') -> setRequired (false); $ cidade = $ this-> createElement ('text', 'cidade'); $ cidade-> setLabel ('Cidade:') -> setValue ('') -> setRequired (false) -> setAttrib ("tabindex ', '6'); estado = $ this-> createElement ('text', 'Estado'); $ Estado-> setLabel ('Estado : ') -> setAttrib ('maxlength', 2) -> setValue ('') -> setRequired (false) -> setAttrib ("tabindex ', '7'); país US $ = $ this-> createElement ('select , = $ This-> createElement ('text', 'telefone'); $ telefone-> setLabel ('Telefone:') -> setAttrib ('maxlength', '25 ') -> setValue ('') -> setRequired ( true); $ emailAddress = $ this-> createElement ('text', 'emailAddress'); $ emailAddress-> setLabel ('Email:') -> addValidator ('StringLength', série
Se você leu meu post anterior sobre decoradores, você vai entender facilmente o código. No entanto, essas são como novas para o meu blog e decoradores podem precisar de alguma explicação.
Como de costume, estamos ampliando nosso formulário de Zend_Form, definir o nosso construtor, chamar o construtor pai e então definir nossos elementos.
A linha mais importante é depois de adicionar elementos à nossa forma . O código
$this->setElementDecorators(array(
'viewHelper',
'Errors',
array(array('data'=>'HtmlTag'),array('tag'=>'td')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr'))
));
simply set decorators to all the elements to the form elements. Here we are define that close label in “td”, form elements in “td” and put both these in “tr”.
If you want to set decorators to only few elements then
$this->setElementDecorators(array(
'viewHelper',
'Errors',
array(array('data'=>'HtmlTag'),array('tag'=>'td')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr'))
),array('firstname','lastname'));
This code will set decorators to only fistname and lastname element of the form. Rest of the form elements will be render in the default decorators.
At the end we define
$this->setDecorators(array(
'FormElements',
array(array('data'=>'HtmlTag'),array('tag'=>'table')),
'Form'
));
This define the form level decorators. We are putting “table” tag after “form” tag. So all the row will be wrapped in table.