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');
Related
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);
}
}
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');
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.
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'));
}
Maybe somebody knows. How to wrap Zend_Form_Element_Radio including Label of whole stack of radio inputs (with input labels).
public $radioDecorators = array(
'ViewHelper',
array('Description',array('tag'=>'div','class'=>'','placement' => 'prepend')),
array('Errors',array('class'=>'error_message_show','placement' => 'prepend')),
array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'element')),
array('label', array('class'=>'label_text','placement' => 'prepend')),
array(array('rows' => 'HtmlTag'), array('tag' => 'div', 'class' => 'radio')),
);
$offer_type = new Zend_Form_Element_Radio('offer_type', array(
'label' => 'Label I'd like to wrap with inputs', //Label to wrap
'required' => true,
'description' => '',
'decorators' => $this->radioDecorators,
'multioptions' => array (
'standard' => 'standard',
'premium' => 'premium',
),
));
$this->addElement($offer_type);
Above example didn's solve my as it wraps only several inputs label's.
I think I know what you're after and if so, you're in luck as I just had to do this the other day.
The standard ZF multi-option element separates each "option" using the separator property (defaults to <br /> for radio and newline for select). This is fine for <option> elements but pretty shoddy for a collection of radio buttons.
The solution is to
Add HtmlTag decorator(s) to the element, wrapping the content
Set the separator to close and re-open the HtmlTag
For example, here's a solution to wrap the collection of inputs in an unordered list
$offer_type = new Zend_Form_Element_Radio('offer_type', array(
'separator' => '</li><li>',
'decorators' => array(
'ViewHelper',
array(array('liWrapper' => 'HtmlTag'), array('tag' => 'li')),
array(array('ulWrapper' => 'HtmlTag'), array('tag' => 'ul')),
// the rest
)
));
The alternative is to write your own view helper. Creating your own version of formRadio should be pretty easy.