ZF2 form element description - zend-form

in ZF1 form and form elements had a setDescription method that was outputted as <p>description here</p> in view ....
ZF2 seems that doesn't have this method
so my question is how can i add description to form elements ?
this is my view :
<div>
<?
$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form);
foreach ($form->getElements() as $el) {
?>
<div class="form_element">
<?=$this->formRow($el);?>
</div>
<?
}
echo $this->partial('system/form/buttons_form_part.phtml', array('form' => $form));
echo $this->form()->closeTag();
?>
</div>

Using ZF 2.1.5, one solution might be setOptions().
In the form definiton:
$file = new Element\File('file');
$file->setLabel('Photo (.jpg, .gif, .png)');
$file->setOptions(array('description' => 'test description'));
…
When rendering the form element:
$element = $form->get(…);
var_dump($element->getOptions());
Will give you access to:
array(1) { ["description"]=> string(16) "test description" }

If you mean a label for an Element you can use the setLabel method when you create the form (in Controller).
$name = new Element('name');
$name->setLabel('Your name');
Or if you use an array to create your form elements use this:
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'type' => 'text'
),
)
),
Here is a link:
enter link description here

Related

Yii framework: how to get id from DB, when choosing related record from DB

I have text edit box with Autocomplete::widget on the web page.
Autocomplete populates from DB. When I start to type and choose Autocomplete item (which is the First Name from DB, in my case) I need to get the associated id from DB. How could this get implemented?
<?php
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Rating;
use app\models\Teacher;
use yii\jui\AutoComplete;
use yii\web\JsExpression;
/* #var $this yii\web\View */
/* #var $model app\models\Rating */
/* #var $form ActiveForm */
$data = Teacher::find()
->select(['lname as value', 'lname as label','id as id'])
->asArray()
->all();
//print_r($data[0].id);//exit;
?>
<div class="rating-input">
ФИО
<?php
echo AutoComplete::widget([
'name' => 'Teacher',
'id' =>'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'1',
],
]);
https://paste.ee/p/BtQav
try this
<input type="hidden" name="teacher_id" id="teacher_id" value="">
<?php
echo AutoComplete::widget([
'name' => 'Teacher',
'id' =>'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'1',
'select' => new \yii\web\JsExpression("function( event, ui ) {
// $('#teacher_id').val(ui.item.id);//#teacher_id is the id of hiddenInput.
console.log(ui.item.id);
}")
],
]);
You have more information in documentation page : http://www.yiiframework.com/wiki/830/simple-jui-autocomplete-in-yii2/

cakephp fat model still undefined variable in the view action

I have created a method in my model that successfully displays the correct data when I pr($variable) to the screen. However without the print I am receiving the 'Notice (8): Undefined variable' message.
The model in question 'industry' has many 'news and events'.
What I'm trying to do in the view for a specific industry is display news that is of two different conditions. One type of news is news.type = highlight(displaying just one highlight story), and the second is a list of active stories.
This is the method in my model.
// industry highlight news
public function getHighlightNews() {
$newsHeadline = $this->News->find('first', array(
'conditions' => array('News.type' => 'highlight','News.active' => 'yes'),
'limit' => 1,
'recursive' => 0,
'fields' => array(
'News.slug',
'News.title',
'News.date',
'News.imgPathThumb',
'News.alt_tag',
'News.id',
'News.caption',
'News.body'
),
));
return $newsHeadline;
}
public function getNewsList() {
$newslist = $this->News->find('all', array(
'conditions' => array('News.active = "Yes"','News.industry_id = 1'),
'limit' => 4,
'recursive' => 0,
'fields' => array(
'News.slug',
'News.title',
'News.date',
'News.industry_id',
'News.imgPathThumb',
'News.alt_tag',
'News.id',
'News.caption',
'News.body'
),
));
return $newslist;
}
And this is what is in my industry controller. I've created similar model methods for the news list and event lists.
public function view($id = null) {
$this->layout='default';
$this->Industry->id = $id;
if (!$this->Industry->exists()) {
throw new NotFoundException(__('Invalid industry'));
}
$eventlist = $this->Industry->getEventsList($id);
$newslist = $this->Industry->getNewsList($id);
$highlights = $this->Industry->getHighlightNews($id);
//pr($newslist); die;
$this->set('eventlist', $eventlist, 'newslist', $newslist, 'highlights', $highlights);
}
And this is my view:
<div class="first articles">
<?php foreach ($highlights as $highlight): ?>
<h1><?php echo $highlight['Industry']['title']; ?></h1>
<p><?php echo $highlight['Industry']['body']; ?>
<?php endforeach; ?>
</p>
</div><!-- /articles -->
the message I get in the view is:
Notice (8): Undefined variable: highlights [APP\View\Industries\view.ctp, line 12]
Warning (2): Invalid argument supplied for foreach() [APP\View\Industries\view.ctp, line 12]
I suspect it's something small I'm overlooking.
Thanks, Paul
Setting variables doesn't work like that. You need to change this:
$this->set('eventlist', $eventlist, 'newslist', $newslist, 'highlights', $highlights);
To this:
$this->set('eventlist', $eventlist);
$this->set('newslist', $newslist);
$this->set('highlights', $highlights);
Or this:
$this->set(compact('eventlist','newslist','highlights'));

Passing ID to CGridView

How to pass grid row id from CGridView to filter values in another CGridView opened in Dialog
My View Code for Form Grid(see screenshot),
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'document-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'class' => 'CButtonColumn',
'template' => '{edit}{DocumentDelete}',
'buttons' => array(
'DocumentDelete' => array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/delete.png',
'url'=>'Yii::app()->createUrl("baseContact/DocumentDelete", array("id"=>$data->crm_document_id))',
),
'edit' => array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/update.png',
'url'=>'Yii::app()->createUrl("baseContact/edit", array("id"=>$data->crm_document_id))',
),
),),
'crm_document_id',
'name',
'doc_type',
'delivery_method',
'content_subject',
'content_body',
'is_active',
),
)); ?>
View Code for Popup Grid,
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'ManagedocumentAttach-grid',
'dataProvider'=>DocumentAttachmentModel::model()->search(),
//'filter'=>$model,
'columns'=>array(
array(
'name'=>'',
'value'=>'CHtml::checkBox("cid[]",null,array("value"=>$data->crm_document_attachment_id,"id"=>"cid_".$data->crm_document_attachment_id))',
'type'=>'raw',
'htmlOptions'=>array('width'=>5),
//'visible'=>false,
),
'crm_document_id',
'name',
'type',
),
)); ?>
how to pass Form edited row id to Popup GridView ?
Clearly you use AJAX to get the popup so i would first would make sure the ID of the edit button is the same as the ID from the item you want to open. Then you can do the following JS (using JQUERY)
$(".edit").on("click", function() {
var id = $(this).attr("id");
$.ajax({
type:"POST",
url: "controller/action/"+id;
success: function(data) {
//open dialog box and fill it with data
}
});
You could also add the id as data so you can get it with $_POST instead of it being a variable defined by the function. If you write the JS in a php document you can use $this->createUrl, but that is just whatever you prefer.
If with this you can not solve your problem then let us see how you implemented it right now.
i am not sure ... but i have a technique to do it....
if i have to do something like this ....
i will give class by htmlOptions and after that i will get value which should be an id to open popup..
example
<?php $this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider' => $dataProvider ,
'type' => TbHtml::GRID_TYPE_BORDERED,
'template' => "{items}",
'columns' => array(
array(
'name' => 'vendor_configuration_id',
'header' => $dataProvider->model->getAttributelabel('vendor_configuration_id'),
'htmlOptions' => array('class'=>'idClass'),
),
array(
'name' => 'menu_type',
'header' => $dataProvider->model->getAttributelabel('menu_type'),
'htmlOptions' => array(),
),
?>
now jquery for it
$('.idClass').on("click",function(){
var neededId = $(this).html();
alert(neededId );
//openn popup based on this id or caal ajax to retrive data based on this
});

Yii - Modifing html generated by CListView

I'm using ClistView to display the content of a dataprovider.
ClistView is supposed to call a partial view, that will loop for each model.
I would like to display something (i.e. a tag) before the first model and something (i.e. a ) after the last model of the pagination.
Assume that I have a view (index.php):
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$localDataProvider,
'itemView'=>'_view', // refers to the partial view named '_post'
'summaryText'=>'Sono visualizzati i record da {start} a {end} su un totale di {count} libri',
'pager' => Array(
'header' => 'Vai alla pagina',
'prevPageLabel' => 'Indietro',
'nextPageLabel' => 'Avanti',
),
));
In _view.php I have just the cells of a table.
If I put before the widget the html to render the table header and just after the html to render the table footer I obtain that inside the div there is the html of the pager.
How I can shift the header and the footer directly in _view.php?
Thanks
With this class extension:
Yii::import('zii.widgets.CListView');
class PlainCListView extends CListView
{
public $preItemsTag = '';
public $postItemsTag = '';
public function renderItems()
{
echo $this->preItemsTag."\n";
$data=$this->dataProvider->getData();
if(($n=count($data))>0)
{
$owner=$this->getOwner();
$render=$owner instanceof CController ? 'renderPartial' : 'render';
$j=0;
foreach($data as $i=>$item)
{
$data=$this->viewData;
$data['index']=$i;
$data['data']=$item;
$data['widget']=$this;
$owner->$render($this->itemView,$data);
if($j++ < $n-1)
echo $this->separator;
}
}
else
$this->renderEmptyText();
echo $this->postItemsTag."\n";
}
}
I's possible to override the lines of the base version of the class
echo CHtml::openTag($this->itemsTagName,array('class'=>$this->itemsCssClass))."\n";
echo CHtml::closeTag($this->itemsTagName);
With this solution with code:
$pre_html = '<table><thead><th>Codice Account</th><th>Nome</th></thead><tbody>';
$post_html = '</tbody></table>';
$this->widget('zii.widgets.PlainCListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'preItemsTag'=>$pre_html,
'postItemsTag'=>$post_html,
'summaryText'=>'Sono visualizzati i record da {start} a {end} su un totale di {count} libri',
'pager' => Array(
'header' => 'Vai alla pagina',
'prevPageLabel' => 'Indietro',
'nextPageLabel' => 'Avanti',
),
'sortableAttributes'=>array('titolo'),
'enableSorting'=>0,
));
It's possible to get in the output what I need.
Since you are trying to generate a table, you should be trying to do it using CGridView instead of CListView.
Try setting the template for the CClistView as
'template' => "<your header tag>{items}<your footer tag>{pager}",
you might arrange the template stuff as you need.

How to add span to a label using $form->labelEx from Yii?

I wish to generate the following markup:
<label>some label here <span>(optional)</span></label>
Is there a way to do this, by using Yii labelEx or just label?
Thanks
If the attribute is required
You can set the CHtml static properties like this
CHtml::$beforeRequiredLabel = '';
CHtml::$afterRequiredLabel = '<span>(optional)</span>';
Yii will generate a tag
<label>CHtml::$beforeRequiredLabel.$label.CHtml::$afterRequiredLabel</label>
but the static properties will effect all labels generated by $form->labelEx() (#see CHtml::activeLabelEx())
if you don't need, you must set the static properties to default
If the attribute is not required
you can set the htmlOptions
$form->labelEx($model, $attribute, array('label' => 'your label'))
It's simple, make use of the Decorator pattern:
<?php
// components/CustomCHtml.php
class CustomCHtml extends CHtml
{
public static function activeLabelEx($model,$attribute,$htmlOptions=array())
{
if (isset($htmlOptions['optional']) && $htmlOptions['optional']) {
if (isset($htmlOptions['label']) && $htmlOptions['label']) {
$htmlOptions['label'] .= $htmlOptions['optional'];
} else {
$htmlOptions['label'] = $htmlOptions['optional'];
}
}
return parent::activeLabelEx($model,$attribute,$htmlOptions);
}
}
use case:
<?php echo CustomCHtml::activeLabelEx($model, 'field', array('optional'=>' <span>(optional)</span>')); ?>
Here is a little hack to make it possible.
<?php
$username = $form->field($model, 'username')
->input('text', ['class' => 'form-control input-lg'])
->label(true, 'username', ['class' => 'fa fa-user signin-form-icon']);
$username->parts['{label}'] = '<span class="fa fa-user signin-form-icon">dwe</span>';
echo $username;
?>
You could use CHtml::$afterRequiredLabel and/or CHtml::$beforeRequiredLabel
$initValue = CHtml::$afterRequiredLabel;
CHtml::$afterRequiredLabel = $initValue.'<span>(optional)</span>';
echo $form->labelEx($model, $fname, array('class' => ''));
echo $form->textField($model, $fname, array('class' => ''));
CHtml::$afterRequiredLabel = $initValue;
This way the label will be applied only on the specified form field. You actually set the initial value of CHtml::$afterRequiredLabel and/or CHtml::$beforeRequiredLabel on a temp variable, you overwrite the value that you need, and then reset it to original value.