Zend Form Decorator - adding options to tag of an input Label - zend-form

I'm trying to achieve the following html output using Zend_Form Decorator:
<tr>
<td id="from-label" **class="labelcell"**><label for="from" class="required">From</label></td>
<td><input type="text" name="from" id="from" value="" class="text"></td>
</tr>
I'm trying to add class attribute and for example inline style attribute on the enclosing tag of the Label. In the above example I want to add class="labelcell"
The decorator statements are as follows:
$from = $this->createElement('text', 'from', array(
'validators'=> array(array('regex', false, '/^[0-9]+/i')),
'required'=> true,
'label'=> 'From'
)
);
$from->setAttrib('class', 'text');
$from->setDecorators(
array(
'ViewHelper',
'Description',
'Errors',
array(array('data'=>'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
Is there a way to achieve what I want without extending the Zend_Form_Decorator_Label to pass additional option to the enclosing tag?

$from->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array(array('data'=>'HtmlTag'), array(
'tag' => 'td'
)),
array('Label', array(
'tag' => 'td',
'class' => 'labelcell'
'tagClass' => 'YourClassNameHere' <- THIS IS WHAT WILL ADD TO LABEL WRAPPER
)),
array(array('row' => 'HtmlTag'), array(
'tag' => 'tr'
)),
));

There is an option class. You should be able to define it in your config array, too. Try this:
$from->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array(array('data'=>'HtmlTag'), array(
'tag' => 'td'
)),
array('Label', array(
'tag' => 'td',
'class' => 'labelcell'
)),
array(array('row' => 'HtmlTag'), array(
'tag' => 'tr'
)),
));

Related

Yii ESelect2 extension show preselected valued

i want to show already selected values in tags of ESelected extension but unable to find any solution my code in update form in view is
$this->widget('ext.select2.ESelect2', array(
'name' => 'cat_names[]',
'data' => Coupon::getCategories(),
'options' => array(
'placeholder' => '',
'allowClear' => true,
),
'htmlOptions' => array(
'multiple' => 'multiple',
),
));
if i add tags fields in option of Eselect it changes the behaviour and stop working i add tags in options like this
'tags' => Coupon::getCategories_old(),
Any suggestion will be helpfull thanks in advance
$this->widget('ext.select2.ESelect2', array(
'name' => 'cat_names[]',
'value'=> array(91,95),
'data' => Coupon::getCategories(),
'options' => array(
'placeholder' => '',
'allowClear' => true,
),
'htmlOptions' => array(
'multiple' => 'multiple',
),
));

Cannot do filtering via a dropdown menu in Yii TbGridView (v1.x.x)

I am trying to get the filtering to run although I am having some issues below is my view note that this in in a PARTIAL view :-
If I click on the header of the column it does the sorting as expected, however if I select an option from the dropdown it doesn't filter the items...
Any ideas?
<?php
$myuser = new Myuser('search');
$filterBtn = $this->widget('bootstrap.widgets.TbButton', array(
'icon' => 'filter',
'size' => 'small',
'label' => $myuser->paging ? 'View All' : 'View Less',
'htmlOptions' => array('class'=>'pull-right', 'style'=> 'margin:20px 0;'),
'url' => $myuser->paging ? array('/carrot/myuser/admin/paging/0') : array('/carrot/myuser/admin')
), true);
$this->widget('bootstrap.widgets.TbGridView',array(
'id' => 'myuser-grid','type'=>'striped bordered condensed',
'dataProvider' => $myuser->search(),
'filter' => $myuser,
'columns' => array(
array(
'id' => 'user_id',
'class' => 'CCheckBoxColumn',
'checked' => 'in_array($data->user_id, $this->grid->owner->model->student_ids)',
'checkBoxHtmlOptions' => array(
'name' => 'selected_student_id[]',
)
),
'firstname',
'surname',
'username',
array(
'name' => 'year_id',
'filter' => CHtml::activeDropDownList($myuser, 'year_id', CHtml::listData(Organisation::model()->distinctYears, 'year_id', 'year_id'), array('prompt'=>'All Years')),
'htmlOptions' => array('style' => 'text-align:center;'),
'headerHtmlOptions' => array('style' => 'text-align:left;'),
),
array(
'name' => 'form_name',
'header' => 'Form',
'filter' => CHtml::activeDropDownList($myuser, 'form_name', CHtml::listData(Organisation::model()->distinctForms, 'form_name', 'form_name'), array('prompt'=>'All Forms')),
),
array(
'name' => 'House',
'filter' => CHtml::activeDropDownList($myuser, 'House', CHtml::listData(Organisation::model()->distinctHouses, 'House', 'House'), array('prompt'=>'All Houses')),
),
),
)); ?>
My model has the following search() method:
public function search($limit = false)
{
$criteria = new CDbCriteria();
$criteria->compare('t.user_id',$this->user_id,true);
$criteria->compare('t.firstname',$this->firstname,true);
$criteria->compare('t.surname',$this->surname,true);
$criteria->compare('t.username',$this->username,true);
$criteria->compare('t.year_id',$this->year_id);
$criteria->compare('t.form_name',$this->form_name);
$criteria->compare('t.House',$this->House);
$criteria->compare('o.organisation_id',$this->organisation_id);
$criteria->group = 't.user_id';
$criteria->together = true;
return new CActiveDataProvider($this->currentUserOrganisation(), array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => $this->paging ? ($limit) ? $limit : OverviewController::PAGE_SIZE : 2000
),
'sort' => array(
'defaultOrder' => array('firstname'=>false, 'surname'=>false),
'attributes' => array(
'organisation_name' => array(
'asc' => 'organisation_name',
'desc' => 'organisation_name DESC',
'default' => 'desc',
),
'*'
)
),
));
}
I think you can't use a DropDownList as filter, but you can easily use CHTML::listData():
array(
'name' => 'year_id',
'filter' => CHtml::listData(Organisation::model()->distinctYears, 'year_id', 'year_id'),
'htmlOptions' => array('style' => 'text-align:center;'),
'headerHtmlOptions' => array('style' => 'text-align:left;'),
),
array(
'name' => 'form_name',
'header' => 'Form',
'filter' => CHtml::listData(Organisation::model()->distinctForms, 'form_name', 'form_name'),
),
array(
'name' => 'House',
'filter' => CHtml::listData(Organisation::model()->distinctHouses, 'House', 'House'),
),
This will automatically generate a DropDownList for you.
I hope this works for you.

Yii customize CGridView Items template

I am using Yii with bootstrap features;
So, I have changed the container tagName from div to table;
Now, the table with the items class is inside the table with table class;
The template property defines my issue;
How do I add the following class : table table-striped table-hover to the table with items class?
the tables structure is:
<table class="table">
<tr>
<td>
<table class="items">
...
{items} generates the <table class="items"> table
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'orders-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'ajaxUrl' => Yii::app()->baseUrl . '/orders/admin',
'tagName' => 'table',
'htmlOptions' => array(
'class' => 'table',
'style' => 'width:auto;',
'align' => 'left',
),
'cssFile' => Yii::app()->baseUrl . '/css/custom.css',
'template' =>
'<tr><td style="border-top:none;height:74px;">{pager}</td></tr>' .
'<tr><td style="height:34px;border-top:none;">{summary}</td></tr>' .
'<tr><td>{items}</td></tr>' .
'<tr><td style="border-top:none;">{summary}</td></tr>' .
'<tr><td style="border-top:none;height:74px;">{pager}</td></tr>',
'columns' => array(
array(
'header' => 'Order ID',
'name' => 'id',
'type' => 'raw',
'value' => 'CHtml::link(str_pad($data->id, 9, "0", STR_PAD_LEFT),array("update","id"=>$data->id))',
),
array(
'header' => 'Name',
'name' => 'var_user_full_name',
'value' => '$data->UserFullName',
'filter' => CHtml::activeTextField($model, 'var_user_full_name'),
),
array(
'name' => 'total_price',
'type' => 'raw',
'value' => 'CurrencyData::$_currency[$data->currency]." ".$data->total_price',
),
array(
'name' => 'created',
'value' => 'date("d/m/Y",$data->created)',
'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'created',
'language' => 'en-AU',
// 'i18nScriptFile' => 'jquery.ui.datepicker-en.js',
'htmlOptions' => array(
'id' => 'datepicker_for_due_date',
'size' => '10',
),
'defaultOptions' => array(
'showOn' => 'focus',
'dateFormat' => 'dd/mm/yy',
'showOtherMonths' => true,
'selectOtherMonths' => true,
'changeMonth' => true,
'changeYear' => true,
'showButtonPanel' => true,
)
), true),
),
array(
'name' => 'active',
'header' => 'Status',
'value' => 'OrdersData::$active[$data->active]',
//'filter' => OrdersData::$active,
'filter' => CHtml::activeDropDownList($model, 'active', OrdersData::$active, array('prompt' => '-Select-')),
),
array(
'class' => 'CButtonColumn',
'header' => 'Action',
'template' => '{update}',
),
),
));
?>
found it:
'itemsCssClass' => 'table table-striped table-hover',

Yii's zii.widgets.CMenu - can it be a pop-up menu?

Hi I'm using the test code from http://www.yiiframework.com/doc/api/1.1/CMenu
$this->widget('zii.widgets.CMenu', array(
'items'=>array(
// Important: you need to specify url as 'controller/action',
// not just as 'controller' even if default acion is used.
array('label'=>'Home', 'url'=>array('site/index')),
// 'Products' menu item will be selected no matter which tag parameter value is since it's not specified.
array('label'=>'Products', 'url'=>array('product/index'), 'items'=>array(
array('label'=>'New Arrivals', 'url'=>array('product/new', 'tag'=>'new')),
array('label'=>'Most Popular', 'url'=>array('product/index', 'tag'=>'popular')),
)),
array('label'=>'Login', 'url'=>array('site/login'), 'visible'=>Yii::app()->user->isGuest),
),
));
Other test code such as zii.widgets.jui.CJuiAutoComplete and zii.widgets.jui.CJuiDraggable work fine in my Yii view...
The CMenu code snippet just shows the links as a hierarchical list.
When using a div with an id of mainmenu things seem to improve a bit... then I tried using a lot of nested arrays:
http://sky-walker.net/temp/test/yii/testnews/index.php?r=site/test
It supports array nesting of 5+ levels deep...
But if I highlight/select it all (to display the white menu links) it doesn't display the hierarchy nicely...
I was wondering if it could be a popup menu that gradually shows children if the parents are hovered over?
I also tried disabling the div with id of mainmenu from the layout's main.php.
ok, after seeing what you have, follow my example:
<div id="mainmenu">
<?php
$this->widget('zii.widgets.CMenu', array(
'items' => array(
array('label' => 'linkbook', 'url' => array('/site/index')),
array('label' => 'About', 'url' => array('/site/page', 'view' => 'about')),
array('label' => 'Contact', 'url' => array('/site/contact')),
array('label' => 'Suggest Website', 'url' => array('/websiteSuggest/index'),'visible' => !Yii::app()->user->isGuest),
array('label' => 'Servers', 'url' => array('/server/index'),'visible' => !Yii::app()->user->isGuest),
array('url' => Yii::app()->getModule('user')->loginUrl, 'label' => Yii::app()->getModule('user')->t("Login"), 'visible' => Yii::app()->user->isGuest),
array('url' => Yii::app()->getModule('user')->registrationUrl, 'label' => Yii::app()->getModule('user')->t("Register"), 'visible' => Yii::app()->user->isGuest),
array('url' => Yii::app()->getModule('user')->profileUrl, 'label' => Yii::app()->getModule('user')->t("Profile"), 'visible' => !Yii::app()->user->isGuest),
array('url' => Yii::app()->getModule('user')->logoutUrl, 'label' => Yii::app()->getModule('user')->t("Logout") . ' (' . Yii::app()->user->name . ')', 'visible' => !Yii::app()->user->isGuest),
),
));
?>
</div><!-- mainmenu -->
<br/>
<div id="mainmenu">
<?php
$this->widget('zii.widgets.CMenu', array(
'items' => array(
array('label' => 'Website', 'url' => array('/website/index'), 'visible' => !Yii::app()->user->isGuest),
array('label' => 'Url', 'url' => array('/url/index'), 'visible' => !Yii::app()->user->isGuest),
array('label' => 'Pattern Url', 'url' => array('/patternUrl/index'), 'visible' => !Yii::app()->user->isGuest),
),
));
?>
</div>
I'm using the MbMenu Yii extension now...
http://www.yiiframework.com/extension/mbmenu
It is very easy to install and use and it seems the input array from CMenu can be reused with it (as well as the CssMenu Yii extension)...
It is at the bottom of:
http://sky-walker.net/temp/test/yii/testnews/index.php?r=site/test

Populating Form Data in ZF2 when using Fieldsets

I am currently playing around with ZF2 beta 4 and I seem to be stuck when i try to use fieldsets within a form and getting the data back into the form when the form is submitted. I am not sure if I am not setting the input filters right for fieldsets or I am missing something. For example, I have the following (simplified to make it clear):
Controller
public function indexAction(){
$form = new MyForm();
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->post());
if ($form->isValid()) {
//Do something
print_r($form->getData()); //for debug
}
}
return array('form' => $form);
}
MyForm.php
class MyForm extends Form
{
public function __construct()
{
parent::__construct();
$this->setName('myForm');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'title',
'attributes' => array(
'type' => 'text',
'label' => 'Title',
),
));
$this->add(new MyFieldset('myfieldset'));
//setting InputFilters here
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
)));
//Now add fieldset Input filter
foreach($this->getFieldsets() as $fieldset){
$fieldsetInputFilter = $factory->createInputFilter($fieldset->getInputFilterSpecification());
$inputFilter->add($fieldsetInputFilter,$fieldset->getName());
}
//Set InputFilter
$this->setInputFilter($inputFilter);
}
}
MyFieldset.php
class MyFieldset extends Fieldset implements InputFilterProviderInterface{
public function __construct($name)
{
parent::__construct($name);
$factory = new Factory();
$this->add($factory->createElement(array(
'name' => $name . 'foo',
'attributes' => array(
'type' => 'text',
'label' => 'Foo',
),
)));
}
public function getInputFilterSpecification(){
return array(
'foo' => array(
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
),
);
}
}
I am able to output the form as expected and I end up with two input elements named 'title' and 'myfieldsetfoo' (the name given when outputing with the ViewHelper). So of course when I submit the raw post will show values for 'title' and 'myfieldsetfoo'. However, when I use SetData() the values for the field set are not being populated (although I can see the values in the raw post object). Instead, examining the output of '$form->getData()' I receive:
Array(
[title] => Test,
[myfieldset] => Array(
[foo] =>
)
)
What am I missing? What do I need to do so that ZF2 understands how to populate the fieldset?
Thanks for any help, this is driving me crazy.
Why I do is concatenate InputFilter so I could handle the whole HTML form array posted.
<form method="POST">
<input type="text" name="main[name]" />
<input type="text" name="main[location]" />
<input type="text" name="contact[telephone]" />
<input type="submit" value="Send" />
</form>
This will create an array posted like
post["main"]["name"]
post["main"]["location"]
post["contact"]["telephone"]
Filtered and validated with:
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
$post = $this->request->getPost();
$inputFilter = new InputFilter();
$factory = new InputFactory();
// $post["main"]
$mainFilter = new InputFilter();
$mainFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
$mainFilter->add($factory->createInput(array(
'name' => 'location',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
$inputFilter->add($mainFilter, "main");
// $post["contact"]
$contactFilter = new InputFilter();
$contactFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
$contactFilter->add($mainFilter, "contact");
//Set posted data to InputFilter
$inputFilter->setData($post->toArray());
http://www.unexpectedit.com/zf2/inputfilter-validate-and-filter-a-form-data-with-fieldsets
I think you forgot to prepare your form in the controller:
return array('form' => $form->prepare());
This will rename the "name" field of your fieldset to "myfieldset[foo]", so you don't have to prepend the fieldsets name on your own.
Just use
'name' => 'foo'
instead of
'name' => $name . 'foo'
in your fieldset class.
I think the problem is that you declare a new form in your controller. And that clear the previous form.
$form = new MyForm();
I use the Service Manager to declare the form and filters.
And then in the controller I do:
$form = $this->getServiceLocator()->get('my_form');
That way I always get the object I want
Update
I no longer use service manager to call forms. I just call a new form and issue $form->setData($data);
The source for the data can also be entity though then I would issue: $form->bind($entity)