How do I add Kartik ActiveField on Kartik DetailView? - yii2

I'm using Kartik's awesome extension called DetailView. I'd like to add the Kartik widget called ActiveField to the DetailView. What is the proper way to use the ActiveField widget in the DetailView widget?
The code I'm using generates the error First parameter must either be an object or the name of an existing class. According to Kartik's documentation, any widget (not just the DetailView widgets) can be used. What is the proper way to do that?
$attributes = [
['attribute' => 'definition_summary',
'format' => 'raw',
'type'=>DetailView::INPUT_WIDGET,
'widgetOptions'=>[
'class'=>ActiveField::className(),
'addon' => ['prepend' => ['content' => '#']],
],
];
echo DetailView::widget([
'model' => $model,
'attributes' => $attributes,
]);

Related

Yii2: Using TinyMCE into Kartik's DetailView with custom settings

I'd like to insert 2amigos' TinyMCE widget in Kartik's DetailView edit mode. This is what I got by now:
[
'attribute' => 'myAttribute',
'format' => 'raw',
'type' => 'widget',
'widgetOptions' => ['class' => TinyMce::classname()],
'value' => $model->myAttribute,
],
With this chunk I managed to show TinyMCE editor with default settings. What I'm trying to do now is to show it with custom settings defined by:
Yii::$app->params['myTinyMceParams']
In form I'm doing this:
<?= $form->field($model, 'myAttribute')->widget(TinyMce::className(), Yii::$app->params['myTinyMceParams']) ?>
Any ideas?
I finally found a solution, maybe not ideal but fully operative: to merge both 'class' array and rest-of-options array into 'widgetOptions':
'widgetOptions' => ArrayHelper::merge(['class' => TinyMce::classname()], Yii::$app->params['tinyMceParams']),

Handle Image update in yii2

Hi everyone i am using FileInput kartik widget in yii2 project and uploading and saving works fine but i am having problem while updating the images
I can get the images path on the update form and create and array and display in as initialpreview in the widget but the problem is that my files field is required field so even there are some values in initialpreview the form gives error to upload image as its required field. So what should i do here?
I only want to give preview of their uploaded pics to the user but if they dont make any changes than i dont want to update anything for images
Following is my view code and model code
return [
[[ 'price', 'created_date', 'created_by', 'updated_date', 'updated_by','file','address'], 'required'],
[['price'], 'number'],
[['address'], 'string', 'max' => 255],
[['file'], 'file', 'extensions'=>'jpg, gif, png', 'maxFiles' => 4],
];
<?php
$initalpreview = array();
foreach($model->adsImages as $images) {
$initalpreview[] = Html::img('/upload/'.$images->image, ['class'=>'file-preview-image']);
}
?>
<?=$form->field($model, 'file[]')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*',
'multiple'=>true],
'pluginOptions' => [
'maxFileCount' => 5,
'initialPreview'=> $initalpreview,
'showUpload' => false,
]
]); ?>
So what should i do here?
Thank you
try this
In Model create a scenario named 'update' and mention the fields that are required when update action works
return [
[['price', 'created_date', 'created_by','updated_date','updated_by','address'], 'required', 'on' => 'update'],
];
Now inside controller action update call the scenario
public function actionUpdate(){
//Model Declaration
$model->scenario = 'update';
// update code
}

yii2 Kartik-V Typeahead Basic autocomplete on name but store integer value

Updates have been made below
I am trying to use the Kartik-V Typeahead Basic widget with the Yii2 Framework.
The code below is working to display the required data, the user can search via the university name and it appears in the autocomplete list.
The issue is, the model needs to the university id, not the name. Thus the rules are this field can only store an integer and returns a validation error once you select one of the typeahead results.
<?= $form->field($model, 'university_id')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...'],
]); ?>
I am hoping someone can help me understand if there is a setting that needs to be changed so when saving, the user friendly 'uni_name' data is changed back to the uni 'id'.
UPDATE:
I have gotten the code partly working thanks to "Insane Skull".
The new code is:
<?= $form->field($model, 'name')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...', 'id' => 'testID'],
'pluginEvents' => [
'typeahead:select' => new yii\web\JsExpression("function(event, ui) { $('#testing123').val(ui.item.id); }"),
]
]); ?>
<?= Html::activeHiddenInput($model, 'university_id', array ('id' => 'testing123'))?>
Now I am unfortunately getting the error:
Method yii\web\JsExpression::__toString() must return a string value
I would rather use Select2 instead of Typeahead, you are basically trying to implement the functionality that already exists on Select2 but using Typeahead.
<?= $form->field($model, 'university_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'options' => ['placeholder' => 'Filter as you type ...'],
]); ?>
You can use activeHiddenInput() for this purpose.
Create one public variable in model say name.
Then:
<?= $form->field($model, 'name')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...'],
'select' => new yii\web\JsExpression("function( event, ui ) {
$('#id_of_hiddenField').val(ui.item.id);
}")
]); ?>
<?= Html::activeHiddenInput($model, 'university_id')?>
And in Controller Get the value of activeHiddenField.

yii2 join model as dataProvider

I've joined 2 tables like following:
$model = SalesEntry::find()
->joinWith('salesItems')
->all();
then in view used DataProvider like following:
GridView::widget([
'dataProvider' => $model,
'columns' => [
'date', // sample field from first table to see if ok
],
]);
and I’ve got following error:
Call to a member function getCount() on a non-object
What am I doing wrong here?
That is because an ActiveQuery instance is not a DataProvider, which the widget expects. You need to wrap it in an ActiveDataProvider for it to work:
GridView::widget([
'dataProvider' => new \yii\data\ActiveDataProvider(['query' => $model]),
' columns' => [
'date', // sample field from first table to see if ok
],
]);
DataProvider in GridView should be an instance of yii\data\DataProviderInterface
See docs.

html to pdf converter in yii2 with pagination in table

view:
<p>
<?= Html::a('Download This page', ['report'], ['class' => 'btn btn-danger']) ?>
</p>
controller:
public function actionReport()
{
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
'content' => $content,
'options' => ['title' => 'Krajee Report Title'],
'methods' => [
'SetHeader' => ['Krajee Report Header'],
'SetFooter' => ['{PAGENO}'],
]
]);
return $pdf->render();
}
This function works perfectly but my html table has pagination . so i am confused how to deal with table that has pagination.
You should disable the pagination. it all depends on how you define your data provider (read more about data providers here http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html). Probably you should do something like this
************* = new ActiveDataProvider([
'pagination' => false,
..............
]);
I think you can also call it like
$dataProvider->pagination =false;
Just in case you need to disable it in a specific case.