Access controller from button in Yii 2 - php

In my Yii 2 application, I'm trying to make download button.
Here's my download controller:
public function actionDownload()
{
$path = 'templates/file';
if(file_exists($path)){
Yii::$app->response->sendFile($path);
}
}
My button:
<?= Html::a('Download Form', ['class' => 'btn btn-success']) ?>
Is there a way to access the function actionDownload() from my button?

Try:
<?= Html::a('Download Form', ['yourControllerName/download'], ['class' => 'btn btn-success']) ?>

Related

Yii2 Using If else in Views _Form

I Have 2 textinputs called Address and Mail_address
I use Jquery so when I input some texts in Address, Mail_address has exactly same texts with Address.
Now I want to add 1 checkbox so when I click that checkbox,
Mail_address has become disabled or readonly and also it has same value as Address
But How do I implement if else for the checkbox in Jquery or in _form ?
Thank You
Here's my views _Form
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\bootstrap\Modal;
use yii\helpers\BaseHtml;
use app\assets\myassets\InputAsset;
?>
<?php
$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 'Save ?';
Modal::end();
?>
<?php
$this->registerJs('
$(document).ready(function () {
$(document).on("change" ,"#'. Html::getInputId($model ,'Address') .'" ,function(){
$("#'. Html::getInputId($model ,'Mail_Address') .'").val();
var first = $("#'. Html::getInputId($model ,'Address') .'").val();
var third = first;
$("#'. Html::getInputId($model ,'Mail_Address') .'").val(third);
});
});
');
?>
<div class="tcust-form">
<?php $form = ActiveForm::begin([
'id' => 'marketing',
]);
?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'Address')->textInput() ?>
<?= $form->field($model, 'Mail_Address')->textInput() ?>
<?= $form->field($model, 'SameasAbove')->checkbox() ?>
</div>
</div>
<div class="form-group">
<?= 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>
all you need to do is to write a change function for checkbox like
$('#checkboxID').on('change',function(){
if($('#checkboxID).prop('checked'))
{
var address = $('#addressID').val();
$('#mailAddress').val(address).addClass('disabled');
}
else
{
if($('#checkboxID).hasClass('disabled')
{
$('#mailAddress).removeClass('disabled');
}
}
})

PHP & Yii buttons implementation

I need to implement a button created in a view. In other programming languages this is very easy: your button has an ID so you can make a reference to it in a controller to implement its action. But in PHP I see like there are some predefined buttons (ex, submitbutton) and I don't understand how can you link an action with a button.
If someone could help me it would be very nice!
First you create an Action in your controller and then in your view try this:
<?= Html::a('YourFormName', ['yourControllerName/yourActionName'], ['class' => 'btn btn-success']) ?>
In an ActiveForm
<?php $form = ActiveForm::begin(); ?>
<div class="form-group">
<?= Html::submitButton('Button caption', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
This is the code for submit button
<?= Html::submitButton('Button Name',['class'=>'btn btn-success'])?>
if you want to make button from link here is the code
<?= Html:a('Caption',['controller/action'],['class'=>'btn btn-success'])?>
if you want to pass some query string in link the her is the code
<?= Html::a('caption',['controller/action','id'=>$model->id],['class'=>'btn btn-success'])?>

Pjax update multiple blocks Yii2

I learn Yii2 and I decided to get acquainted with the work of technology Pjax on site: http://blog.neattutorials.com/yii2-pjax-tutorial/. There is example "Multiple blocks". But it is implemented as a example version, and is not quite correct. It is written below. In this example in one action actionMultiple calculating string and key in one place, but it must be relised into different actions. So I decided to do it right but collided with the problem that when I click on the link it redirect me to a new page with the generation of a string or key. I need to do it in same page without reloading.
Controller:
public function actionMultiple()
{
$security = new Security();
$randomString = $security->generateRandomString();
$randomKey = $security->generateRandomKey();
return $this->render('multiple', [
'randomString' => $randomString,
'randomKey' => $randomKey,
]);
}
public function actionString()
{
$security = new Security();
$randomString= $security->generateRandomString();
return $this->render('_randomString', [
'randomString' => $randomString,
]);
}
public function actionKey()
{
$security = new Security();
$randomKey = $security->generateRandomKey();
return $this->render('_randomKey', [
'randomKey' => $randomKey,
]);
}
view multiple:
<?php
use yii\widgets\Pjax;
use yii\bootstrap\Html;
?>
<div class="col-sm-12 col-md-6">
<?php Pjax::begin(); ?>
<?= Html::a("Generate Random String", ['site/string'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomString ?></h3>
<?php Pjax::end(); ?>
</div>
<div class="col-sm-12 col-md-6">
<?php Pjax::begin(); ?>
<?= Html::a("Generate Random Key", ['site/key'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomKey ?><h3>
<?php Pjax::end(); ?>
</div>
view _randomString:
<?php
use yii\helpers\Html;
?>
<?= Html::a("Generate Random String", ['site/string'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomString ?></h3>
view _randomKey:
<?php
use yii\helpers\Html;
?>
<?= Html::a("Generate Random Kay", ['site/key'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomKey ?><h3>
Please tell me what I'm doing wrong.

How to create form in view from different model in yii2?

Iam new to yii. I am developing customer project app. I have a view wherein iam displaying the data from both the models, customer and projects.
How do i create a form to add new projects?
my project is here
To display project data in customer view, iam using
$query=Projects::find()
->where(['projects_clients_id'=> $model->customer_id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
]);
You can render several model and/or dataProvider in a view (properly constructed)
eg:
return $this->render('viewTestMulti', [
'modelOne' =>$modelOne,
'dataProviderTwo' => $providerTwo,
'dataProviderThree' => $providerThree,
'modeFour' => $modelFour,
]);
And then you can use a view with several gridView related to proper dataProvider and several forms everyone with a proper action
so when you press the specified submit you invoke the proper controller action
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $formOne = ActiveForm::begin();
$formOne->action= yii\helpers\Url::to('ControllerOne\create');
?>
<?= $formOne->field($modelOne, 'name') ?>
<?= $formOne->field($modelOne, 'email') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php $formFour = ActiveForm::begin();
$formFour->action= yii\helpers\Url::to('ControllerFour\create');
?>
<?= $formFour->field($modelFour, 'name_four') ?>
<?= $formFour->field($modelFour, 'email_four') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
I hope this could be useful
You have all the documentation about ActiveForms and I recommend, if you are new in Yii2 to use The Definitive Guide to Yii 2.0, here the Forms Section
This is a basic form:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>

Yii2 Display Error Messages Inside Modal

Here's how I implement my modal in my index.php view:
<?= Html::button(
'Change Password',
['value' => Url::to(['changepassword']). '&id=' . $session['user_id'],
'class' => 'btn btn-success',
'id' => 'modButton'
]) ?>
<?= Yii::$app->session->getFlash('message'); ?>
<?php
Modal::begin(['id' => 'modal2']);
echo "<div id='modContent'></div>";
Modal::end();
?>
And here is my modal form:
<?php $form = ActiveForm::begin(); ?>
<?= Yii::$app->session->getFlash('message'); ?>
</br>
<?= $form->field($model, 'password')->passwordInput(['value' => '', 'style' => 'width: 300px;'])->label('New Password') ?>
<?= $form->field($model, 'ConfirmNewPassword')->passwordInput(['value' => '', 'style' => 'width: 300px;']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Change Password', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
In my controller (just in case you'll be needing it):
if ($model->load(Yii::$app->request->post())) {
$model->password = sha1($model->password);
if($model->password !== sha1($model->ConfirmNewPassword)){
Yii::$app->session->setFlash('message', "Passwords don't match!");
return $this->redirect(['index']);
}
}
Every time I input an invalid password in my modal, the page redirects to the modal form in a separate page and there it displays the error message. I want that when the user inputs an invalid password, the modals stays with the error message somewhere inside it.
How do I do this?
Yes because you redirect to the "index" action if the password is not the same in Controller :
if($model->password !== sha1($model->ConfirmNewPassword)){
Yii::$app->session->setFlash('message', "Passwords don't match!");
return $this->redirect(['index']);
}
Should becomes :
if($model->password !== sha1($model->ConfirmNewPassword)){
Yii::$app->session->setFlash('message', "Passwords don't match!");
}
else {
return $this->redirect(['index']);
}

Categories