Strategi must be a string - yii2

please help me..
when data input arises such problems "Strategi must be a string."
this is my controller :
$isistrategi = $_POST['FormNarasi'];
$fn = FormNarasi::find()->where([
'kriteria_id' => $model->id,
'form_spmi_id' => $formSpmi->id,
])->one();
if(empty($fn))
$fn = new FormNarasi;
$fn->kriteria_id = $model->id;
$fn->form_spmi_id = $formSpmi->id;
$fn->strategi = $isistrategi;
this is my _form :
<?php
$fn = FormNarasi::find()->where([
'kriteria_id' => $model->id,
'form_spmi_id' => $formSpmi->id
])->one();
echo $form->field($fn, 'strategi')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'advance'
])
?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success','value'=>'1','name'=>'btn-submit']) ?>
</div>
<?php ActiveForm::end(); ?>
please help, master

Here you are assigning an array to the model
$isistrategi = $_POST['FormNarasi'];
...
$fn->strategi = $isistrategi; // HERE
and in the following code you are accessing it. There is an array assigned. So you should assign there a string (the content of the CKEditor)
echo $form->field($fn, 'strategi')->widget(CKEditor::className(), [ // HERE
...
])
As #Sfili_81 mentioned, do not access the $_POST directly and rather use $model->load(Yii::$app->request->post())

Related

Yii2 Pagination with Tabs

I have 5 tabs on one page. All the tabs have different content, but on one of them i need to have pagination. When click on pagination the page is refreshing and the current opened tab is closed and show by default first tab ... I want when click on pagination, the current tab to be open and the refresh only part with data information.
here is my code:
<?php
Pjax::begin([
'id' => 'w0',
'enablePushState' => true, // I would like the browser to change link
'timeout' => 10000 // Timeout needed
]);
$spec = Specifications::find()->where('active = 1')->orderBy(['sort' => SORT_ASC]);
$count = $spec->count();
$pagination = new Pagination(['totalCount' => $count, 'defaultPageSize' => 20]);
$models = $spec->offset($pagination->offset)
->limit($pagination->limit)
->all();
echo LinkPager::widget([
'pagination' => $pagination,
'hideOnSinglePage' => true,
'prevPageLabel' => 'Предишна',
'nextPageLabel' => 'Следваща'
]);
if ($spec) { ?>
<div class="form-group">
<label>Спецификации</label></br>
<?php
foreach ($models as $singleSpec) {
echo $singleSpec->id." ".$singleSpec->title;
}
?>
</div>
<?php } ?>
<?php Pjax::end() ?>
remove 'id'=>'w0' from Pjax, it is refreshing your page

Yii2 How to Use Html::tag or Html::beginTag

i want to echo like this
<li>
<img src="img/h4-slide.png" alt="Slide">
<div class="caption-group">
<h2 class="caption title">some_title
</h2>
<h4 class="caption subtitle">Dual SIM</h4>
<a class="caption button-radius" href="some_route"><span class="icon"></span>check</a>
</div>
</li>
here my code for render image carousel :
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => Html::img(Yii::$app->urlManager->baseUrl . '/uploads/slide/' . $slide->id . '.jpg'),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
And my carousel :
<div class="slider-area">
<div class="block-slider block-slider4">
<?= Carousel::widget([
'id' => 'bxlider-home',
'items' => $slides,
'options' => [
'class' => 'slide',
'data-interval' => 3000,
],
'controls' => [
'<span class="bx-next fa fa-angle-left"></span>',
'<span class="bx-prev fa fa-angle-right"></span>',
],
]) ?>
</div>
</div>
how to Slide->title, slide->body, and some links can be in class caption-group ?
I think it would be better to create new partial file.
Create a new file called _slider.php
Call $this->render('_slider') inside configuration of the slider. Please check below code.
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => $this->render("_slider"),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
You can write html inside _slider.php easily now. Don't need to use Html::beginTag() etc.
Generating Tags
The code generating a tag looks like the following:
<?= Html::tag('p', Html::encode($user->name), ['class' => 'username']) ?>
<p class="username">samdark</p>
$options = ['class' => ['btn', 'btn-default']];
echo Html::tag('div', 'Save', $options);
// renders '<div class="btn btn-default">Save</div>'
Hyperlinks:
<?= Html::a('Profile', ['user/view', 'id' => $id], ['class' => 'profile-link']) ?>
Images:
<?= Html::img('#web/images/logo.png', ['alt' => 'My logo']) ?>
generates
<img src="http://example.com/images/logo.png" alt="My logo" />
Lists:
<?= Html::ul($posts, ['item' => function($item, $index) {
return Html::tag(
'li',
$this->render('post', ['item' => $item]),
['class' => 'post']
);
}]) ?>
Even more?
visit

Yii2-user, dektrium-yii2-user, Yii2 Populate a dropdown in yii2

I am pretty sure that there is a better way to populate the array, needed for the dropdown:
<?php
$items2 = [Yii::$app->user->identity->id => Yii::$app->user->identity->username ]; ?>
<!--...some html -->
<?= $form->field($model, 'idUser')->dropDownList($items2,['Item' => ''])?>
already try:
$item2 = ArrayHelper::map(Yii::$app->user->identity::find()->all(), 'id', 'name');
reason, I want to display 'name' but submit 'value'='id'.
Should be this
<?= $form->field($model, 'idUser')->
dropDownList(ArrayHelper::map(Yii::$app->user->identity->find()->all(),
'id', 'username'), ['prompt'=>'Select...'])?>

Insert data inner join Cakephp 3.0

I'm new to CakePHP, I just finished the blog/article tutorial and now I am trying to add the comment system to it.
In my comments table I have, Id, article_Id, name, email, text and created. In my articles table I have Id, user_id, title, text, created.
I'm trying to add the comment according to the article ID by using Insert Inner join Query. So let's say a user views a specific article like /view/1, before he add comments, the article ID is already passed so he can comment accordingly to that specific article ID he is viewing. How can I do that? I hope I'm clear.
This is my add function for my comment:
public function leavemessage()
{
$comment = $this->Comments->newEntity();
$articles = $this->loadModel('Articles');
$leavemessage = $articles->find()
->hydrate(false)
->join([
'table' => 'comments',
'alias' => 'c',
'type' => 'LEFT',
'conditions' => 'c.article_id = articles.id',
])
->insert(['id'])
->where(['c.article_id = articles.id'])
->execute();
if ($this->request->is('post')) {
$comment = $this->Comments->patchEntity($comment, $this->request->data);
if ($this->Comments->save($comment)) {
$this->Flash->success('The comment has been saved.');
return $this->redirect([
'controller' => 'Articles',
'action' => 'index',
'c.article_id'
]);
} else {
$this->Flash->error('The comment could not be saved. Please, try again.');
}
}
$articles = $this->Comments->Articles->find('list', ['limit' => 200]);
$this->set(compact('comment', 'articles'));
$this->set('_serialize', ['comment']);
}
My add comments view
<div class="comments form large-10 medium-9 columns">
<?= $this->Form->create($comment); ?>
<fieldset>
<legend><?= __('Add Comment') ?></legend>
<?php
echo $this->Form->input('article_id', ['value' => $articleId]);
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('text');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

Yii2: getting error while page load '$model not defined'

I have developed form to allow owner to create team. code is:
<?php $form = ActiveForm::begin(['id' => 'team-create-form', 'action' => ['site/create-team-form'], 'options' => array('role' => 'form')]);
<div class="col-lg-10 form-group" id="createTeamForm" style="margin-top: 15px;">
<div class="col-lg-4">
<?= $form->field($model, 'team_name',['template' => "{label}\t{input}\n{error}"])->textInput(array('placeholder'=>'Enter team name....')); ?>
</div>
<div class="col-lg-4">
<?= $form->field($model, 'team_description',['template' => "{label}\t{input}\n{error}"])->textInput(array('placeholder'=>'Enter team Description....')); ?>
</div>
<div class="col-lg-2">
<?= Html::submitButton('Submit', ['class' => 'btn btn-danger', 'id' => 'tsubmit', 'style' => 'margin-top: 22.5px; margin-right: 15px;']) ?>
</div>
</div>
I have tried loading the page with the above code but it is showing me error "$model not defined". How to resolve that. Am i need to add something in the main-local.php???
public function actionLogin()
{
$model = new LoginForm();
$session = Yii::$app->session;
if ($model->load(Yii::$app->request->post()) && $model->login()) {
$collection1 = Yii::$app->mongodb->getCollection('users');
$teamid = $collection1->findOne(array('username' => $model->email_address));
$session->set('id', $teamid['_id']);
$session->set('name', $teamid['name']);
$session->set('username', $model->email_address);
$collection2 = Yii::$app->mongodb->getCollection('teamdashboard');
$teams = $collection2->find(array('admin' => $model->email_address));
$model1 = new TeamCreateForm();
return $this->render('dashboard', ['model'=>$model1, 'teams'=> $teams]);
} elseif($session->isActive){
$username = $session->get('username');
$collection = Yii::$app->mongodb->getCollection('users');
$teams = $collection->findOne(array('username' => $username));
return $this->render('dashboard', ['teams'=>$teams]);
}else{
$this->layout = 'index';
return $this->render('login', ['model' => $model]);
}
}
I have renamed the productpage as dashboard for better understanding.
Now when i run this & logs in, The address bar url shows url:..../web/index.php?r=site/login whereas it should show me url:..../web/index.php?r=site/dashboard & shows me the view of dashboard.
When i refresh the page, i brings me back to the login...
Did you use $model in dashboard view? If you do - you need to pass it (the same way as the login).
You have to send the $model to the view. The view only knows variables if you send it to it.
I have no idea what you mean with the address bar. The address bar has nothing to do with what you send to the view.
EDIT
Your entire way of thinking is strange. Why would u show different views depending if the person is registered or not?
return $this->render('dashboard', ['teams'=>$teams]);
return $this->render('login', ['model' => $model]);
User redirect with parameters to move the customer to another page. Having an URL like /login that actually shows a dashboard is not logical.