Add extra element using zend form - php

Here is my code
private $elementDecorators = array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
);
public function init()
{
$username = new Zend_Form_Element_Text('username',array(
'decorators' =>$this->elementDecorators,
'label' =>'Username',
'required' =>true,
'span' =>array('class'=>'validation','id'=>'unameInfo'),
));
}
$this->addElements(array(
$username
));
$this->setDecorators(array(
'FormElements',
array('HtmlTag',
array('tag'=>'table', 'width' => '100%')
),
'Form'
));
Form created for above code is as below
<tr>
<td id="username-label"><label for="username" class="blue-color required">Username</label></td>
<td><input type="text" name="username" id="username" value="" span="Array"></td>
</tr>
I want following html
<tr>
<td id="username-label"><label for="username" class="blue-color required">Username</label></td>
<td>
<input type="text" name="username" id="username" value="" span="Array">
<span class="validation" id="userinfo"></span>
</td>
</tr>
How can i add span tag in my above zend form code?
Thank you in advance

You can use the AnyMarkup decorator.
$username = new Zend_Form_Element_Text('username',array(
'decorators' => array(
'ViewHelper',
array('AnyMarkup', array('markup' => 'your-markup-here', 'placement' => 'append')),
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
),
'label' => 'Username',
'required' => true,
'span' => array('class'=>'validation','id'=>'unameInfo'),
// actually, this last 'span' entry strikes me as odd
));
To add the decorator to an element using the short-form (as above) rather than creating an instance, you need to register the decorator's path/prefix with the element, something like:
$username->addPrefixPath('My_Decorator_', APPLICATION_PATH . '/../library/My/Decorator', Zend_Form_Element::DECORATOR);
You can add that prefix/path to all (currently defined) elements using the:
$form->addElementPrefixPath($prefix, $path)
method.

Try like below,
...
$submit = new Zend_Form_Element_Submit('submit', array(
'label' => 'Submit Button',
'class' => 'form-submit',
'decorators' => array(
'ViewHelper',
),
));
//$submit->removeDecorator('Label');
$this->addElement($submit);
$reset = new Zend_Form_Element_Reset('reset', array(
'label' => 'Reset Button',
'class' => 'form-reset',
'decorators' => array(
'ViewHelper',
),
));
//$submit->removeDecorator('Label');
$this->addElement($reset);
$this->addDisplayGroup(array('submit', 'reset',), 'submitButtons', array(
'order' => 10,
'decorators' => array(
'FormElements',
array(
array('data' => 'HtmlTag'),
array('tag' => 'td','class'=>'move_td')
),
array(
array('row' => 'HtmlTag', 'class' => 'element'),
array('tag' => 'tr')
)
),
));
...
This will generate code like below,
...
<td class="move_td">
<input type="submit" class="form-submit" value="Submit Button" id="submit" name="submit">
<input type="reset" class="form-reset" value="Reset Button" id="reset" name="reset">
</td>
...
Updated
Use below code to generate span
$this->addElement(
'hidden',
'dummy',
array(
'required' => false,
'ignore' => true,
'autoInsertNotEmptyValidator' => false,
'decorators' => array(
array(
'HtmlTag', array(
'tag' => 'span',
'id' => 'span-id',
'class' => 'span-class'
)
)
)
)
);
$this->dummy->clearValidators();
Now you need to use addDisplayGroup function to group html elements.

Related

how to change the styling of label zend framework 1.12

Hi i am using zend i want to put some styling in specific label how can i
class Application_Form_CategoryEnablingItemsForm extends Zend_Form {
public $elementDecorators = array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'col-md-4 displaycls')),
array('Label', array('class' => 'col-md-3 control-label displaylbl', 'requiredSuffix' => '*')),
array('Errors', array('class' => 'zend-error'))
);
$this->addElement('select', 'product_category_parent', array(
'label' => 'Product Category Parent Name',
'required' => true,
'multiOptions' => $productCategory_options,
'filters' => array('StringTrim'),
'class' => 'form-control selection parent_category',
));
$productCategory_sub = $productCategoryMapper->getAllProductCategory();
$productCategory_sub_options = array();
$productCategory_sub_options[""]= "-Product Sub Category Name-";
if($productCategory_sub)
{
foreach ($productCategory_sub as $category)
{
$productCategory_sub_options[$category->product_category_id] = $category->product_category_name;
}
}
I added 'attribs' => array ('style' => 'margin-left: -799px; ')
but its working for whole select options not working for label,Any suggestions?
Try below code and just add your lable style class in place of my-class-name class.
$element = $this->createElement("select", 'product_category_parent');
$element->setLabel('Product Category Parent Name');
$element->setRequired(TRUE);
$element->addMultiOptions($productCategory_options);
$element->setAttrib("class", 'form-control selection parent_category');
$element->removeDecorator('HtmlTag');
$element->setDecorators(array('ViewHelper'));
$element->getDecorator('Label')->setOption('class', 'my-class-name');

ZF1: Add input type as class to tag wrapping input field

Is there a way to add the type of input-element to a class attribute on a wrapping tag?
In the example code below it could be the 'Div' decorator that already has the class of 'element' OR the LI-tag.
(I have ommitted some code)
class My_Form extends Zend_Form
{
public function loadDefaultDecorators($disableLoadDefaultDecorators = false)
//Set the decorators we need:
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description', 'escape' => false)),
array('decorator' => array('Div' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'element')),
array('Label', array('escape' => false)),
array('decorator' => array('Li' => 'HtmlTag'), 'options' => array('tag' => 'li')),
));
}
}
OR if it's possible to create My_Form_Element, and automaticly have all Zend_Form_Element_XXX extend from that.
I would like to end out with markup like this
<form>
<ul>
<li>
<label for="contactForm-contact_subject" class="optional">Regarding:</label>
<div class="element form-input-text"><input type="text" name="contactForm[contact_subject]" id="contactForm-contact_subject" value="" /></div>
</li>
<li>
<label for="contactForm-contact_message" class="required">Message:</label>
<div class="element form-textarea"><textarea name="contactForm[contact_message]" id="contactForm-contact_message" rows="24" cols="80"></textarea></div>
</li>
<li>
<div class="element form-input-submit"><input type="submit" name="contactForm[form_contact_submit]" id="contactForm-form_contact_submit" value="form_contact_submit" /></div>
</li>
</ul>
</form>
Just override render method:
class My_Form extends Zend_Form
{
public function loadDefaultDecorators($disableLoadDefaultDecorators = false)
{
//Set the decorators we need:
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description', 'escape' => false)),
array('decorator' => array('Div' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'element')),
array('Label', array('escape' => false)),
array('decorator' => array('Li' => 'HtmlTag'), 'options' => array('tag' => 'li')),
));
}
public function render(Zend_View_Interface $view = null)
{
/* #var $element Zend_Form_Element */
foreach ($this->getElements() as $element) {
$type = end(explode('_', $element->getType()));
$element->getDecorator('Div')->setOption('class',
sprintf('%s form-%s', 'element', strtolower($type)));
}
return parent::render($view);
}
}

zend using Decoration move serverside validation message next to textbox

In zend,
Using decorator how bring the validation message next to the text box.
My current decoration code
$elementDecoration = array(
'ViewHelper',
'Description',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td','width'=>'75%', 'class' => 'txt-td-field')),
array('Label', array('tag' => 'td','width'=>'50%', 'class' => 'txt-td-label', 'placement' => 'prepend')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr','valign'=>'top'),'width'=>'102%'),
);
$fname = $this->createElement('text', 'first_name');
$fname ->setRequired(true)
->setAttrib('class','my_class')
->addValidator('NotEmpty', true, array('Name is required'));
$fname->setDecorators(array('ViewHelper','Errors'));
This will do the job.
try it with custom decorator:
class My_Form_Decorator_TdError extends Zend_Form_Decorator_Errors
{
public function render($content)
{
$errors = parent::render('');
return $content . "<td>" . $errors . "</td>";
}
}
and then set decorators as following:
$elementDecoration = array(
'ViewHelper',
'Description',
array(array('data' => 'HtmlTag'), array('tag' => 'td','width'=>'75%', 'class' => 'txt-td-field')),
array('Label', array('tag' => 'td','width'=>'50%', 'class' => 'txt-td-label', 'placement' => 'prepend')),
'TdError',enter code here
array(array('row' => 'HtmlTag'), array('tag' => 'tr','valign'=>'top'),'width'=>'102%'),
);
to set path to custom decorators call in form constructor
$this->addPrefixPath('My_Form_Decorator', 'My/Form/Decorator', 'decorator');

How to add an brother element in zend framework Using Zend_Form_Element

For example:
$assignment_type = $this->createMyElement('text', 'assignment_type', array(
'name' => 'assignment_type',
'id' => 'assignment_type_label'
))->setAttrib('maxlength', '100')->addDecorator('Htmltag', array('tag' => 'div', 'class' => 'input_text'));
Here I am creating an input wraped by div tag, but how can I add other element inside this div?
So I want to see something like this:
<div>
<input />
<img /> <!--the brother element created -->
</div>
Is that possible? or what hacks I need to use?
Here's how I did it to wrap three elements in one div (a date picker in this case):
$bday = new Zend_Form_Element_Select('bday');
$bday->setLabel('Birth Date: ')
->setDecorators(array(
array('ViewHelper'),
array('Label', array('tag' => 'dt')),
array('HtmlTag', //opening tag
array(
'tag' => 'div',
'openOnly' => TRUE,
'id' => 'bday',
'placement' => 'prepend'
)),
));
$bdaymonth = new Zend_Form_Element_Select('bdaymonth');
$bdaymonth->addValidator('Digits')
->setDecorators(array(
array('ViewHelper')
));
$bdayyear = new Zend_Form_Element_Select('bdayyear');
$bdayyear->addValidator('Digits')
->setDecorators(array(
array('ViewHelper'),
array('HtmlTag', //closing tag
array(
'tag' => 'div',
'closeOnly' => TRUE
)),
)); //elements truncated for brevity
I hope this gives you some ideas.

zend decorator forms create table of rows within table

I have the following code and i would like to create the form in a table of rows. The 1st for loop is one table and the the 2nd for loop will be sub tables. These subtables I will use javascript to show and hide them using a plus/minus button.
<?php
class Application_Form_LineData extends Zend_Form
{
private $lineId;
private $dataId;
private $name;
public function init()
{
/* Form Elements & Other Definitions Here ... */
}
public $elementDecoration = array(
'ViewHelper',
array(array('data' => 'HtmlTag'), array('tag' => 'td'))
);
public $elementRowDecoration = array(
'ViewHelper',
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
);
public $elementTableDecoration = array(
'ViewHelper',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
);
public function setLineId($lineId)
{
$this->lineId = $lineId;
}
public function setName($name)
{
$this->name = $name;
}
public function setDataId($dataId)
{
$this->dataId = $dataId;
}
public function startform($entries)
{
$this->setMethod('post') ->setAttrib('enctype', 'multipart/form-data');
//$this->setName('linedata');
//$this->setAction($_SERVER["REQUEST_URI"]);
//$this->setAction('line/submit');
$count = count($entries);
for($i=0;$i<$count;$i++){
$this->addElement('text', 'lineid_'.$entries[$i]['PGA_Id'], array(
'required' => true,
'value' => $entries[$i]['PGA_Id'],
'decorators' => $this->elementDecoration,
));
$this->addElement('text', 'name_'.$entries[$i]['PGA_Name'], array(
'required' => true,
'value' => $entries[$i]['PGA_Name'],
'readonly' =>true,
'decorators' => $this->elementDecoration
));
$this->addElement('checkbox', 'check'.$entries[$i]['PGA_Id'], array(
'decorators' => $this->elementDecoration,
));
$this->setElementDecorators(
array(
'ViewHelper',
'Label'
),
array(
'lineid_'.$entries[$i]['PGA_Id'],
'name_'.$entries[$i]['PGA_Name'],
'check'.$entries[$i]['PGA_Id']
)
);
// Data sets for each line
/*
$countData = count($entries[$i]['Data_Set']);
for($x=0;$x<$countData;$x++){
$this->addElement('hidden', 'dataid_'.$entries[$i]['Data_Set'][$x]['PGA_Id'], array(
'required' => true,
'decorators' => $this->elementDecoration,
'decorators' => $this->elementRowDecoration,
//'decorators' => $this->elementTableDecoration,
));
$this->addElement('text', 'name_'.$entries[$i]['Data_Set'][$x]['PGA_Name'], array(
'required' => true,
'value' => $entries[$i]['Data_Set'][$x]['PGA_Name'],
'readonly' =>true,
'decorators' => $this->elementDecoration
));
$this->addElement('hidden', 'path_'.$entries[$i]['Data_Set'][$x]['PGA_Path'], array(
'required' => true,
'value' => $entries[$i]['Data_Set'][$x]['PGA_Path'],
'readonly' =>true,
'decorators' => $this->elementDecoration
));
$this->addElement('checkbox', 'check'.$entries[$i]['Data_Set'][$x]['PGA_Id'], array(
'decorators' => $this->elementDecoration
));
}
*/
}
//show var here
//var_dump($this->lineId);
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Submit',
'decorators' => $this->elementDecoration
));
/*
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
//array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
*/
$this->setDecorators(array(
'FormElements',
array(array('data'=>'HtmlTag'),array('tag'=>'table')),
'Form'
));
//print_r($this);
//exit();
return $this;
}
//$this->setDecorators(array('FormElements', array('SimpleTable', ), 'Form'));
}

Categories