using zend form decorators

For programming and general questions on Zend Framework
amar9111
Posts: 18
Joined: Tue Apr 30, 2013 9:48 am

Re: using zend form decorators

Post by amar9111 » Fri Jun 14, 2013 12:06 am

The best thing is if you use the view scripts

amar9111
Posts: 18
Joined: Tue Apr 30, 2013 9:48 am

Re: using zend form decorators

Post by amar9111 » Fri Jun 14, 2013 4:13 pm

create you form like this
class Form_Whyus extends Zend_Form
{
public function init() {

$this->addElement('text', 'your_field', array (
'Label' => 'Labels',
'Required' => false,
'Size' => 40,
'Tabindex' => 1,
'Filters' => array ('StringTrim', 'StripTags', 'StringToLower'),
'Validators' => array (
new Zend_Validate_StringLength(array ('min' => 1, 'max' => 10)),
),
));

$checkbox = new Zend_Form_Element_Checkbox('Why_Active');
$checkbox->setLabel('Active');
$checkbox->setCheckedValue(1);
$checkbox->setUncheckedValue(0);
$this->addElement($checkbox);

$order = new Zend_Form_Element_Select('Why_Sort_Order', array(
"label" => "Please Select Sort Order",
'Tabindex' => 20,
"required" => true,
));
$order->addMultiOptions(array(
0 => "Sort Order" ,
));
$order->addMultiOptions(array(
'1'=> "1" ,
'2'=>"2" ,
'3'=>"3" ,
));
$this->addElement($order);
$this->addElement('submit', 'submit', array(
'required' => false,
'label' => 'submit',
'tabindex'=> 3,
'value' => 'submit',

));
$this->addElement('submit', 'cancel', array(
'required' => false,
'label' => 'Cancel',
'tabindex'=> 4,
'value' => 'cancel',

));

}

}
Then in Controller

public function indexAction()
{

$form = $this->_getWhyusForm();

$this->view->form = $form;


}
protected function _getWhyusForm()
{

$form = new Form_Whyus(array (
'method' => 'post',
'action' => $this->view->url(array (
'controller' => 'whyus',
'action' => 'index',
)),
'id' => 'whyusForm',
'name' => 'Whyus',
'class' => 'Form',
));

$form->setElementDecorators(
array(
'ViewHelper',
'Errors',

));

$form->setDecorators(array(
'Errors',

array('ViewScript', array(
// the view template script in views script folders
'viewScript' => '/whyus/WhyusTemplate.phtml',
// the module that contains our view templatesS
'viewModule' => 'default'
)),

));

return $form;

}

Then in your views /whyus/WhyusTemplate.phtml

<form action="<?php print $this->escape($this->element->getAction()); ?>"
method="<?php print $this->escape($this->element->getMethod()); ?>"
name="<?php print $this->escape($this->element->getName()); ?>"
id="<?php print $this->escape($this->element->getId()); ?>"
class="<?php print $this->escape($this->element->getAttrib('class')); ?>">
<div>
<fieldset>
<legend><?php print $this->translate('New Why Us:'); ?></legend>
<div class="control-group">
<label class="control-label" for="Why_Text">
<?php print $this->element->Why_Text->getLabel(); ?>
</label>
<div class="controls">
<?php print $this->element->Why_Text; ?>
</div>
</div>

<div class="control-group">
<label class="control-label" for="Why_Sort_Order">
<?php print $this->element->Why_Sort_Order->getLabel() ?>
</label>
<div class="controls">
<?php print $this->element->Why_Sort_Order ?>
</div>
</div>
<div class="control-group">
<label class="control-label" for="Why_Active">
<?php print $this->element->Why_Active->getLabel() ?>
</label>
<div class="controls">
<?php print $this->element->Why_Active ?>
</div>
</div>
<div class="form-actions">
<?php print $this->element->submit; ?>
<?php print $this->element->cancel; ?>
</div>
</fieldset>
</div>
</form>
then in your view script eg index.phtml

<?php echo $this->form ?>

this is best approach if you want to add the decorators the way you like
i hope it help u
Amar lehal



gpradeepjain wrote:

Code: Select all

<div class="field50Pct">
 <div class="fieldItemLabel">
  <label for='First Name'>First Name:</label>
 </div>
 <div class="fieldItemValue">
  <input type="text" id="firstname" name="firstname" value="" />
 </div>
</div>

<div class="clear"></div>
I want the code to appear like this in source code . how do i write the same thing in zend using decorators ? $firstname = new

Code: Select all

Zend_Form_Element_Text('FirstName');
        $firstname->setLabel('FirstName')
                 ->setRequired(true)
                 ->addFilter('StripTags')
                 ->addFilter('StringTrim')
                 ->addErrorMessage('Error in First Name')
                 ->addValidator('NotEmpty');

Post Reply