how to handle multiple submit buttons in form cakephp3 - cakephp-3.0

I have just started learning cakephp3, so please excuse me for anything wrong.
So I have one form in which I am collecting data, I want to have two submit buttons. One submit button will return to the index page, and another submit button will re-direct me to some other action.
But how to know in controller that which submit button has been clicked?
here is my code
<?= $this->Form->create($User) ?>
<fieldset>
<legend><?= __('Add User') ?></legend>
<?php
echo $this->Form->control('first_name');
echo $this->Form->control('last_name');
echo $this->Form->control('status');
?>
</fieldset>
<?= $this->Form->submit(__('Save & Exit', array('name'=>'btn1'))) ?>
<?= $this->Form->submit(__('Save & Add Educational Details', array('name'=>'btn2'))) ?>
<?= $this->Form->end() ?>
in my controller i have written this
if(isset($this->request->data['btn1'])) {
return $this->redirect(['action' => 'index']);
} else if(isset($this->request->data['btn2'])) {
return $this->redirect(['controller' => 'educationQualifications', 'action' => 'add']);
}
but I am not able to get which button has been clicked..
Please tell how can I achieve this?
Thanks in advance

What ever you did is right but a small mistake, You have to use isset() in if condition as if implicitly not check for isset(). Check the correction below
if(isset($this->request->data['btn1'])) {
return $this->redirect(['action' => 'index']);
} else if(isset($this->request->data['btn2'])) {
return $this->redirect(['controller' => 'educationQualifications', 'action' => 'add']);
}
Or you can try this, you have to give the same name for the buttons.
In your view
<?= $this->Form->submit(('Save & Exit'), array('name'=>'btn')) ?>
<?= $this->Form->submit(('Save & Add Educational Details'), array('name'=>'btn')) ?>
In your controller
if($this->request->data['btn']=='Save & Exit') {
return $this->redirect(['action' => 'index']);
} else if($this->request->data['btn']=='Save & Add Educational Details')) {
return $this->redirect(['controller' => 'educationQualifications', 'action' => 'add']);
}

Related

Yii2 how to format <sup> tags in forms and GridView?

I have an attributeLabels for an attribute that has a <sup> tag:
public function attributeLabels() {
return [
'density' => Yii::t('yii', 'density (kg/dm<sup>3</sup>)'),
];
}
In the view it looks appropriate, but in the form and in the GridView as label it's simply like (kg/dm<sup>3</sup>)
I have tried to add labelOptions with many different format values to it, but no luck.
<?= $form->field($model, 'density', ['labelOptions' => ['format' => '(html/text/raw etc.)']])->textInput() ?>
Is it possible to make it look like a real <sup> (kg/dm3) text in the form, and if yes, can you please tell me how? Thank you.
Call it like that:
<?= $model->getAttributeLabel('density'); ?>
<?= $form->field($model, 'density')->textinput()->label(false) ?>
and after that format your template
Edit (better way):
<?= $form->field($model, 'density', ['labelOptions' => ['label' => $model->getAttributeLabel('density')]])->textinput() ?>

How to display a HTML tag in Yii2 form error summary

I am trying to display a link in error message in login for, but it is not working.
The error message in LoginForm valdiation:
$this->addError($attribute, 'Your account has been disabled. Enable It');
In login.php (view):
<?= $form->errorSummary($model); ?>
I tried like below, but not working:
<?= $form->errorSummary($model,['errorOptions' => ['encode' => false,'class' => 'help-block']]); ?>
I am getting the following output instead of rendered a tag:
You need to disable encoding at ActiveForm level using encodeErrorSummary property, if you want to use $form->errorSummary($model):
<?= $form = ActiveForm::begin([
'id' => 'login-form',
'encodeErrorSummary' => false,
'errorSummaryCssClass' => 'help-block',
]) ?>
<?= $form->errorSummary($model) ?>
Alternatively you may use Html::errorSummary() directly:
<?= Html::errorSummary($model, ['encode' => false]) ?>

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

Implementing Upvote using Pjax

I want to submit data without reloading the page. So I used Pjax. When I click on upvote(views\site\vote.php:), the page reloads and returns an error saying undefined variable: id. I wish to know why and how to solve this problem.I think it is due to id=>$id.
views\site\vote.php:
<?php Pjax::begin(['enablePushState' => false]); ?>
<?= Html::a('upvote', ['site/upvote', 'id'=>$id]) ?>
<?php Pjax::end(); ?>
But with 'id'=>18 instead of 'id'=>$id, it works (submits, page doesn't reload and does not return an error).
<?php Pjax::begin(['enablePushState' => false]); ?>
<?= Html::a('upvote', ['site/upvote', 'id'=>18]) ?>
<?php Pjax::end(); ?>
controllers\SiteController.php:
public function actionVote()
{
$id=18;
return $this->render('vote',[
/* 5 other variables */
'id'=>$id,
]);
}
public function actionUpvote($id)
{
.....
return $this->render('vote');
}

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.