I've just started with Yii (2.0) and I have a problem.
I need to make sure that the Maximum Order Quantity is always equal or greater than the Minimum Order Quantity. Which means, that you can't order a minimum of 10, and maximum of 5 for example.
Here is a screenshot.
And some code:
<?= $form->field($model, 'minimum_order_quantity')->widget(TouchSpin::classname(), [
'options' => [
'placeholder' => 'Minimum Order Quantity ...',
'class' => 'input-lg',
],
'pluginOptions' => [
'buttonup_class' => 'btn btn-primary',
'buttondown_class' => 'btn btn-info',
'buttonup_txt' => '<i class="glyphicon glyphicon-plus-sign"></i>',
'buttondown_txt' => '<i class="glyphicon glyphicon-minus-sign"></i>'
],
]) ?>
<?= $form->field($model, 'maximum_order_quantity')->widget(TouchSpin::classname(), [
'options' => [
'placeholder' => 'Maximum Order Quantity ...',
'class' => 'input-lg',
],
'pluginOptions' => [
'buttonup_class' => 'btn btn-primary',
'buttondown_class' => 'btn btn-info',
'buttonup_txt' => '<i class="glyphicon glyphicon-plus-sign"></i>',
'buttondown_txt' => '<i class="glyphicon glyphicon-minus-sign"></i>'
],
]) ?>
This would be the _form.php.
Thanks in advance!
You can do this with a rule in you Model:
[['maximum_order_quantity'], 'compare', 'compareAttribute' => 'minimum_order_quantity', 'operator' => '>='],
Related
I have a following code below:
<?= $form->field($model, 'date') ->widget(yii\jui\DatePicker::className(),['clientOptions' => [ 'placeholder' => 'dd/mm/yyyy',
'id' => 'form',
'autocomplete' => 'off',
'value' => date('m/d/Y'),
'autoclose'=>true,
]]) ?>
That will result in HTML in following way:
<div class="form-group has-icon has-label">
<label for="formSearchUpLocation">Picking Up Location</label>
<input type="text" class="form-control" id="formSearchUpLocation" placeholder="Airport or Anywhere">
<span class="form-control-icon"><i class="fa fa-map-marker"></i></span>
</div>
My question is as following:
How can I integrate the css and bootstrap classes in HTML below into yii2 active form above?
Thank you for your help.
if you are looking to add the class to all the group divs mean all the div that have the class form-group then you can use the fieldConfig option of the ActiveForm or if you want it for one specific field then you can use the options option of the $form->field() as the 3rd parameter
For the whole Form
$form = yii\widgets\ActiveForm::begin([
'fieldConfig' => [
'options' => [
'class' => 'my-group'
]
]
]);
For Single Field
echo $form->field($model, 'name', ['options' => ['class' => 'my-class']])->textInput();
Conversion
About converting your above HTML using ActiveForm the following should work you can use template option of the $form->field() 3rd parameter to add your custom icon after the input, along with other, see below will create your desired HTML
echo $form->field($model, 'date', [
'options' => [
'class' => 'form-group has-icon has-label'
],
'inputOptions' => [
'class' => 'form-control'
],
'template' => '{label}{input}<span class="form-control-icon"><i class="fa fa-map-marker"></i></span>{error}'
])->widget(yii\jui\DatePicker::class, [
'id' => 'created_at',
'options' => [
'placeholder' => 'Airport or Anywhere'
]
]);
You will have something like below
<?= $form->field($model, 'date') ->widget(yii\jui\DatePicker::className(),['clientOptions' => [ 'placeholder' => 'dd/mm/yyyy',
'id' => 'form',
'class' => 'WRITE-YOUR-CLASS'
'autocomplete' => 'off',
'value' => date('m/d/Y'),
'autoclose'=>true,
]]) ?>
just input your class name in 'class' section it will work.
example : 'class' => 'fa fa-map-marker'
You could try something like the code bellow to concat your calendar icon with the widget:
<?php
$addon = '<span class="input-group-addon">
<i class="fa fa-calendar-alt"></i>
</span>';
echo $addon.$form->field($model, 'date')->widget(yii\jui\DatePicker::className(),['clientOptions' => [ 'placeholder' => 'dd/mm/yyyy',
'id' => 'form',
'autocomplete' => 'off',
'value' => date('m/d/Y'),
'autoclose'=>true,
]]);
?>
Or you can try search more on the plugin you are using, some widgets have his own attribute to render the calendar icon.
Can you help me to add a filter on checkbox column on Grid view widget yii2? I used yii\grid\CheckboxColumn to add the check box column in the Gridview on my index.php. But I couldn't add a filter above the column as the other columns. Please look into my code below.
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
['label' => 'Client', 'attribute' => 'name', 'value' => 'client.view', 'format' => 'raw'],
'postCode',
'start',
'end',
[
'attribute' => 'categoryID',
'value' => 'category.name',
'filter' => ArrayHelper::map(Servicecategory::find()->where(['status' => true])->asArray()->all(), 'id', 'name')
],
[
'attribute' => 'status',
'headerOptions' => ['style' => 'width:12%'],
'value' => 'Status',
'filter' => array_filter(\app\models\Booking::$statuses),
'filterInputOptions' => ['class' => 'form-control', 'prompt' => 'All']
],
['class' => 'yii\grid\CheckboxColumn',
'header' => 'follow Up',
'contentOptions' => ['class' => 'text-center'],
'checkboxOptions' => function($model, $key, $index) {
$url = \yii\helpers\Url::to(['booking/followup/' . $model->id]);
return ['onclick' => 'js:followUp("' . $url . '")', 'checked' => $model->followUpEmailSent ? true : false, 'value' => $model->followUpEmailSent];
}
],
['class' => 'yii\grid\ActionColumn',
'headerOptions' => ['style' => 'width:10%'],
'template' => '{view} {approval} {update} {delete} ',
'buttons' => [
/* 'view' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', ['/booking/review/' . $model->id], [
'title' => Yii::t('app', 'Review'),
]);
}, */
'approval' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-ok"></span>', ['/booking/approval/' . $model->id], [
'title' => Yii::t('app', 'Additional Details'),
'class' => 'error',
]);
}
],
],
],
]);
Following is the checkbox column.
['class' => 'yii\grid\CheckboxColumn',
'header' => 'follow Up',
'contentOptions' => ['class' => 'text-center'],
'checkboxOptions' => function($model, $key, $index) {
$url = \yii\helpers\Url::to(['booking/followup/' . $model->id]);
return ['onclick' => 'js:followUp("' . $url . '")', 'checked' => $model->followUpEmailSent ? true : false, 'value' => $model->followUpEmailSent];
}
],
Can someone help with this?
You may use your Gii tool-> CRUD generator to create your filter file.
Then you can pass your params to search model like this:
$searchModel = new SearchModel;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
You will need to return your $dataProvider from SearchModel
Use the search file for implement filters on grid.
I suggest that do not use gridview filter in your case.
Write your search related code in search file and add checkbox on search form.
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<div class="ideas-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?php echo $form->field($model, 'name[]')->checkboxList(
['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']);
?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
This is my grid view code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'toolbar'=> [
['content'=>
Html::a('<i class="glyphicon glyphicon-plus"></i> Add New', ['create'],['class'=>'btn btn-shadow btn-success'])
],
'{toggleData}',
],
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'Name',
'vAlign'=>GridView::ALIGN_MIDDLE,
],
[
'attribute'=>'Age',
'vAlign'=>GridView::ALIGN_MIDDLE,
'value'=>'Age',
'width'=>'5%',
'hAlign'=>'center',
],
[
'attribute'=>'Address',
'vAlign'=>GridView::ALIGN_MIDDLE,
],
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'Status',
'vAlign'=>GridView::ALIGN_MIDDLE,
'hAlign'=>GridView::ALIGN_CENTER,
'width'=>'15%',
'filterType'=>GridView::FILTER_SELECT2,
'filterWidgetOptions'=>[
'options' => ['placeholder' => 'Search Status...'],
'pluginOptions' => [
'allowClear' => true
],
],
'value'=>function($model){
return ($model->Status=='0'?'Pending':($model->Status=='1'?'Accepted':'Rejected'));},
'filter'=>['Pending','Diterima'],
'label' => 'Status',
'refreshGrid'=>true,
'editableOptions'=>[
'format' => Editable::FORMAT_BUTTON,
'asPopover' => true,
'inputType' => Editable::INPUT_DROPDOWN_LIST,
'data'=>['0'=>'Pending', '1'=>'Accepted', '2'=>'Rejected'],
'options' => ['class'=>'form-control'],
]
],
[
'class' => 'kartik\grid\ActionColumn',
'template' => '
<li>{view}</li>
<li>{update}</li>
<li>{delete}</li>',
'dropdown'=>true,
'dropdownOptions'=>['class'=>'pull-right'],
'headerOptions'=>['class'=>'kartik-sheet-style'],
],
],
'panel'=>[
'type'=>GridView::TYPE_DEFAULT,
]
]); ?>
Help me with the Status attribute. The result from this gridview is like this :
what i want is like this :
So, it's like when the Status is Pending, the editableOption is on.But when the Status is Accepted or Rejected, the editableOption is disable. Is that possible ?
You need Change value option of Status Attribute.
Change in value option of Status Attribute.
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'Status',
// ------- your code ------------
'format'=>'raw',
'value'=>function($model){
if($model->Status=='0')
{
// if status =0 than create button and append with string
$btn='<button type="button" id='.$model->id.' class="btn btn-default ><i class="fa fa-pencil-square-o"></i></button>';
return 'Pending'.$btn;
}
else
{
return($model->Status=='1'?'Accepted':'Rejected');
}
},
// ------- your code ------------
],
There are multiple ways to do what you have asked, if you have used editableOptions, then
For Editable::INPUT_DROPDOWN_LIST type -> use 'editableButtonOptions' => [ 'disabled' => $data->your_status_variable_condition],
For any other type add readonly property
'readonly' => function($data){
return $data->your_status_variable_condition;
},
I hope it helps someone who is looking for the same issue
So I have created a simple search form, and created the query within my controller (I am aware this isn't the best way to do it and should be done in my model/search model however for the time being it will do).
I was wondering how can I add an option to my form within the two dropdown with the label All which if passed will mean that that part of the query isn't applied.
Below is my view
<?php $form = ActiveForm::begin(['id' => 'home-search','method' => 'post', 'action' => Url::to(['productitem/search'])]); ?>
<?= $form->field($productitem, 'name')->textInput(array('placeholder' => 'What are you looking for?'))->label(false) ?>
<?= $form->field($productitem, 'brand_id')->dropDownList(
ArrayHelper::map(ProductBrand::find()->all(),'id','name'),
['prompt'=>'Select Brand']
)->label(false) ?>
<?= $form->field($productitem, 'category_id')->dropDownList(
ArrayHelper::map(ProductCategory::find()->all(),'id','name'),
['prompt'=>'Select Department']
)->label(false) ?>
<div class="form-group search-button">
<button type="submit" class="btn btn-primary" name="login-button">Search <i class="fa fa-lg fa-arrow-circle-o-right"></i></button>
</div>
<?php ActiveForm::end(); ?>
Below is my controller/query
public function actionSearch()
{
$query = ProductItem::find()
->andFilterWhere(['like', 'name', $_POST['ProductItem']['name']])
->andFilterWhere(['in', 'brand_id', $_POST['ProductItem']['brand_id']])
->andFilterWhere(['in', 'category_id', $_POST['ProductItem']['category_id']]);
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
return $this->render('search', [
'dataProvider' => $dataProvider,
]);
}
hope this will offer some reference.
<?= $form->field($model, 'categoryid1')->widget(Select2::className(), [
'id' => 'categoryid1',
'name' => 'categoryid1',
'data' => ArrayHelper::map(Category::getCategoryByPid(0), 'id', 'name'),
'options' => [
'placeholder' => 'choose first',
],
'pluginOptions' => [
'allowClear' => true
],
])->label('first') ?>
<?= $form->field($model, 'categoryid2')->widget(DepDrop::className(), [
'type' => DepDrop::TYPE_SELECT2,
'id' => 'categoryid2',
'name' => 'categoryid2',
'data' => $model->categoryid1 ? ArrayHelper::map(Category::getCategoryByPid($model->categoryid1), 'id', 'name') : [],
'pluginOptions' => [
'depends' => ['product-categoryid1'],
'placeholder' => 'choose second',
'url' => Url::to(['/category/second'])
]
])->label('second'); ?>
<?= $form->field($model, 'categoryid')->widget(DepDrop::className(), [
'type' => DepDrop::TYPE_SELECT2,
'data' => $model->categoryid2 ? ArrayHelper::map(Category::getCategoryByPid($model->categoryid2), 'id', 'name') : [],
'pluginOptions' => [
'depends' => ['product-categoryid1', 'product-categoryid2'],
'placeholder' => 'third',
'url' => Url::to(['/category/third'])
]
])->label('third'); ?>
I have adjusted the widget like:
> import kartik/grid/GridView
{{ use('kartik/grid/GridView') }}
> use GridView::widget(...)
{{
grid_view_widget(
{
'dataProvider': dataProvider,
'export': false,
'responsive': true,
'condensed': true,
'toolbar': [
'{export}',
'{toggleData}'
]
}
)
}}
but the table was cut and toolbar wasn't shown:
Try
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => $columns,
'pjax' => true,
'condensed' => true,
'panel' => [
'heading' => '<h3 class="panel-title">' . $this->title,
'before' => Html::a('<i class="glyphicon glyphicon-plus"></i>', ['create'],
['class' => 'btn btn-success']) . ' ' .
Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['index'], ['class' => 'btn btn-info']),
],
'export' => false,
'toolbar' => [
'{toggleData}'
]])
With this atleast your toolbar will be shown. Let me know what happens. :)