Model validate date format - yii2

My model when validate input date, convert the format type and it cause trouble.
<?php $form = yii\widgets\ActiveForm::begin([
'options' => ['class' => ''],
'fieldConfig' => ['template' => "{label}\n{input}\n{hint}\n{error}"]
]); ?>
<div class="col-md-4">
<?= $form->field($model, 'startDate')->widget(DatePicker::classname(),
[
'language' => 'pt-BR',
'dateFormat' => 'dd/MM/yyyy',
'clientOptions' => [
'yearRange' => '2005:+0',
'defaultDate' => date('d/m/Y', strtotime('-7days'))
],
'options' => [
'class' => 'form-control',
'maxlength' => '10',
'placeholder' => 'Data inicial',
]
]
)->label(false) ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'endDate')->widget(DatePicker::classname(),
[
'language' => 'pt-BR',
'dateFormat' => 'dd/MM/yyyy',
'clientOptions' => [
'yearRange' => '2005:+0',
'defaultDate' => date('d/m/Y')
],
'options' => [
'class' => 'form-control',
'maxlength' => '10',
'placeholder' => 'Data final',
]
]
)->label(false) ?>
</div>
<div class="col-md-4">
<?= Html::submitButton('Filtrar', ['class' => 'btn btn-primary']) ?>
<?php yii\widgets\ActiveForm::end(); ?>
</div>
And models:
<?php
namespace frontend\models;
use yii\base\Model;
use Yii;
class Analysis extends Model
{
public $startDate;
public $endDate;
public function rules()
{
return [
['startDate', 'required'],
['startDate', 'date', 'format' => 'dd/MM/yyyy'],
['startDate', 'checkDateToday'],
['startDate', 'checkGAMinDate'],
['endDate', 'required'],
['endDate', 'date', 'format' => 'dd/MM/yyyy'],
['endDate', 'checkDateToday'],
['endDate', 'checkGAMinDate'],
['endDate', 'checkDateGreaterThan']
];
}
public function checkDateToday($attribute, $params)
{
if($this->$attribute <= date('d/m/Y')){
return true;
} else {
var_dump($this->$attribute);
$this->addError($attribute, $attribute . ' não pode ser maior que hoje: ' . date('d/m/Y'));
}
}
public function checkGAMinDate($attribute, $params)
{
if($this->$attribute > date('01/01/2005')){
return true;
} else {
$this->addError($attribute, $attribute . ' não pode ser menor que 01/01/2005');
}
}
public function checkDateGreaterThan($attribute, $params)
{
if($this->endDate < $this->startDate){
return true;
} else {
$this->addError($attribute, 'Data final não pode ser maior que data incial.');
}
}
public function attributeLabels()
{
return [
'startDate' => Yii::t('app', 'Data inicial'),
'endDate' => Yii::t('app', 'Data final'),
];
}
}
When i put a date like "25/12/2015" ('d/m/Y'), the model return after validate: "12/25/2015". Whats going wrong?

The problem is Datepicker's value property. As the documentation says, it uses Yii::$app->formatter->asDate() to generate the value.
Since the value will be a string always, it must be a valid date format. And as you can see there is no dd/mm/yyyy format, but there is a mm/dd/yyyy.
I can not find a way to cancel this formatting, so you can either change your format or, if you want to maintain this format, you could just change the value of the model to another valid format (IE: dd-mm-yyyy), only to print it correctly.
Something like this before the form:
$model->startDate = str_replace('/', '-', $model->startDate);
$model->endDate = str_replace('/', '-', $model->endDate);

Related

Why does it give an error "missing required parameters: id"?

I understand that the error occurred due to the confusion of variables, although these are my assumptions .. The fact is that this error occurred in the update and in the view: update and view. I have already tried everything .. Please poke your nose where I went wrong?
My controller in which I pass two actions view and update:
public function actionView($id)
{
return $this->render('view', [
'faq' => $this->findModel($id),
]);
}
public function actionUpdate($id)
{
$faq = FaqLang::findOne($id);
$faqLang = Faq::findOne($faq->faq_id);
if ($faq->load(Yii::$app->request->post()) && $faqLang->load(Yii::$app->request->post()) && Model::validateMultiple([$faq, $faqLang]))
{
$faqLang->save(false);
$faq->save(false);
return $this->redirect(['view', 'id' => $faq->id]);
}
return $this->render('update', [
'faq' => $faq,
'faqLang' => $faqLang,
]);
}
protected function findModel($id)
{
if (($faq = FaqLang::findOne($id)) !== null) {
return $faq;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
view.php in which I am trying to display data using DetailView
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* #var $this yii\web\View */
/* #var $faqLang app\modules\admin\models\Faq */
/* #var $faq app\modules\admin\models\FaqLang */
//$this->title = $faq->id;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Faqs'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="faq-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a(Yii::t('app', 'Update'), ['update', 'id' => $faq->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $faq->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'faq' => $faq,
'attributes' => [
'id',
['attribute' => 'name', 'value' => 'faqLang.name'],
['attribute' => 'body', 'value' => 'faqLang.body:ntext'],
'put_date',
[
'attribute' => 'hide',
'format' => 'html',
'value' => function($model) {
if($model->hide == 'show')
return 'Нет';
else
return 'Да';
}
],
],
]) ?>
update.php through which the records are updated
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $faqLang app\modules\admin\models\Faq */
/* #var $faq app\modules\admin\models\FaqLang */
$this->title = Yii::t('app', 'Update Faq: ' . $faq->id, [
'nameAttribute' => '' . $faq->id,
]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Faqs'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $faq->id, 'url' => ['view', 'id' => $faq->id]];
$this->params['breadcrumbs'][] = Yii::t('app', 'Update');
?>
<div class="faq-update">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'faq' => $faq,
'faqLang' => $faqLang,
]) ?>
</div>
And 2 related Faq and FaqLang models
Faq.php
public function getFaqLang()
{
return $this->hasMany(FaqLang::className(), ['faq_id' => 'id']);
}
FaqLang.php
public function getFaq()
{
return $this->hasOne(Faq::className(), ['id' => 'faq_id']);
}
You may be seeing this error because the request path/query string you're using to run your controller actions does not include the $id parameter.
If you specify a method parameter in a controller action, that parameter must be defined in your routes, and passed in the request
Your action methods are as follows:
public function actionView($id)
{
....
}
Which expects a route like
'/view/<id:\d+>' => 'admin/faqs/view'
and
public function actionUpdate($id)
{
...
}
Which expects a route like
'/update/<id:\d+>' => 'admin/faqs/update'
So for example, for the request /view/123 , the value 123 is passed as the value of $id into the actionView($id) method.
If you have a route set up like
'/view' => 'admin/faqs/view'
There's no id parameter defined, and the controller will throw the error missing required parameters: id

Print gridview search in PDF using kartik-mPDF extension

Im using kartik-mpdf library to generate pdf’s of my gridviews, the thing is I want to pass $dataProvider and $searchModel from index.php to the view that is going to be renderer as pdf document (pdf.php), So, when I search on index and click the button ‘Generar pdf’ it calls action GenerarPdf on AlumnoController and loads the same $searchModel and $dataProvider of index action into pdf.php gridview and generate the pdf file.
Here’s the code.
AlumnoController.php - actionGenerarPdf
public function actionGenerarPdf($searchModel, $dataProvider)
{
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
$formatter = \Yii::$app->formatter;
$pdf = new Pdf([
'mode' => Pdf::MODE_CORE, // leaner size using standard fonts
'destination' => Pdf::DEST_BROWSER,
//Se renderiza la vista "pdf" (que abrirá la nueva ventana)
'content' => $this->renderPartial('pdf', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]),
'options' => [
// any mpdf options you wish to set
],
'methods' => [
'SetTitle' => 'SIE: Sistema de Informacion Estudiantil',
'SetSubject' => 'Generating PDF files via yii2-mpdf extension has never been easy',
'SetHeader' => ['SIE: Sistema de Información Estudiantil||Generado el: ' . $formatter->asDate(date("r"))],
'SetFooter' => ['|Página {PAGENO}|'],
'SetAuthor' => 'SIE: Sistema de información Estudiantil',
'SetCreator' => 'Juan Carlos Reyes Suazo',
// 'SetKeywords' => 'Sie, Yii2, Export, PDF, MPDF, Output, yii2-mpdf',
]
]);
return $pdf->render();
}
index.php
<p>
<?php
if (User::isSuperAdmin(Yii::$app->user->identity->id)){
echo Html::a('Crear Alumno', ['create'], ['class' => 'btn btn-success']);
echo Html::a('Ver PDF', [
'/alumno/generar-pdf',
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
],
[
'class' => 'btn btn-success',
'target'=>'_blank',
'data-toggle'=>'tooltip',
// 'title'=>'Will open the generated PDF file in a new window'
]);
}
?>
</p>
pdf.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
['attribute' => 'nombre',
'format' => 'raw',
'value'=>function ($data) {
return Html::a($data['nombre'],['alumno/view', 'id' => $data['id']]);
},
],
//'rut',
['attribute' => 'curso_id',
'format' => 'raw',
'value'=>function ($data) {
return Html::a($data['curso_id'],['curso/view', 'id' => $data['curso_id']]);
},
],
//'fecha_nacimiento',
//'fono',
//'direccion',
//'email:email',
//'apoderado',
['attribute' => 'apoderado',
'format' => 'raw',
'value'=>function ($data) {
if ($data->apoderado_id != null)
return Html::a($data->apoderado->nombre, ['apoderado/view', 'id' => $data['apoderado_id']]);
else
return null;
},
],
//'apoderado_suplente',
//'fono_apoderado',
//'fono_apoderado_sup',
//'antecedentes_medicos',
//'grupo_sanguineo',
//'programa_integracion',
//'taller_artistico',
//'anotaciones',
['class' => ActionColumn::className(), 'template' => '{update} {delete}'],
],
]); ?>
Im getting Bad Request (#400)
Se recibieron datos erróneos para el parámetro “searchModel”
Any help would be appreciated.
I solved this by using sessions, and I generated $searchModel and $dataProvider in actionGenerarPdf instead of sending them by a button.
I'll quote #softark from yii forum
"You can’t pass $searchModel and $dataProvider as the query parameters to an action method, because they are not such simple variables that could be converted to simple strings.
So you have to create the search model and the data provider in your actionGenerarPdf method.
In order to get back the search parameters used in the index action, you can store the query parameters in the session and retrieve them in actionGenerarPdf."
https://forum.yiiframework.com/t/print-gridview-search-in-pdf-using-kartik-mpdf-extension-solved/124925
Here's the code
public function actionIndex()
{
$searchModel = new AlumnoSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$session = Yii::$app->session;
$session->open();
$session['query_params'] = json_encode(Yii::$app->request->queryParams);
$session->close();
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function actionGenerarPdf()
{
$session = Yii::$app->session;
$session->open();
$queryParams = isset($session['query_params']) ? json_decode($session['query_params'], true) : [];
$session->close();
$searchModel = new AlumnoSearch();
$dataProvider = $searchModel->search($queryParams);
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
$formatter = \Yii::$app->formatter;
$pdf = new Pdf([
'mode' => Pdf::MODE_CORE, // leaner size using standard fonts
'defaultFontSize' => 18,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_BROWSER,
//Se renderiza la vista "pdf" (que abrirá la nueva ventana)
'content' => $this->renderPartial('pdf', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]),
'options' => [
// any mpdf options you wish to set
],
'methods' => [
'SetTitle' => 'SIE: Sistema de Informacion Estudiantil',
'SetSubject' => 'Generating PDF files via yii2-mpdf extension has never been easy',
'SetHeader' => ['SIE: Sistema de Información Estudiantil||Generado el: ' . $formatter->asDate(date("r"))],
'SetFooter' => ['|Página {PAGENO}|'],
'SetAuthor' => 'SIE: Sistema de información Estudiantil',
'SetCreator' => 'Juan Carlos Reyes Suazo',
// 'SetKeywords' => 'Sie, Yii2, Export, PDF, MPDF, Output, yii2-mpdf',
]
]);
return $pdf->render();
}
I hope this helps others who want to achieve the same

Yii2 - Dropdownlist OnChange to Display Other Model Attribute

I have these Model Classes
Model:Subject
public function attributeLabels()
{
return [
'subject_id' =>Yii::t('app', 'ID'),
'subject_title' => Yii::t('app', 'Subject Title'),
];
}
Model:Grouping
public function attributeLabels()
{
return [
'grouping_id' => Yii::t('app', 'Grouping ID'),
'grouping_name' => Yii::t('app', 'Grouping Name'),
];
}
Model:SubjectGrouping
public function attributeLabels()
{
return [
'subject_grouping_id' => 'Subject Grouping ID',
'subject_grouping_grouping_id' => 'Subject Grouping Grouping ID',
'subject_grouping_subject_id' => 'Subject Grouping Subject ID',
];
}
Model:Exam
public function attributeLabels()
{
return [
'exam_id' =>Yii::t('app', 'ID'),
'exam_name' => Yii::t('app', 'Exam Name'),
'exam_group_id' => Yii::t('app', 'Exam Group'),
];
}
Model:SubjectGrouping has Foreign Keys from Model: Grouping (subject_grouping_grouping_id) and Model:Subject (subject_grouping_subject_id)
In Model:Exam, exam_group_id relates to subject_grouping_grouping_id in Model:SubjectGrouping.
Controller
public function actionCreate()
{
$model = new Exam();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post())) {
$model->attributes = $_POST['Exam'];
$model->save();
return $this->redirect(['index']);
} else {
return $this->render(
'create', [
'model' => $model
]
);
}
}
View
<div class="col-xs-12 col-sm-4 col-lg-4">
<?=$form->field($model, 'exam_name')->textInput(['maxlength' => 50, 'placeholder' => 'Enter Exam Name'])?>
</div>
<div class="col-xs-12 col-sm-4 col-lg-4">
<?=$form->field($model, 'exam_group_id')->widget(
Select2::classname(), [
'data' => ArrayHelper::map(app\models\Grouping::find()->all(), 'grouping_id', 'grouping_name'),
'language' => 'en',
'options' => ['placeholder' => '--- Select Grouping ---'],
'pluginOptions' => [
'allowClear' => true
]
]
);
?>
</div>
From the diagram, how do I achieve these?
when Dropdownlist for exam_group_id is onChange the list of subjects with respect to grouping is displayed using the relationship between Model: SubjectGrouping and Model: Subject.
Others fields such as exam_name (textInput) and exam_group_id (Dropdownlist) should be save into the Model: Exam. But the subject list should not be saved. It is only for display purpose.
How do I achieve these?
I am using kartik\widgets\Select2 for the dropdownlist
I have done similar code by Kartik extension for state and country data.
here is the code for that.
<?php
//in view use this
use kartik\widgets\DateTimePicker; // or kartik\select2\Select2
use kartik\widgets\DepDrop;
use kartik\widgets\Select2;
?>
<?php
echo
$form->field($model, 'country_id')->widget(
Select2::className(),
[
'data' => \yii\helpers\ArrayHelper::map(common\models\Country::find()->all(), 'id', 'name'),
'options' => [
'id' => 'country_id',
'prompt' => Yii::t('aspns', 'Select')
]
]
);
?>
<?php
echo
$form->field($model, 'state_id')->widget(
DepDrop::classname(),
[
'data' => !empty($model->state_id) ? \yii\helpers\ArrayHelper::map(common\models\State::find()->where(['country_id' => $model->country_id])->all(), 'id', 'name') : [],
'options' => ['id' => 'state_id', 'placeholder' => Yii::t('aspns', 'Select')],
'pluginOptions' => [
'depends' => ['country_id'],
'url' => Url::to(['//country/get-states'])
]
]
)->label(Yii::t('aspns', 'State'));
?>
// In Controller section you need to do this
public function actionGetStates() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$country_id = (int)$parents[0];
$out = State::find()->where(['country_id'=>$country_id])->select(['id', 'name'=>'name'])->asArray()->all();
return Json::encode(['output'=>$out, 'selected'=>'']);
}
}
return Json::encode(['output'=>'', 'selected'=>'']);
}
Change the code according to your requirement.
Thanks

Updating Image in Yii2

While updating image using Yii2 I'm facing a problem with the validation.Its always asking me to upload an image. But I don't want this. Always updating an image is not necessary.
I tried skipOnEmpty but its not working properly it cause effect while uploading a photo, which is also incorrect.
Please help!!
Model
public function rules()
{
return [
[['carid', 'name'], 'required'],
[['carid', 'coverphoto', 'status'], 'integer'],
[['name'], 'string', 'max' => 200],
[['imageFiles'], 'image','extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 4, 'minWidth' => 100, 'maxWidth' => 800, 'minHeight' => 100, 'maxHeight'=>600,'skipOnEmpty' => true],
];
}
Controller
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->photoid]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
You should to use scenario for update.
Like as,
Add on condition in model's rule for applying scenario .
[['imageFiles'], 'image','extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 4, 'minWidth' => 100, 'maxWidth' => 800, 'minHeight' => 100, 'maxHeight'=>600,'skipOnEmpty' => true, 'on' => 'update-photo-upload'],
And use that scenario in controller's action.
public function actionUpdate($id)
{
$model = $this->findModel($id);
$model->scenario = 'update-photo-upload';
........
.....
}
try rule as
[
['imageFiles'], 'file',
'extensions' => 'png, jpg, jpeg, gif',
'mimeTypes' => 'image/jpeg, image/png',
'maxFiles' => 4,
'minWidth' => 100,
'maxWidth' => 800,
'minHeight' => 100,
'maxHeight'=>600,
'skipOnEmpty' => true
],
This is working in my case, hope it works for you too.
**Your View Like This**
<?php use yii\widgets\ActiveForm;?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'imageFiles')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end() ?>
*** Your Controller Like This******
use Yii;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;
class SiteController extends Controller
{
public function actionUpdate()
{
$model = new UploadForm ();
$model->scenario = 'update';
if (Yii::$app->request->isPost) {
$model->imageFiles= UploadedFile::getInstance($model, 'imageFiles');
if ($model->upload()) {
// file is uploaded successfully
return;
}
}
return $this->render('update', ['model' => $model]);
}
}
***** Your ModelLike This ******
use yii\base\Model;
use yii\web\UploadedFile;
class UploadForm extends Model
{
/**
* #var UploadedFile[]
*/
public $imageFiles;
public function rules()
{
return [
[['carid', 'name'], 'required'],
[['carid', 'coverphoto', 'status'], 'integer'],
[['name'], 'string', 'max' => 200],
[['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4,'on'=>'update'],
];
}
public function upload()
{
if ($this->validate()) {
foreach ($this->imageFiles as $file) {
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
return true;
} else {
return false;
}
}
function scenario()
{
return [
'create' => ['imageFiles ', 'carid','name','coverphoto','status'],
'update' => ['imageFiles ', 'carid','name','coverphoto','status'],
];
}
}

yii2 Pjax + java script prompt

It there any way to make 'data-confirm' => Please enter the number' not just confirm but some like JS prompt and get imputed values to sent it to controller?
<?php \yii\widgets\Pjax::begin(['id' => 'pjax-orders_table','clientOptions' => ['method' => 'POST'], 'enablePushState'=>false]) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'id'=>'orders_table',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
///some columns
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}{some}',
'buttons' => [
'some' => function ($url,$model,$key) {
if($model->status=='not confirm')
{
return Html::a('<span class="glyphicon glyphicon-trash"</span>',['my/some', 'id' => $model->id],[
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Please enter the number',
'data-method' => 'post',
]);
}
},
],
],
],
]); ?>
<?php \yii\widgets\Pjax::end() ?>
In controller
public actionSome()
{ $dataProvider = new ActiveDataProvider();
$dataProvider->query = Orders::find()->all();
return $this->render('some',['dataProvider'=>$dataProvider]);
}
instead of Html::a() use Html::button()where button id = some_item_id and then write this JS code
$('.buttons_class').click(function(){
var selection;
do{
selection = parseInt(window.prompt("Please enter a number from 1 to 100", ""), 10);
}
while(isNaN(selection));
if(!isNaN(selection))
{
$.post("some",{id:45,prompt_value:selection}, function(response)
{
console.log(response);
$.pjax.reload({container:'#pjax-orders_table'});
});
}
})