CheckBoxColumn:id not available in Controller - yii2

Following code will show empty array for chekckBoxColumns respectively NULL in Controller of yii framework.
Any ideas, what I do wrong?
Here is code of GridView
<?=
Html::beginForm(['/mail/mail-eingang/deleteallmails'], 'post', ['name' => 'document']);
?>
<p>
<?= Html::submitButton('Delete', ['name' => 'button_checkBoxes', 'title' => 'Löscht alle ausgewählten Mails']); ?>
</p>
.
.
$gridColumn = [
.
.
[
'class' => '\kartik\grid\CheckboxColumn', 'checkboxOptions' => function($model) {
return ['value' => $model->id];
},
],
];
?> <?=
Html::endForm();
Here is Controller
public function actionDeleteallmails() {
$checkbox = (array) Yii::$app->request->post('selection');
print_r("var_dump of post:<br>");
var_dump(Yii::$app->request->post());
print_r("<br><br>var_dump of POST[button_checkBoxes]:<br>");
var_dump($_POST['button_checkBoxes']);
print_r("<br><br>var_dump of post(selection):<br>");
var_dump(Yii::$app->request->post('selection'));
print_r("<br><br>var_dump of checkbox:<br>");
var_dump($checkbox);
die();
}
Controller will give me following result:
var_dump of post:
array(3) { ["_csrf-frontend"]=> string(88) "aOy2kVhHjxM_Q40lq4Cfs-izrPeqhVDXxwcjO-HAxmAOuc7LOSzKf1EOoGfo4ffWsNHnws2yIYb1U2JQq_miVw==" ["button_checkBoxes"]=> string(0) "" ["MailEingangSearch"]=> array(5) { ["id"]=> string(0) "" ["mail_adresse_absender"]=> string(0) "" ["betreff"]=> string(0) "" ["bodytext"]=> string(0) "" ["gelesen"]=> string(0) "" } }
var_dump of POST[button_checkBoxes]:
string(0) ""
var_dump of post(selection):
NULL
var_dump of checkbox:
array(0) { }

Related

Unable to set partial view in my index page yii2

I am trying to render a partial view at index page to show a chart when a certain condition gets true by doing the following.
Index.php
<?PHP
.
.
.
.
else if($type == "403")
{
$columns = [
['class' => 'yii\grid\SerialColumn'],
'Device_ID',
'Customer_ID',
'MSN',
'kWh',
'Data_Date_Time',
];
$this->render('_kwhChart', [
'dataProvider' => $dataProvider,
]) ;
}
.
.
.
.
?>
<?=
GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => $columns
]);
?>
When a certain condition gets true I am able to see the updated gridview values but not the partial view i.e. chart.
Patial view
<?PHP
$dataPointskWh = array();
$model = $dataProvider->getModels();
foreach ($model as $row)
{
// pushing for kwh values
array_push($dataPointskWh,
array("label"=>$row['Data_Date_Time'],"y"=>$row['kWh']));
}
?>
<div id="chartContainer1" style="width: 100%; height: 300px;display: inline-block;"></div>
<script>
var chart1 = new CanvasJS.Chart("chartContainer1", {
exportEnabled: true,
animationEnabled: true,
zoomEnabled: true,
theme: "light1",
title:{
text: "Voltages"
},
legend:{
cursor: "pointer",
verticalAlign: "bottom",
horizontalAlign: "center",
itemclick: toggleDataSeries
},
data: [
{
type: "column",
lineColor:"red",
legendMarkerColor: "red",
name: "kWh",
indexLabel: "{y}",
//yValueFormatString: "V1#0.##",
showInLegend: true,
dataPoints: <?php echo json_encode($dataPointskWh, JSON_NUMERIC_CHECK); ?>
}
]
});
chart1.render();
function toggleDataSeries(e){
e.dataSeries.visible = !(typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible);
chart1.render();
}
Note: I am using a submit button, and the page refresh every time when submit button is clicked.
How can I achieve this?
Any help would be highly appreciated.
You don't render the render() .. you need .. echo
echo $this->render('_kwhChart', [
'dataProvider' => $dataProvider,
]) ;
or
<?= $this->render('_kwhChart', ['dataProvider' => $dataProvider, ]) ?>;

how do I display the same data and aggregate it in one line in yii2?

MODEL
public function getJumlah()
{
return $this->hasMany(AssetMaster::className(), ['id_asset_received' => 'received_date'])
->leftJoin('asset_received','asset_received.id_asset_received = asset_master.id_asset_master')
->groupBy('received_date')
->count();
}
VIEW
<?= GridView::widget(
[
'behaviors' => [
[
'class' => '\dosamigos\grid\behaviors\GroupColumnsBehavior',
'extraRowColumns' => ['assetMaster.asset_name'],
'extraRowValue' => function($model, $key, $index, $totals) {
return '<span class="label label-warning">' . $model['received_year'] . '</span>';
},
'mergeColumns' => ['assetMaster.asset_name','received_year']
]
],
'columns' => [
'assetMaster.asset_name',
received_date',
'received_year',
'Jumlah'
],
'dataProvider' => $dataProvider,
'layout' => "\n{items}\n{pager}"
]
);
?> '
i want sum "nama barang " every search i use , for now "jumlah" is the result of COUNT
and make it into just one line, is there anyone who can help

How to upload image in the yii2

I have one problem in yii2 when I use
$model = Client::find()->where('id=1')->one();
UploadedFile::getInstance($model, 'logo');
then this gitme NULL value. Why am I getting NULL if I print $_FILES and them I see the array values.
I have use the simple upload file in yii2
My view :
$form = ActiveForm::begin(['action' => ['center/updatelogo'], 'layout' => 'horizontal', 'options' => ['enctype' => 'multipart/form-data']]);
echo $form->field($model, 'logo')->widget(FileInput::classname(), [
'options' => ['accept' => $model->logo, 'class' => 'form-control'],
'pluginOptions' => [
'allowedFileExtensions' => ['png', 'jpg'],
'maxFileSize' => 25000,
],
]);
My controller:
$model = Client::find()->where('id=1')->one();
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model, 'logo');
if ($model->validate()) {
$model->file->saveAs('logos/' . $model->file->baseName . '.' . $model->file->extension);
}
}
var_dump($file);die();
I need upload one file to mi server and put the logo, what happened?
my $_POST and my $_FILES :
array(2) {
["_csrf"]=>
string(88) "WF67odQ_DUylcUprRN9FssjEdq5nBn2ARrjjql_D8af6Y1am1ID7AEG5KV2iWNL-7QeEoeGF_UJQYxnH6JgS1A=="
["Center"]=>
array(1) {
["logo"]=>
string(0) ""
}
}
array(1) {
["Center"]=>
array(5) {
["name"]=>
array(1) {
["logo"]=>
string(19) "Libre Albedrío.jpg"
}
["type"]=>
array(1) {
["logo"]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(1) {
["logo"]=>
string(14) "/tmp/phpYijyXv"
}
["error"]=>
array(1) {
["logo"]=>
int(0)
}
["size"]=>
array(1) {
["logo"]=>
int(19900)
}
}
}

The upload variable is empty in yii2

i want add uploadFile to: https://github.com/thyseus/yii2-message
i added public $file2 in Message.php in model
and this is my rules in model:
public function rules()
{
return [
[['title'], 'required'],
[['file2'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
[['title', 'message', 'context'], 'string'],
[['title'], 'string', 'max' => 255],
[['to'], IgnoreListValidator::class],
[['to'], 'exist',
'targetClass' => Yii::$app->getModule('message')->userModelClass,
'targetAttribute' => 'id',
'message' => Yii::t('app', 'Recipient has not been found'),
],
[['to'], 'required', 'when' => function ($model)
{
return $model->status != Message::STATUS_SIGNATURE && $model->status != Message::STATUS_DRAFT;
}],
];
}
and this is my view:
<?= $form->field($model, 'message')->textarea(['rows' => 6]) ?>
<?php
echo $form->field($model, 'file2')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*'],
]);
// With model & without ActiveForm
echo '<label class="control-label">Add Attachments</label>';
echo FileInput::widget([
'model' => $model,
'attribute' => 'file2',
'options' => ['multiple' => true]
]);
?>
and this is my sendMessage Function
$model = new Message();
$model->attributes = $attributes;
var_dump($attributes);
die;
why after upload file my result is this?
array(4) { ["to"]=> array(1) { [0]=> string(2) "65" } ["title"]=> string(3) "asd" ["message"]=> string(30) "asd . " ["file2"]=> string(0) "" }
file2 is empty !
When setting up the form, you need to mention as multipart/form-data. check below example.
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'file2')->fileInput() ?>
<button>Submit</button>
<?php ActiveForm::end() ?>

Put ActiveForm in Gridview Column Yii2

How can put an ActiveForm in a gridview column? The following is the code I made: I tried to render the page such that it includes the active form which I need.
'columns' => [
[ 'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'width: 30px; height: 30px'],
],
[ 'format' => 'raw',
'value' => function($model) { return "<p class='feedback'>". $model->KOMENTAR ."</p><br><p class='feedback-date'>". $model->TANGGAL ."</p><hr><div id='replay-". $model->ID_KOMENTAR."'><ul></ul></div>";},
],
[ 'format' => 'raw',
'contentOptions'=>['style'=>'width: 5px;'],
'value' => function($model) {
if($model->id == Yii::$app->user->identity->id) {
return Html::a('<i class="glyphicon glyphicon-share-alt"></i>',null,['id'=> 'replay-to-'. $model->ID_KOMENTAR ]).' '.
Html::a('<i class="glyphicon glyphicon-pencil"></i>', ['update', 'id' => $model->id]).' '.
Html::a('<i class="glyphicon glyphicon-trash"></i>', ['delete', 'id' => $model->id], ['data' => ['confirm' => 'Do you really want to delete this element?','method' => 'post']]);
}
return Html::a('<i class="glyphicon glyphicon-share-alt"></i>',['feedback', 'id' => $model->id],['id'=> 'replay-to-'. $model->ID_KOMENTAR ]);
},
],
[
'content' => $this->render('feedback_test'),
],
But I got this error:
PHP Warning – yii\base\ErrorException
call_user_func() expects parameter 1 to be a valid callback, function '
<div class="feedback-form">
<p>test</p>
</div>' not found or invalid function name
How do I include the active form in the grid view's column?
Try this.
[
'content' => function($model, $key, $index, $column) {
echo $this->render('feedback_test');
OR
echo $this->render('feedback_test', ['model' => $model]);
},
],
The above answer outputted the form above my grid. I wanted the form in one of my columns for every row of data. I ended up customizing an ActionColumn like this:
[
'class' => 'yii\grid\ActionColumn',
'template' => '{map}',
'contentOptions' => ['class' => 'text-center'],
'buttons' => [
'map' => function ($model) use ($m, $r) {
return $this->render('_form', ['model' => $m, 'req' => $model, 'ref' => $r]);
},
],
'urlCreator' => function ($action, $model) {
if ($action === 'map') {
return $model;
}
},
]