I'm using Zend Framework 2 Form to render my forms, and Bootstrap 3 to style them.
I want to use the Horizontal Form Layout as explained here:
Bootstrap 3 Forms tutorial
To do so, I need to add a div with appropriate classes around the input element; is there any easy way to add such div while adding the new element to the Form class? My element so far:
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'id' => 'contactName',
'maxlength' => '70',
'placeholder' => 'Your full name.',
'class' => 'form-control'
),
'options' => array(
'label' => 'Name: ',
'label_attributes' => array(
'class' => 'control-label col-xs-12 col-sm-2'
)
),
));
Can I add anything here to easily add <div class="col-xs-12 col-sm-10"></div> around the input element when I render it with <?php echo $this->formRow($this->form->get('name')); ?>?
I finally managed to solve the problem by creating a custom View Helper:
public function __invoke($element) {
echo '<div class="form-group">';
echo $this->view->formLabel($element);
echo '<div class="col-xs-12 col-sm-10">' . $this->view->formElement($element) . '</div>';
echo $this->view->formElementErrors($element);
echo '</div>';
}
Though I still wonder if there was another easier way to do that!
Related
I need following output:
<ul>
<li class="active">Value 1</li>
<li>Value 2</li>
<li>Value 3</li>
<li>Value 4</li>
<li>Value 5</li>
...
</ul>
where Value is a data from a data base (DB) (value of some field for example). Is it possible to get recodrs from DB already inserted to some predefined string?
I think you are needing this.
Use ArrayHelper to create an array to store all the data from certain model.
<?php
use yii\helpers\ArrayHelper;
$dataFromDBTable = ArrayHelper::map(ModelName::find()->all(), 'id', 'name');
?>
<ul>
<?php foreach ($dataFromDBTable as $id => $name) { ?>
<li><?=$name?></li>
<?php } ?>
</ul>
Better use Html to create list. Example:
<?=yii\helpers\Html::u($items, $options); ?>
I used ListView with 'tag' => false:
<?=
ListView::widget([
'dataProvider' => $dataProvider,
'options' => ['tag' => false],
'itemOptions' => ['tag' => false],
'layout' => "\n{items}",
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('_specialization_li',['model' => $model, 'index' => $index]);
},
]);
?>
Inside _specialization_li I wrote:
<li><?= $model->name_ ?></li>
Also there is a bugfix for 'tag'=>false on github:
https://github.com/pkirill99/yii2/commit/528b97183591376da80ee168a2a1132ea36f33cf
I want to call function when submit form in yii. In my form I enabled validateOnSubmit.
when in call function form Onsubmit mean it will call function in twice.
My Coding,
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'question-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
),
'htmlOptions' => array('onsubmit' => 'return checkmultiple()',),
));
?>
<div class="form-group">
<?php echo $form->labelEx($model, 'question_title'); ?>
<?php echo $form->textField($model, 'question_title', array('size' => 50, 'maxlength' => 250, 'class' => 'form-control')); ?>
<?php echo $form->error($model, 'question_title'); ?>
</div>
<?php if ($tileAssigned == Yii::app()->const->FLAG_ZERO) { ?>
<div class="form-group">
<?php echo $form->labelEx($model, 'type', array('label' => 'Is Multiple Choice')); ?>
<?php echo $form->radioButtonList($model, 'type', $yesnoList, array('separator' => '', 'onchange' => 'questionTypeChange(this.value);', 'class' => '')); ?>
<?php echo $form->error($model, 'type'); ?>
</div>
I am calling function checkmultiple mean it will call twice.
function checkmultiple()
{
}
i want to submit button disable when form submitted sucessfully, because user click submit button so many times when page loading so its saved the same records so many times.so i want to decided to when form submit sucessfully mean disable that submit button.i am also using aftervalidate function but it can't correct form submit successfull.
My Coding:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'question-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
'afterValidate' => 'js:buttondisable',
),
));
?>
<div class="form-group">
<?php echo $form->labelEx($model, 'question_title'); ?>
<?php echo $form->textField($model, 'question_title', array('size' => 50, 'maxlength' => 250, 'class' => 'form-control')); ?>
<?php echo $form->error($model, 'question_title'); ?>
</div>
<?php if ($tileAssigned == Yii::app()->const->FLAG_ZERO) { ?>
<div class="form-group">
<?php echo $form->labelEx($model, 'type', array('label' => 'Is Multiple Choice')); ?>
<?php echo $form->radioButtonList($model, 'type', $yesnoList, array('separator' => '', 'onchange' => 'questionTypeChange(this.value);', 'class' => '')); ?>
<?php echo $form->error($model, 'type'); ?>
</div>
But After validation function calling when validation done in every time,but i want to form submit sucessfully at this time disable button.
Currently when i click on sort by in CListView generated table, it adds MODEL_NAME_2 to the url
?page=1&sort=MODEL_NAME_2.cat.desc
is there a way to remove it? or maybe rename it to something else? Or is this just how Yii works?
here is my actionIndex() in my controller
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('MODEL_NAME_1', array(
'id'=> '',
'criteria' => array(
'with' => 'MODEL_NAME_2',
'together'=>true, //specifies whether the query should be run using a join
),
'sort'=>array(
'defaultOrder'=>'MODEL_NAME_2.cat ASC',
'attributes'=>array(
'MODEL_NAME_2.cat'=>array(
'asc'=>'MODEL_NAME_2.cat',
'desc'=>'MODEL_NAME_2.cat DESC',
),
'*',
)
),
'pagination'=>array(
'pageVar'=>'page'
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
in my view
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view', // refers to the partial view named '_view'
'enableHistory'=> true,
'sortableAttributes' => array('MODEL_NAME_2.cat'=>'Make'),
'pagerCssClass' => 'pagination',
'loadingCssClass' => '', //remove loading icon
'template'=>'
<div class="row">
<div class="col-md-12">
<div class="pull-left">{summary}</div>
<div class="pull-right">{sorter}</div>
</div>
</div>
<div class="row">
<div class="col-md-12">{items}</div>
</div>',
));
ideally i would like my url to look like this
mysite.com/bla/page/2/sort/cat/desc
basically using alot of / and no ? or &
In ListView definition change:
'sortableAttributes' => array('make'=>'Make'),
In your dataProvider
'sort'=>array(
'defaultOrder'=>'MODEL_NAME_2.cat ASC',
'attributes'=>array(
'make'=>array(
'asc'=>'MODEL_NAME_2.cat',
'desc'=>'MODEL_NAME_2.cat DESC',
),
'*',
)
),
Im trying to close CJuiDialog in Ajax success but its not closing, only iframe contents gets cleared popup not closing,
CJuiDialog Coding:
<?php
$this->beginWidget('zii.Widgets.jui.CJuiDialog',array(
'id'=>'Document-Edit',
'options'=>array(
'title'=>'Manage Documents',
'autoOpen'=>false,
'modal'=>true,
'width'=>800,
'height'=>600,
// 'close'=>'js:function(e,o){location.reload();}' ,
),
));
?>
<iframe id="cru-frame-doc" width="100%" height="100%" frameBorder="0" scrolling="no" >
<?php $this->renderPartial('ManageDocuments_Update', array('model'=>$model)); ?>
</iframe>
<?php $this->endWidget();?>
CGridView Code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'document-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'class' => 'CButtonColumn',
'template' => '{DocumentEdit}',
'buttons' => array(
'DocumentEdit' => array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/update.png',
'url'=>'$this->grid->controller->createUrl("DocumentEdit", array("id"=>$data->crm_document_id,"asDialog"=>1))',
'click'=>'function(){
$("#cru-frame-doc").attr("src",$(this).attr("href")); $("#Document-Edit").dialog("open"); return false;}',
),
),),
'crm_document_id',
'name',
'doc_type',
'delivery_method',
'content_subject',
'content_body',
'is_active',
),
)); ?>
Ajax Submit Button Code:
<?php
echo CHtml::ajaxSubmitButton( 'Save',
'js:document.location.href='.'"'.Yii::app()->createUrl("baseContact/SaveManageDocuments_Update",array("id" => $model->crm_document_id)).'"',
array(
'type'=>'POST',
"dataType" => "json",
'success' => 'function(data){
if(data.result=="success"){
window.parent.$("#Document-Edit").dialog("close");
window.parent.$("#cru-frame-doc").attr("src","");
}
}',
));
?>
Can anyone point out where im making error...
Use the below code in SaveManageDocuments_Update Controller action to close the dialog,
echo CHtml::script("window.parent.$('#Document-Edit').dialog('close');window.parent.$('#cru-frame-doc').attr('src',''); window.parent.$.fn.yiiGridView.update('document-grid');");
Yii::app()->end();