Yii2 Dynamic Form - yii

I'm using wbraganca's dynamicform samples codes for mine own project. My code with its corresponding errors are as follows.
Under the view folder
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use frontend\models\Items;
use frontend\models\Employees;
use frontend\models\Departments;
use dosamigos\datepicker\DatePicker;
use wbraganca\dynamicform\DynamicFormWidget;
/* #var $this yii\web\View */
/* #var $model backend\models\Borrow */
/* #var $form yii\widgets\ActiveForm */
$js = '
jQuery(".dynamicform_wrapper").on("afterInsert", function(e, item) {
jQuery(".dynamicform_wrapper .panel-title-address").each(function(index) {
jQuery(this).html("Items: " + (index + 1))
});
});
jQuery(".dynamicform_wrapper").on("afterDelete", function(e) {
jQuery(".dynamicform_wrapper .panel-title-address").each(function(index) {
jQuery(this).html("Items: " + (index + 1))
});
});
';
$this->registerJs($js);?>
<div class="borrow-form">
<?php $form = ActiveForm::begin(['id'=>'dynamic-form']); ?>
<div class="row">
<div class="col-xs-4">
<?= $form->field($model,'dept_id')->dropDownList(
ArrayHelper::map(Departments::find()->all(),'id','dept_name'),
['prompt'=>'select departments'])
?>
</div>
<div class="col-xs-4">
<?=$form->field($model, 'return_date')->widget(
DatePicker::className(), [
'inline' => false,
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
</div>
<div class="padding-v-md">
<div class="line line-dashed"></div>
</div>
<!-- beginning of dynamic form -->
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 10, // the maximum times, an element can be added (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsAddress[0],
'formId' => 'dynamic-form',
'formFields' => [
'items_id',
'unit',
'request',
'allowed',
],
]); ?>
<div class="panel panel-default">
<div class="panel-heading">
<h4><i class="glyphicon glyphicon-envelope"></i> Items
</h4>
</div>
<div class="panel-body">
<div class="container-items"><!-- widgetBody -->
<?php foreach ($modelsAddress as $i => $modelAddress): ?>
<div class="item panel panel-default"><!-- widgetItem -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Items</h3>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (! $modelAddress->isNewRecord) {
echo Html::activeHiddenInput($modelAddress, "[{$i}]id");
}
?>
<div class="row">
<div class="col-xs-4">
<?= $form->field($modelAddress, "[{$i}]items_id")->dropDownList(
ArrayHelper::map(Items::find()->all(),'id','item_name'),
['prompt'=>'select items']) ?>
</div>
<div class="col-xs-2">
<?= $form->field($modelAddress, "[{$i}]unit")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-xs-2">
<?= $form->field($modelAddress, "[{$i}]request")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-xs-2">
<?= $form->field($modelAddress, "[{$i}]allowed")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-xs-2">
<?= $form->field($modelAddress, "[{$i}]unit_price")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div><!-- .panel -->
<?php DynamicFormWidget::end(); ?>
<!-- end dynamic form-->
<div class="row">
<div class="col-xs-5">
<?= $form->field($model,'emp_id')->dropDownList(
ArrayHelper::map(Employees::find()->all(),'id','emp_name'),
['prompt'=>'select employees'])
?>
<?= $form->field($model,'head_id')->dropDownList(
ArrayHelper::map(Employees::find()->all(),'id','emp_name'),
['prompt'=>'select dept heads'])
?>
<?= $form->field($model,'man_id')->dropDownList(
ArrayHelper::map(Employees::find()->all(),'id','emp_name'),
['prompt'=>'select stoke managers'])
?>
<?= $form->field($model,'keeper_id')->dropDownList(
ArrayHelper::map(Employees::find()->all(),'id','emp_name'),
['prompt'=>'select stoke keepers'])
?>
</div>
<div class="col-xs-5">
<?=$form->field($model, 'emp_date')->widget(
DatePicker::className(), [
'inline' => false,
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
<?=$form->field($model, 'head_date')->widget(
DatePicker::className(), [
'inline' => false,
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
<?=$form->field($model, 'man_date')->widget(
DatePicker::className(), [
'inline' => false,
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
<?=$form->field($model, 'keeper_date')->widget(
DatePicker::className(), [
'inline' => false,
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
The create action under the controller
public function actionCreate()
{
$model = new Borrow();
$modelsAddress = [new Borrow];
if ($model->load(Yii::$app->request->post())) {
$modelsAddress = Model::createMultiple(Borrow::classname());
Model::loadMultiple($modelsAddress, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsAddress) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsAddress as $modelAddress) {
$modelAddress->id = $model->id;
if (! ($flag = $modelAddress->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
return $this->render('create', [
'model' => $model,
'modelsAddress' => (empty($modelsAddress)) ? [new Address] : $modelsAddress
]);
}
The Model Class under the Model Folder
<?php
namespace frontend\models;
use Yii;
use yii\helpers\ArrayHelper;
class Model extends \yii\base\Model
{
/**
* Creates and populates a set of models.
*
* #param string $modelClass
* #param array $multipleModels
* #return array
*/
public static function createMultiple($modelClass, $multipleModels = [])
{
$model = new $modelClass;
$formName = $model->formName();
$post = Yii::$app->request->post($formName);
$models = [];
if (! empty($multipleModels)) {
$keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));
$multipleModels = array_combine($keys, $multipleModels);
}
if ($post && is_array($post)) {
foreach ($post as $i => $borrow) {
if (isset($borrow['id']) && !empty($borrow['id']) && isset($multipleModels[$borrow['id']])) {
$models[] = $multipleModels[$borrow['id']];
} else {
$models[] = new $modelClass;
}
}
}
unset($model, $formName, $post);
return $models;
}
}
It gives the following error when I run my code:
PHP Fatal Error – yii\base\ErrorException Class 'frontend\controllers\Model' not found as shown below.

It looks like you have not namespaced frontend\models\Model in the controller.
Add there at the beginning:
use frontend\models\Model;

Related

Aligning Kartik Sidenav

I can see Kartik Sidenav in my home page. Now I want the sideNav to continue till the bottom of the page.
And the main content be in the right of the sideNav not behind it. Also SideNav is supposed to be collapsible.
I can't see it. Please help. I'm attaching how my pages look like.
My backend/views/layout/main.php is as below
<?php
/* #var $this \yii\web\View */
/* #var $content string */
use backend\assets\AppAsset;
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use common\widgets\Alert;
use kartik\sidenav\SideNav;
use yii\helpers\Url;
AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div class="wrap">
<?php
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
$menuItems[] = [
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
<div class="row">
<div class="col-xs-5 col-sm-4 col-lg-3">
<?php
echo SideNav::widget([
'type' => SideNav::TYPE_DEFAULT,
'heading' => 'Operations',
'items' => [
[
'url' => Yii::$app->homeUrl,
'label' => 'Home',
'icon' => 'home',
],
[
'url' => 'http://localhost:8080/advanced/backend/web/index.php?r=tc/bills',
'label' => 'Insert TC',
'icon' => 'cloud',
],
[
'label' => 'Help',
'icon' => 'question-sign',
'items' => [
['label' => 'About', 'icon'=>'info-sign', 'url'=>'#'],
['label' => 'Contact', 'icon'=>'phone', 'url'=>'#'],
],
],
],
]);
?>
</div>
</div>
<div class="container">
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]) ?>
<?= Alert::widget() ?>
<?= $content ?>
</div>
</div>
<footer class="footer">
<div class="container">
<p class="pull-left">© My Company <?= date('Y') ?></p>
<p class="pull-right"><?= Yii::powered() ?></p>
</div>
</footer>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
If you want the SideNav in the left column you must organiza you page proper inside the container.
Essentially move your SideNav <div class="col-xs-5 col-sm-4 col-lg-3">
inside the <div class="container">
and place the $content inside a remaining column
<div class="col-xs-7 col-sm-8 col-lg-9">
<?= $content ?>
</div>
this way ..
<?php
/* #var $this \yii\web\View */
/* #var $content string */
use backend\assets\AppAsset;
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use common\widgets\Alert;
use kartik\sidenav\SideNav;
use yii\helpers\Url;
AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div class="wrap">
<?php
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
$menuItems[] = [
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
<div class="container">
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]) ?>
<?= Alert::widget() ?>
<div class="col-xs-5 col-sm-4 col-lg-3">
<?php
echo SideNav::widget([
'type' => SideNav::TYPE_DEFAULT,
'heading' => 'Operations',
'items' => [
[
'url' => Yii::$app->homeUrl,
'label' => 'Home',
'icon' => 'home',
],
[
'url' => 'http://localhost:8080/advanced/backend/web/index.php?r=tc/bills',
'label' => 'Insert TC',
'icon' => 'cloud',
],
[
'label' => 'Help',
'icon' => 'question-sign',
'items' => [
['label' => 'About', 'icon'=>'info-sign', 'url'=>'#'],
['label' => 'Contact', 'icon'=>'phone', 'url'=>'#'],
],
],
],
]);
?>
</div>
<div class="col-xs-7 col-sm-8 col-lg-9">
<?= $content ?>
</div>
</div>
If you need a wide container you can simply change the class container this way <div class="container-fluid">
for reduce the left spacing of the SideNav you can set an in tag styling for the div containing the SideNav
`<div class="col-xs-5 col-sm-4 col-lg-3" style="padding-left: 0px;">
`

Bootstrap3 Modal not working with GridView's pjax

I have a following GridView, but unfortunately Bootstrap modal isn't working properly if I have used Pjax pagination or search. Well, the modal does show up, but it won't submit the form. And when I leave the modal out of Pjax(), then it isn't loaded at all.
Pjax::begin(['id' => 'pjax_id', 'timeout' => false, 'enablePushState' => false]);
echo GridView::widget([
'dataProvider' => $details,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['format' => 'raw',
'label' => 'Category',
'value' => function ($data) {
$string = '';
foreach ($data['Category'] as $category) {
$string .= $category->category_name . '<br/>';
}
return $string;
}],
'book_name',
['format' => 'raw',
'contentOptions' => ['class' => 'form-cell'],
'label' => '',
'value' => function ($data) {
$buttons = Html::button('Edit', ['class' => 'btn btn-default mleft modallink', 'data-toggle' => 'modal', 'data-target' => '#'.$data->cat_id]);
return $buttons;
}
],
[
'format' => 'raw',
'label' => '',
'value' => function ($data) {
if (Yii::$app->user->identity->user_type == 'admin') {
$string = '</span>';
$string .= '<a class=\'mleft\' href=\''.Url::to(['category/update', 'id' => $data->cat_id]).'\'><span class="glyphicon glyphicon-pencil"></span></a>';
return $string;
}
}
],
],
]);
$details = $details->getModels();
foreach ($details as $detail) {
?>
<div class="modal fade" id="<?= $detail->cat_id ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><?= $detail->category_name ?></h4>
</div>
<div class="modal-body">
<?php
echo Html::beginForm(Url::to(['category/details', 'id' => $detail->cat_id]), 'post', ['id' => 'popup']);
echo Html::beginTag('div', ['class' => 'form-group']);
echo Html::activeLabel($detail, 'cat_description');
echo Html::activeTextarea($detail, 'cat_description', ['class' => 'form-control']);
echo Html::endTag('div');
echo Html::beginTag('div', ['class' => 'form-group']);
echo Html::activeLabel($detail, 'link');
echo Html::activeInput('text', $detail, 'link', ['class' => 'form-control']);
echo Html::endTag('div');
echo Html::beginTag('div', ['class' => 'form-group']);
echo Html::activeLabel($detail, 'resource');
echo Html::activeInput('text', $detail, 'resource', ['class' => 'form-control']);
echo Html::endTag('div');
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<?= Html::submitButton('Submit', ['class' => 'btn btn-success']) ?>
<?= Html::endForm(); ?>
</div>
</div>
</div>
</div>
<?php
}
Pjax::end();
So how do I keep GridView's Pjax functionality so that the modal still works?
Pjax widget has a formSelector attribute that, by default, will find all forms with data-pjax attribute set to something not falsy.
The easiest way to fix your form is to add that attribute to your form definition:
echo Html::beginForm(
Url::to(['category/details', 'id' => $detail->cat_id]),
'post',
[
'id' => 'popup',
'data-pjax' => 1,
]
);

Yii 2.0 hidden field value not modifiable using javascript

In my model I have a hidden field.I am modifying its value using javascript (jQuery).But when I submit the form to model, it still contains its initial value instead of values modified via javascript.I have checked whether javascript is modifying the values, and is confirmed that values are being modified.But when form is submitted it is sending initial values.
Code is given below
<?php
/*Model -- wrong_answers is my hidden field*/
namespace app\models;
use yii;
use yii\db;
use yii\base\Model;
use yii\web\UploadedFile;
class QuestionModel extends Model
{
public $quiz_id = null;
public $question = '';
public $choices = '';
public $explanation ="";
public $hints = array();
public $correct_answer = "";
public $wrong_answers = '{}';//when populating from already entered data use {"a":"as"} format instead of {'a':'as'}
public $wrong_answer = "";
public function rules()
{
return [
[['quiz_id'], 'safe'] ,
[['question'], 'required'] ,
[['correct_answer'], 'required'] ,
[['wrong_answers'], 'safe'] ,
[['wrong_answer'], 'safe'] ,
[['explanation'],'required']
];
}
public function get_quiz($quiz_id) {
$ret = array();
$command = Yii::$app->db->createCommand("CALL get_quiz(:qz_id)");
$command->bindParam(":qz_id", $this->quiz_id);
$result = $command->queryAll();
if(sizeof($result) > 0) {
$ret = $result[0] ;
}
return $ret;
}
public function save_question() {
$ret = false;
//echo "quiz- id is ".$this->quiz_id;
$quiz_details = $this->get_quiz($this->quiz_id);
if(sizeof($quiz_details)) {
$quiz_data = $quiz_details['quiz_data'];
$quiz_data = json_decode($quiz_data);
$choices = [];
echo $this->wrong_answers;
exit;
$wrong_answers_array = json_decode($this->wrong_answers);
if($this->wrong_answer)
array_push($wrong_answers_array, $this->wrong_answer);
print_r($wrong_answers_array);
exit;
foreach($wrong_answers_array as $wr_ans) {
array_push($choices,array('choice'=>$wr_ans));
}
$question_data = array('question'=>$this->question,'explanation'=>$this->explanation,'choices'=>$choices);
$ret = false;
}
return $ret;
}
public function getHint($key) {
return $this->hints[$key];
}
}
Controller - actionAdd_question is the action
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\QuizModel;
use app\models\QuestionModel;
use yii\web\UploadedFile;
global $username;
class QuizController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['#'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionEntry()
{
}
public function actionView_all() {
$request = Yii::$app->request;
$layout_params=array(
'username'=>'admin',
'sidebar_menu1_class' =>'active',
'sidebar_menu12_class' =>'active',
'dash_title' => 'Courses',
'dash_sub_title'=>'View all'
);
$message = '';
$data = array();
$layout_params['scripts'] = array("view-all-modules.js");
/*$model = new ViewAllCompaniesModel();
$pagination_data = $model->getTotalCompanies();
$layout_params['js_variables'] = array(
'totalCompanyPages'=>$pagination_data['total_page_count'],
'csrfToken' => "'".$request->csrfToken."'" ,
'csrfTokenName' => "'".$request->csrfParam."'"
);
*/
return $this->render('view-all-modules', [
'layout_params'=>$layout_params,
'message' =>$message,
'data' =>$data
]);
}
public function actionAdd() {
$layout_params=array(
'username'=>'admin',
'sidebar_menu1_class' =>'active',
'sidebar_menu12_class' =>'active',
'dash_title' => 'Quiz',
'dash_sub_title'=>'Add new quiz'
);
$message = array();
$request = Yii::$app->request;
$proceed = true;
$parent_course_id = null;
$req_params = $_POST;
$layout_params['scripts'] = array("add-quiz.js");
$model = new QuizModel();
if(!$model->quiz_id) {
if ($model->load($request->post()) && $model->validate()) {
$params = $request->post();
$model->save_quiz();
$message['title'] = 'Wow !';
$message['body'] = 'Successfully added quiz '.$model->quiz_title;
} else {
}
} else {
}
return $this->render('add-quiz', ['model' => $model,
'layout_params'=>$layout_params,
'message' =>$message
]);
//return $this->render('add-company',$data);
}
public function actionSave() {
//print_r($_POST);
}
public function actionAdd_question() {
print_r($_POST);
$layout_params=array(
'username'=>'admin',
'sidebar_menu1_class' =>'active',
'sidebar_menu12_class' =>'active',
'dash_title' => 'Quiz',
'dash_sub_title'=>'Add question'
);
$request = Yii::$app->request;
$message = array();
$layout_params['scripts'] = array("add-question.js");
$model = new QuestionModel();
if($request->get('q_id')) {
$model->quiz_id = $request->get('q_id');
if ($model->load($request->post()) && $model->validate()) {
$model->save_question();
} else {
echo "yo yo";
}
return $this->render('add-question', ['model' => $model,
'layout_params'=>$layout_params,
'message' =>$message
]);
} else {
}
}
public function actionIndex()
{
$data = array(
'layout_params'=>array(
'username'=>'admin',
'sidebar_menu11_class' =>'active'
)
);//
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
}
View
/*Assigning the parameters to be accessible by layouts*/
foreach($layout_params as $layout_param => $value) {
$this->params[$layout_param] = $value;
}
?>
<div class="form-group">
</div>
<div class="col-md-12">
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Add Question</h3>
</div><!-- /.box-header -->
<!-- form start -->
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<input type = 'hidden' id = 'test' name = 'test' />
<div class="box-body">
<?php if(isset($message)&&sizeof($message)): ?>
<div class="form-group">
<div class="callout callout-info alert-dismissible">
<h4><?php if(isset($message['title']))echo $message['title'];?></h4>
<p>
<?php if(isset($message['body']))echo $message['body'];?>
</p>
</div>
</div>
<?php endif;?>
<div class="form-group">
<?= $form->field($model, 'question')->textInput(array('class'=>'form-control')); ?>
</div>
<div class="form-group">
<?= $form->field($model, 'correct_answer')->textInput(array("class"=>"form-control",'id'=>'correct_answer')); ?>
<?= Html::activeHiddenInput($model, 'wrong_answers',array('id'=>'wrong_answers')) ;?>
<?= Html::activeHiddenInput($model, 'quiz_id',array('id'=>'quiz_id')) ;?>
</div>
<div class="form-group">
<?= $form->field($model, 'wrong_answer')->textInput(array("class"=>"form-control ",'id'=>'wrong_answer')); ?>
<button id = 'add-answer' type = "button">Add Another</button>
<?= Html::activeHiddenInput($model, 'wrong_answers',array('id'=>'wrong_answers')) ;?>
<?= Html::activeHiddenInput($model, 'quiz_id',array('id'=>'quiz_id')) ;?>
</div>
<div class="form-group" id ='wrong-group'>
<h3>Wrong Answers</h3>
<!-- The following dummy html is used for generating html inside javascript -->
<ul class="edit-list-dummy" id ="edit-list-dummy">
<li> <button class="btn btn-danger" id = 'dummy-wrong-ans-delete' onClick = "deleteWrongAnswer(this.name)" type ="button">Delete</button> <span id ='dummy-wrong-answer'>Hello I am a wrong answer here</span>
</ul>
<ul class="edit-list" id = 'wrong-answers'>
</ul>
</div>
<div class="form-group">
<?= $form->field($model, 'explanation')->textArea(array("class"=>"form-control",'id'=>'explanation')); ?>
</div>
</div><!-- /.box-body --> <div class="box-footer">
<?= Html::submitButton('Save Question', ['class' => 'btn btn-primary','onclick'=>'this.preventDefault']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
Javascript -#wrong_answers is my hidden input id
var wrongAnswers = {};//{1:answer1,2:answer2...};
var randCount = 0;//radom count for setting as wrongAnswers attribute
//initialise wrong answers json from database
$(document).ready(function() {
console.log($('#wrong_answers').val());
wrongAnswers = jQuery.parseJSON($('#wrong_answers').val());
console.log(wrongAnswers);
$('#add-answer').click(function() {
var wrongAns = $('#wrong_answer').val().trim();
if(wrongAns) {
wrongAnswers[randCount] = wrongAns;
randCount++;
generateWrongAnswers();
$('#wrong_answer').val("");
// alert($('#wrong_answers').val());
}
});
generateWrongAnswers();
});
function generateWrongAnswers() {
$('#wrong-group').hide();
var html = '';
for(var wrongIdx in wrongAnswers) {
//console.log("w is ".wrongAnsIdx);
$('#dummy-wrong-answer').html(wrongAnswers[wrongIdx]);
$('#dummy-wrong-ans-delete').attr('name',wrongIdx);
html += $('#edit-list-dummy').html();
}
if(html) {
$('#wrong-group').show();
$('#wrong-answers').html(html);
}
var jsonData = JSON.stringify(wrongAnswers);
$('#wrong_answers').val(jsonData);
// $('#test').val(jsonData);
//$('#correct_answer').val("Mallu");
// alert($('#wrong_answers').val());
}
function deleteWrongAnswer(idx) {
delete wrongAnswers[idx];
generateWrongAnswers() ;
}
Finall solved it. The issue is that, I accidentally included two hidden input fields with same id.Removed one and solved

How create file input at form, which upload file and write link in database

I'm trying to learn Yii2 by writing my own cms.
I want realize attachment photo for shop items. I don't know right way to do this, but i think it's should be so -
On save, files which was selected in multiple file input are uploads to server.
Get the url by each photo
Write links in database cell by template
<div class="itemImgs">
<img class="0" src="{link}"> <!-- first -->
<img class="1" src="{link}"> <!-- second -->
<img class="..." src="{link}"> <!-- ... -->
</div>
Please, help me understand, what i must write in model and\or controller, if its right way. Else, please tell me how should to do it.
Thanks.
UPD
Action in controller:
public function actionCreate() {
$model = new ShopItems();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'category' => ShopCategories::find()->all()
]);
}
}
Model contain this functions:
public function behaviors() {
return [
TimestampBehavior::className(),
];
}
/** #inheritdoc */
public static function tableName() {return 'shop_items';}
/** #inheritdoc */
public function rules() {
return [
[['category_id', 'title', 'desc', 'price', ], 'required'],
[['category_id', 'price', 'in_stock', 'discount',], 'integer'],
[['desc', 'options',], 'string'],
[['photos',], 'file', 'maxFiles' => 10],
[['title'], 'string', 'max' => 100]
];
}
/** #inheritdoc */
public function attributeLabels() {
return [
'id' => 'ID',
'category_id' => 'Категория',
'title' => 'Название',
'desc' => 'Описание',
'price' => 'Цена',
'discount' => 'Скидка %',
'in_stock' => 'На складе',
'photos' => 'Фото',
'options' => 'Опции',
'created_at' => 'Дата добавления',
'updated_at' => 'Последнее редактирование',
];
}
And the view _form.php:
<?php $form = ActiveForm::begin(); ?>
<div class="col-lg-6">
<?= $form->field($model, 'title')->textInput(['maxlength' => 100]) ?>
<?= $form->field($model, 'desc')->textarea(['class' => 'ckeditor',]); ?>
</div>
<div class="col-lg-6">
<?= $form->field($model, 'category_id')->dropDownList(
ArrayHelper::map($category, 'category_id', 'category_name')
) ?>
<?= $form->field($model, 'price')->textInput() ?>
<?= $form->field($model, 'discount')->textInput() ?>
<?= $form->field($model, 'in_stock')->textInput() ?>
<?= $form->field($model, 'photos[]')->fileInput(['multiple' => true]) ?>
<?= $form->field($model, 'options')->textInput() ?>
</div>
<div class="clearfix"></div>
<div class="col-xs-12">
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Добавить' : 'Принять изменения', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
See documentation - http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html#uploading-multiple-files
In controller:
if ($model->file && $model->validate()) {
foreach ($model->file as $file) {
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
}
Input model Photo:
if ($model->file && $model->validate()) {
foreach ($model->file as $file) {
$path = 'uploads/' . $file->baseName . '.' . $file->extension;
$file->saveAs($path);
$photo = new Photo();
$photo->path = $path;
$photo->save();
}
}
Photo table like that:
CREATE TABLE `photo` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`path` VARCHAR(255) NOT NULL ,
`status` TINYINT(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
ENGINE=InnoDB;
Get photos use:
$photos = Photo::find()->all();
In view:
<div class="itemImgs">
<?php foreach ($photos as $k=>$photo) ?>
<img class="<?= $k ?>" src="<?= $photo->path ?>"/><br/>
<?php } ?>
</div>
EDIT
Set in controller
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$files = UploadedFile::getInstances($model, 'file');
$photosArr = [];
foreach ($files as $file) {
$path = 'uploads/' . $file->baseName . '.' . $file->extension;
$file->saveAs($path);
$photosArr[] = $path;
}
$model->photos = Json::encode($photosArr);// better put json in afterSave
$model->save(false);// because validate() run before
return $this->redirect(['view', 'id' => $model->id]);
}
In field photos you get JSON with path for uploaded photos. In view:
$arr = Json::decode($model->photos); // better put in afterFind()
foreach ($arr as $path) {
echo '<img src="'.$path.'">';
}

yii modal dialog login

I've been looking all over the internet and I can't seem to find the solution to just a simple login via header. I tried all sorts of way. The problem I have right now just reloads every time i hit login. It's only when I go to ?r=site/login it'll let me login. Is there some kind of controller for headers!??! Not sure what is wrong anymore.
Right now i have this in my header:
<?php $this->beginWidget(
'bootstrap.widgets.TbModal',
array('id' => 'login')
); ?>
<?php
$model=new LoginForm;
$this->renderPartial('//site/login', array('model'=>$model));
?>
<?php $this->endWidget(); ?>
//stuff here
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'Signin',
'type' => 'warning',
'htmlOptions' => array(
'data-toggle' => 'modal',
'data-target' => '#login',
),
)
);
?>
And then in site/login:
<div class="spacing">
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<div class="form" id="login-form">
<div class="login-container">
<div class="login-header">
<h1 title="login">Sign In</h1>
</div>
<div class="login-form">
<div class="input-position">
<?php echo $form->labelEx($model,'Email',array('class'=>'span3','style'=>'text-align:left')); ?>
<?php echo $form->textField($model,'email', array('class'=>'span3')); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="input-position">
<?php echo $form->labelEx($model,'password',array('class'=>'span3','style'=>'text-align:left')); ?>
<?php echo $form->passwordField($model,'password',array('class'=>'span3',)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="input-position ">
<div class="rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Login'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
</div>
</div>
</div>
</div>
controller
public function actionLogin()
{
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('login',array('model'=>$model));
}
As per my understanding, please keep your login script inside some loop like this to escape it from reloading every time.
if(Yii::app()->user->isGuest and (Yii::app()->controller->id=='site' and Yii::app()->controller->action->id = 'login')){
$this->beginWidget('bootstrap.widgets.TbModal', array('id' => 'login'));
---
$this->endWidget();
}else{
// keep some log out or other script here.
}
Here is the login what i have implemented on Modal login style:
Component/Header:
<?php
Yii::import('zii.widgets.CPortlet');
class Header extends CPortlet
{
protected function renderContent()
{
$this->render('Header');
}
}
Components/view/Header.php
<?php if(Yii::app()->user->isGuest){?>
<?php echo TbHtml::link('<i class="sign-in"></i>Sign in', '#', array(
'data-toggle' => 'modal',
'data-target' => '#loginModal',
));?>
<?php } else{ echo 'Hi!'.$user; } ?>
In View/Layout/Main.php
<?php
$model = new LoginForm;
$this->widget('bootstrap.widgets.TbModal', array(
'id' => 'loginModal',
'header' => '<h3 id="loginModal">Sign In</h3>',
'content'=> $this->renderPartial('userlogin',array('model'=>$model),true),
'remote' => $this->createUrl('site/userlogin'),
'onShow'=>'function(){$("#login-form")[0].reset(); }',
'show'=>false,)); ?>
View/Site/Userlogin.php
<?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'login-form',
//'helpType'=>'help-none',
'action'=>Yii::app()->createUrl('userlogin'),
//'enableAjaxValidation' => false,
'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange'=>false,
'afterValidate' => 'js:function(form, data, hasError) {
if (!hasError){
str = $("#login-form").serialize() + "&ajax=login-form";
$.ajax({
type: "POST",
url: "' . Yii::app()->createUrl('site/userlogin') . '",
data: str,
dataType: "json",
beforeSend : function() {
$("#login").attr("disabled",true);
},
success: function(data, status) {
if(data.authenticated)
{
window.location = data.redirectUrl;
}
else
{
$.each(data, function(key, value) {
var div = "#"+key+"_em_";
$(div).text(value);
$(div).show();
});
$("#login").attr("disabled",false);
}
},
});
return false;
}
}',
),
));?>
<?php echo $form->textFieldControlGroup($model,'username',array('span'=>5,'maxlength'=>255,'required'=>true)); ?>
<?php echo $form->passwordFieldControlGroup($model,'password',array('span'=>5,'maxlength'=>255,'required'=>true)); ?>
<?php echo TbHtml::submitbutton('Sign In',array('class'=>'btn btn-primary')); ?>
<?php $this->endWidget(); ?>
This will Authenticate via Ajax
SiteController:
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
$errors = CActiveForm::validate($model);
if ($errors != '[]')
{
echo $errors;
Yii::app()->end();
}
}
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if ($model->validate() && $model->login()) {
if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
$user_id = Yii::app()->user->id;
$arr = array();
$record = Users::model()->findByPk($user_id);
if ($record) {
echo CJSON::encode(array( 'authenticated' => true,
'redirectUrl' => CController::createUrl('users/trainerprofile'),
"id" => $record->id,));
Yii::app()->end();
}
}