Yii2 Render an image in DetailView - yii2

I am trying to render an Image in view file. My code is like below:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'name',
//'photo',
[
'attribute'=>'photo',
'value'=>('<img src =' .'uploads/' . $model->photo . ' height="100" width="100"' . '>')
],
[
'attribute' => 'birth_date',
'format' => ['date', 'dd-MM-Y'],
],
'mobile',
],
]) ?>
Though the code below works:
<?php echo ('<img src =' .'uploads/' . $model->photo .'>'); ?>
Thanks.

Try this:
[
'attribute'=>'photo',
'value'=>$model->photo,
'format' => ['image',['width'=>'100','height'=>'100']],
],

DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'photo:image',
],
])

[
'attribute'=>'group_pic',
'value'=>('uploads/group_pro_pic/' . $model->group_pic),
'format' => ['image',['width'=>'230','height'=>'200']],
]

Try this:
[
'attribute'=>'images',
'value'=> Yii::$app->homeUrl.'uploads/'.$model->images,
'format' => ['image',['width'=>'100','height'=>'100']],
],

Try this
[
'attribute' => 'vUpload',
'value'=>'uploads/' .$model->vUpload,
'format' => ['image',['width'=>'100','height'=>'100']],
],

[
'attribute'=>'profile',
'value'=>'../uploads/' .$model->profile,
'format' => ['image',['width'=>'100','height'=>'100']],
],

Related

Update data in Kartik Detailview is not working

I've detail showed using Kartik Detail View. This widget has Edit inline function by clicking pencil icon button in top right side like this.
But then the table doesn't be editable :
And nothing happen, my data still the same, my update not success. It's possible to solve my problem? Thanks.
I have read the official guide and it looks identical:
https://demos.krajee.com/detail-view
This is my view code:
<?php echo DetailView::widget([
'model' => $modelAnagrafiche,
'responsive' => true,
'mode' => 'edit',
'enableEditMode' => true,
'buttons1' => '{update}',
'panel' => [
'type' => 'primary',
'heading' => 'Contratto' . ' : ' . $modelAnagrafiche >cognome_ragione_sociale . ' ' . $modelAnagrafiche->nome
],
'attributes' => [
[
'group'=>true,
'label'=>'Sezione Anagrafica',
'rowOptions'=>['class'=>'table-primary']
],
[
'columns' => [
[
'attribute' => 'cognome_ragione_sociale',
'displayOnly' => true,
'valueColOptions' => ['style' => 'width:30%']
],
[
'attribute' => 'nome',
'format' => 'raw',
'valueColOptions' => ['style' => 'width:30%'],
'displayOnly' => true,
'type' => DetailView::INPUT_TEXT,
],
],
],
[
'columns' => [
[
'attribute' => 'codice_fiscale',
'displayOnly' => true,
'valueColOptions' => ['style' => 'width:30%']
],
[
'attribute' => 'partita_iva',
'format' => 'raw',
'valueColOptions' => ['style' => 'width:30%'],
'displayOnly' => true
],
],
],
[
'columns' => [
[
'attribute' => 'tipo_documento',
'displayOnly' => true,
'valueColOptions' => ['style' => 'width:30%'],
'format' => 'raw',
'value' => $modelAnagrafiche->tipoDocumento,
],
[
'attribute' => 'numero_documento',
'format' => 'raw',
'valueColOptions' => ['style' => 'width:30%'],
'displayOnly' => true
],
],
],
[
'columns' => [
[
'attribute' => 'data_nascita',
'displayOnly' => true,
'format' => 'date',
'type' => DetailView::INPUT_DATE,
'widgetOptions' => [
'pluginOptions' => ['format' => 'yyyy-mm-dd']
],
],
[
'attribute' => 'id_provincia_nascita',
'displayOnly' => true,
'valueColOptions' => ['style' => 'width:30%'],
'format' => 'raw',
'value' => $modelAnagrafiche->provinciaNascitaNome,
'label' => 'Provincia Nascita'
],
],
],
[
'columns' => [
[
'attribute' => 'id_comune_nascita',
'displayOnly' => true,
'format' => 'raw',
'value' => $modelAnagrafiche->comuneNascitaNome,
'label' => 'Comune Nascita'
],
],
],
],
]);
?>
This is the action in my controller:
public function actionUpdateAnagrafica()
{
$post = Yii::$app->request->post();
if (empty($post['Anagrafiche']['id'])) {
throw new NotFoundHttpException('Non esiste nessuna anagrafica.');
}
$modelAnagrafiche = Anagrafiche::findOne($post['Anagrafiche']['id']);
if ($modelAnagrafiche->load($post) && $modelAnagrafiche->save()) {
return $this->redirect(['view', 'id' => $modelAnagrafiche->id]);
} else {
return $this->render('update-anagrafica', [
'modelAnagrafiche' => $modelAnagrafiche,
]);
}
}
You have to remove all the displayOnly attributes.
According to the official guide:
displayOnly: boolean|Closure, if the input is to be set to as display
only in edit mode. If set to true, no editable form input will be
displayed, instead this will display the formatted attribute value.

Change name of \yii\imperavi\Widget widget

I want to change the name of the form field from Page[body] to body lets say. Following is the code for WYSIWYG editor.
<?php echo $form->field($model, 'body')->widget(
\yii\imperavi\Widget::className(),
[
'plugins' => ['fontcolor', 'video'],
'options'=>[
'minHeight'=>400,
'maxHeight'=>400,
'buttonSource'=>true,
//'imageUpload'=>Yii::$app->urlManager->createUrl(['/file-storage/upload-imperavi'])
]
]
) ?>
I don't want to change my model just the name of the form field submitted as the filed is submitted to remote API.
For normal fields i do <?php echo $form->field($model, 'name')->textInput(['name' => 'name']) ?>
In the options you could assign an your name value
<?php echo $form->field($model, 'body',
[ 'options' => [ 'name' => 'your_name']])->widget(
\yii\imperavi\Widget::className(),
[
'plugins' => ['fontcolor', 'video'],
'options'=>[
'minHeight'=>400,
'maxHeight'=>400,
'buttonSource'=>true,
//'imageUpload'=>Yii::$app->urlManager->createUrl(['/file-storage/upload-imperavi'])
]
]
) ?>
Thank you guys this worked.
<?php
echo yii\imperavi\Widget::widget([
// You can either use it for model attribute
'model' => $model,
'attribute' => 'body',
// or just for input field
//'name' => 'body',
'htmlOptions'=>[
'name'=>'body',
],
// Some options, see http://imperavi.com/redactor/docs/
'options' => [
'toolbar' => false,
],
]);
?>
and this also
<?php echo $form->field($model, 'body',
[ 'options' => [ 'name' => 'body']])->widget(
\yii\imperavi\Widget::className(),
[
'plugins' => ['fontcolor', 'video'],
'htmlOptions'=>['name'=>'body'],
'options'=>[
'minHeight'=>400,
'maxHeight'=>400,
'buttonSource'=>true,
//'imageUpload'=>Yii::$app->urlManager->createUrl(['/file-storage/upload-imperavi'])
]
]);?>

my export in yii2 using kartik-v/yii2-export not working

i am trying to use kartik export and its not working for me. In config file I have added following code:
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
],
],
In Composer i have added folowing code
"kartik-v/yii2-export": "#dev",
"kartik-v/yii2-mpdf":"#dev",
"kartik-v/yii2-grid": "#dev"
My view code is like this:
<?php
use yii\helpers\Html;
use kartik\grid\GridView;
use yii\bootstrap\Tabs;
use kartik\export\ExportMenu;
use yii\widgets\Pjax;
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="general-info-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php
<p>
<?= Html::a('create', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php
$gridColumns = [
'sfcl_name',
[
'attribute'=> 'org_type',
'value' => 'orgType.cv_lbl'
],
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'columns' => [
'sfcl_name',
'phone',
[
'attribute'=>'regd_dt_ad',
'format'=>['date', 'php:Y-M-d'],
'xlFormat'=>'mmm\-dd\, yyyy', // different date format
'width'=>'100px'
],
[
'attribute'=> 'org_type',
'value' => 'orgType.cv_lbl'
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
My controller code is:
My controller code to call this view : public function actionIndex()
{
$searchModel = new SfclGeneralSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
On click of export menu nothing is happening.In tutorial i saw there were options for exporting to pdf ,html,csv,json,text.such options doesnot appers in my case.Is the css not working or what?
you run "composer update" command?.
also in your web.php
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
],
],
add the property downloadAction, like this:
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
'downloadAction' => 'gridview/export/download',
]
],

Yii2-editable-widget. Doesn't work with few widgets

I'm using editable widget (2amigos/yii2-editable-widget or kartik-v/yii2-editable). It works fine, but when I try it in foreach it works with first element only. I want to use a several widgets for a model. How can I solve it?
Here is the code:
foreach ($models as $model) {
echo Editable::widget( [
'model' => $model,
'attribute' => 'name',
'url' => 'site/rename',
'type' => 'text',
'mode' => 'pop',
'clientOptions' => [
' pk' => $model->id,
]
]);
}
I guess you have to set a unique id for each Editable Widget. Look how the js code for the widget is embedded.
e.g. if you use kartiks widget
echo Editable::widget([
'model' => $model,
'attribute' => 'name',
'type' => 'primary',
'size'=> 'lg',
'inputType' => Editable::INPUT_TEXT,
'editableValueOptions' => ['class' => 'text-success h3'],
'options'=> [
'id'=>'name-editable'.uniqid(),
]
]);

yii2 detailview set value with function htmlspecialchars() expects parameter 1 to be string, object given

How to set value for DetailView. I am trying by set value gridview way but that just show error result
This is the code i wrote
<?php echo DetailView::widget([
'model' => $model,
'attributes' => [
'username',
'email',
[ 'label' => 'Nama',
'value' => function($model) { return $model->first_name ." ". $model->last_name; },
],
],
]); ?>
The error is :
htmlspecialchars() expects parameter 1 to be string, object given
In DetailView no need to use function in value property.
<?php echo DetailView::widget([
'model' => $model,
'attributes' => [
'username',
'email',
[ 'label' => 'Name',
'value' => $model->first_name ." ". $model->last_name,
],
],
]); ?>