DatePicker Plugin Made by 2Amigo in yii 2 - php

I downloaded DatePicker Plugin using composer in Yii2 and I can see the file in my vendor directory. The plugin is 2amigos/yii2-date-picker-widget you can see it on GitHub.
My problem is when I make a model name COMPANIES and I made a CRUD, I want to use DatePicker in my CRUD and here is my _form.php file :
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use dosamigos\datepicker\DatePicker;
?>
<div class="companies-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'company_name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'company_email')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'company_address')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'logo')->textInput(['maxlength' => true]) ?>
<?=
$form->field($model, 'company_start_date')->widget(
DatePicker::className(), [
'inline' => false,
// 'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'y-m-d',
],
]);
?>
<?= $form->field($model, 'company_created_date')->textInput() ?>
<?= $form->field($model, 'company_status')->dropDownList([ 'active' => 'Active', 'inactive' => 'Inactive', ], ['prompt' => '']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
but when I click on Create in my grid it gives me the following error:
Error
(#1) An internal server error occurred. The above error occurred while
the Web server was processing your request.
Please contact us if you think this is a server error. Thank you.
What should I do to fix this?

Related

How to use search button for gridview

Sorry for my english!!
I need your help. I wantto make filtering and search by clicking on button. So, first, I enter a name or age, or both, and then click on the button, and table appears with data that has already been filtered.If all fields are empty and the button is not pressed, then the table should not be visible. The table appears only after clicking. (I'm using database)
Here's my fields for filter and button:
<div class="search">
<?php
echo $form->field($data, 'name')->textInput();
echo $form->field($data, 'age')->textInput();
?>
</div>
<?= Html::submitButton('search'); ?>
And my gridview:
<div class="gridview">
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'age',
]); ?>
</div>
What i need to write in my controller? I don't understand, i really want to get it!!
Thanks everyone for your attention!!
And what about search by click?
To search out of the GridView you should make like this searching form:
<div class="search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($data, 'name') ?>
<?= $form->field($data, 'age') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Hope it helps you get the right direction.
You can get a multidimensional array of search fields in the controller using the method Yii::$app->request->queryParams Or Yii::$app->request->get()
Also to check and not display the table before searching: (or send blank field)
Create your own gridview and search page using the Gii tool (CRUD Generator)
Then add the required codes (for example)
View File
<div class="search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($data, 'name') ?>
<?= $form->field($data, 'age') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php if ($show === true): ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'age',
],
]); ?>
<?php endif ?>
Controller(Index action)
public function actionIndex()
{
$searchModel = new YourModelSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
# code...
// Yii::$app->request->queryParams['YourModelSearch'] Or ...
$get_model = current(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
// Check in whichever way you prefer
'show' => (!empty($get_model['name']) or !empty($get_model['age'])) ? true : false,
]);
}
You can also use JQuery.

Yii2: Search form not working with Gridview in Pjax

There are some questions here, but the problem is in the filters that are in the gridview.
My problem is that I can not integrate an external form with the gridview itself because I do not want to use the search form that is part of the gridview.
Controller
public function actionIndex()
{
$searchModel = new BlogSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Views
index.php
<?= Html::button('Filter', ['data-toggle' => 'modal', 'data-target' => '#filter-modal', 'class' => 'btn btn-primary']) ?>
<?php
Modal::begin([
'header' => '<h3>Search Blog</h3>',
'id' => 'filter-modal'
]);
echo $this->render('_search', ['model' => $searchModel]);
Modal::end();
?>
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'title',
'content'
]
]); ?>
<?php Pjax::end(); ?>
_search.php
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'content') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
In the "index.php" the gridview is inside the pjax, but the search form is outside, but even if the form was within "Pjax :: begin" it would not work, either.
That is, when I do the search the page is reloaded. I want only gridview updatated.
As far as I understood from the discussion you don't want to use the filter fields inside the GridView and want to use the search form instead to filter the GridView. if that is correct you need to do 2 things for that
1. Move your form inside the pjax block
index.php
<?= Html::button('Filter', ['data-toggle' => 'modal', 'data-target' => '#filter-modal', 'class' => 'btn btn-primary']) ?>
<?php Pjax::begin(['enablePushState'=>false]); ?>
<?php
Modal::begin([
'header' => '<h3>Search Blog</h3>',
'id' => 'filter-modal'
]);
echo $this->render('_search', ['model' => $searchModel]);
Modal::end();
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'title',
'content'
]
]); ?>
<?php Pjax::end(); ?>
2. And the second an most important thing is to include the option of data-pjax inside the form options.
_search.php
<?php $form = ActiveForm::begin([
'action' => ['index'],
'id'=>'my-form',
'method' => 'get',
'options' => [
'data-pjax' => 1
],
]); ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'content') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
EDIT
You might find problem with the modal overlay staying there and the modal window itself hides, provide your form with an id like id=>"my-form" and add the following inside your _search.php file to bind the beforeSubmit event for ActiveFormJS
$this->registerJs('$("#my-form").on("beforeSubmit", function (e) {
$("#filter-modal").modal("hide");
});', \yii\web\View::POS_READY);

Yii2 Basic display create from from another model to a view from another model

I create a CRUD called Channel and a CRUD Post, so I want to add create Post form to DetailView of Channel; example when a user view Channel Alpha under the Alpha details he have a form from Post to create a Post inside that Channel
user can view the detail of a Channel and also can add Post to that Channel
something similar to :
in Channel controller
public function actionView($id)
{
$ly_addPost = new Posts();
return $this->render('view', [
'model' => $this->findModel($id),
'addpost' => $ly_addPost,
]);
}
and in channel view I did edit it to :
//Yii2 code
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $model app\models\Channel */
$this->title = $model->Channel_name;
$this->params['breadcrumbs'][] = ['label' => 'Channels', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="channel-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->Channel_id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->Channel_id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<div class="col-md-12">
<?= $this->render ('_form', [
'addpost' => $ly_addPost,
])
?>
<div class="posts-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'Posts_title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'Posts_text')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'Posts_file')->textInput(['maxlength' => true]) ?>
<?php //= $form->field($model, 'Posts_crdate')->textInput() ?>
<?= $form->field($model, 'Channel_id')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'Permissions_id')->textInput() ?>
<?php //= $form->field($model, 'user_id')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
but I get error :
PHP Notice – yii\base\ErrorException
Undefined variable: ly_addPost
Change addpost to ly_addPost show below
public function actionView($id)
{
$ly_addPost = new Posts();
return $this->render('view', [
'model' => $this->findModel($id),
'ly_addPost' => $ly_addPost,
]);
}
Just Change $ly_addPost to $addpost in view file
<div class="col-md-12">
<?= $this->render ('_form', [
'addpost' => $addpost,
])
?>
...

How to reduce column gaps in yii/bootsrap/activeform (Yii2)?

I am using yii/bootrap/active form and I set the layout to Horizontal
How can I reduce a gap between two columns ? I mean, where is the CSS file in yii/bootrap/activeform ?
My view code is like this
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Modal;
use yii\helpers\BaseHtml;
use app\assets\myassets\InputAsset;
?>
<?php
$listData = ['1' => 'Ya', '0' => 'Tidak'];
$button = Html::button('Cancel', ['class' => 'btn btn-default btn-md', 'data' => ['dismiss' => "modal"]]);
$button .= Html::button('Submit', ['id' => 'submit', 'class' => 'btn btn-success success btn-md']);
Modal::begin([
'header' => '<h2>Konfirmasi</h2>',
'toggleButton' => false,
'id' => 'confirm-submit',
'footer' => $button
]);
echo 'Data ini akan disimpan ?';
Modal::end();
?>
<div class="jenis-form">
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'id' => 'marketing',
'action' => ['index'],
'method' => 'get',
'fieldConfig' => [
'horizontalCssClasses' => [
'label' => 'col-sm-2',
'offset' => 'col-sm-offset-2',
'wrapper' => 'col-sm-4',
],
],
]);
?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'jn_byr')->textInput(['readonly' => !$model->isNewRecord,'autofocus'=>$model->isNewRecord,]) ?>
<?= $form->field($model, 'nama')->textInput(['autofocus'=>!$model->isNewRecord,]) ?>
<?= $form->field($model, 'ket_dnd')->textInput() ?>
<?= $form->field($model, 'ket_bat')->textInput() ?>
<?= $form->field($model, 'ket_tts')->textInput() ?>
<?= $form->field($model, 'k_inv')->textInput() ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'ket_battg')->textInput() ?>
<?= $form->field($model, 'ketkw')->textInput() ?>
<?= $form->field($model, 'ketrk')->textInput() ?>
<?= $form->field($model, 'tagih[]')->dropDownList($listData,['prompt'=>'<<< Pilih >>>']); ?>
<?= $form->field($model, 'deposit')->dropDownList($listData,['prompt'=>'<<< Pilih >>>']); ?>
</div>
</div>
<div>
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

Yii2 Basic Background change

Im using bootstrap and Im trying to set a background image for the login screen.I try with div with id that catch all the things and set the background but didnt work .
Here my Login.php
<div id="bimg">
<div class="site-login">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to login:</p>
<?php
$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{label}\n <div class=\"center\"><div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div></div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]);
?>
<?= $form->field($model, 'email')->textInput(['autofocus' => true, 'placeholder' => 'Write your email address']) ?>
<?= $form->field($model, 'password')->passwordInput(['placeholder' => 'Write your password']) ?>
<?=
$form->field($model, 'rememberMe')->checkbox([
'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>",
])
?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<div>
<?= Html::a(Yii::t("app", "Forgotten password") . "?", ["/site/forgot"]) ?>
<br>
</div>
<br>
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
and the class with path in bootstrap.css
#bimg{
width:100%;
background-image: url(../web/assets/css/images/5.jpg) !important;
}
In layouts-main.php
controller->action->id == 'login') ? 'background-wrapper' : NULL ?>">
Check on which page u are , and if u are the page u want set the "class" u want

Categories