delete button with confirmation in yii2 - php

This view.php is generated with CRUD in Yii2 but the delete button doesn't confirm window.
But in index.php generated with CRUD in Yii2, The confirm window for delete worked.
<p>
<?= Html::a('Edit', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
<?= Html::a('Back','index', ['class' => 'btn btn-warning']) ?>
</p>
Delete Button in index.php:
<span class="glyphicon glyphicon-trash"></span>
Delete Button in view.php:
<a class="btn btn-danger" href="var/delete?id=2" data-confirm="Are you sure you want to delete this item?" data-method="post">Delete</a>

The section about yii.js is still in progress.
To get default support of confirmation you should register yii.js.
Using yii\web\YiiAsset
use yii\web\YiiAsset;
YiiAsset::register($this);
or registering script files (not recommend, just for example)
$this->registerJsFile('#yii/assets/yii.js', ['depends' => [\yii\web\JqueryAsset::className()]]);

Related

yii2: set another site for link with Html::a

i have a link (href) to another site but i have problem
this is my code :
return Html::a('Create More', ["https://face.com/"], ['class' => 'btn btn-primary', 'role' => 'modal-remote']);
why my link is a new action?
i want change my baseurl to https://face.com/ but dosnt work
this is my new link:
https://niniplus.com/newadmin/index.php/https://face.com
By just removing the array ["https://face.com/"], an absolute url will be return.
return Html::a('Create More', "https://face.com/", ['class' => 'btn btn-primary', 'role' => 'modal-remote']);
If you want to use an absolute url you can call yii\helpers\Url::to() yourself, before passing the URL to this method, like this:
return Html::a('Create More', Url::to('https://face.com/', true), ['class' => 'btn btn-primary', 'role' => 'modal-remote']);
You need to add use yii\helpers\Url; into view and you should write your anchor tag like below:
return Html::a('Create More', Url::to('https://face.com/', true), ['class' => 'btn btn-primary', 'role' => 'modal-remote']);

implement function for hiding buttons for guest users Yii2

First i want to say that i know there is many post about jQuery and Yii2 around the network but still can't make it clear for me! My question is pretty simple for you i think. When a user is with User role permission he shouldn't be able to update or delete others posts. And so if the user is this kind of guy i want to make both anchors ('Edit' and 'Delete') with opacity 0.5.
This is my view part:
<?php $form = \yii\bootstrap\ActiveForm::begin([
'method' => 'post'
]);
?>
<div class="col-md-8 text-left">
<?= Html::a('Edit', ['update', 'id' => $model->post_id], ['class' => 'btn btn-primary']); ?>
<?=
Html::a('Delete', ['delete', 'id' => $model->post_id],
['data-method' => 'POST', 'class' => 'btn btn-danger']);
?>
</div>
<div class="col-md-4 text-right">
<?= Html::a('Back', 'index', ['class' => 'btn btn-warning']); ?>
</div>
<?php \yii\bootstrap\ActiveForm::end(); ?>
I have added my file to AppAsset.php(test.js):
public $js = [
'js/slide-show.js',
'js/test.js'
];
I know how to make my fucntion but do not know how to implement it. Think i should do it with if statement (if(user->isGuest){ execute function and make anchros with opacity 0.5 }). Can you guys teach me the right way? Will be thankful for every advice! Thank you in advance!
You could assign a disable option so the button is displayed but disabled
if (Yii::$app->user->isGuest) {
echo Html::a('Edit', ['update', 'id' => $model->post_id], ['class' => 'btn btn-primary',
'disabled' => 'disabled']);
echo Html::a('Delete', ['delete', 'id' => $model->post_id],
['data-method' => 'POST', 'class' => 'btn btn-danger',
'disabled' => 'disabled']);
}

How to identify the two button clicks of the same form in Yii2

I have two buttons named search,create in the same form called search.How to identify these two button clicks in the controller function.
<?= Html::a(Yii::t('app', 'Search'), ['search'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('app', 'Create'), ['search'], ['class' => 'btn btn-success']) ?>
It is a little strange what you are asking. But here goes nothing.
First you can always just add a parameter to the link, for example:
<?= Html::a(Yii::t('app', 'Search'), ['search', 'button' => 'search'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('app', 'Create'), ['search', 'button' => 'create'], ['class' => 'btn btn-success']) ?>
This will create 2 different links and you can use the GET parameter to figure out what was clicked.
What I actually think you are trying to do is to submit a form. In Bootstrap the buttons and links look the same. You actually have 2 links, you do not have 2 buttons. The simple solution would just to turn those links into actual buttons and give them a name and a value.
<?= Html::button(Yii::t('app', 'Search'),
[
'name'=>'button',
'value'=>"search",
'class' => 'btn btn-success'
]
)?>
<?= Html::button(Yii::t('app', 'Create'),
[
'name'=>'button',
'value'=>"create",
'class' => 'btn btn-success'
]
)?>
If your form is using GET to transmit data check the GET parameter to figure out what was clicked, otherwise use POST to figure out the same thing.

create pjax form inside a active form yii2

I can't create a Pjax form inside an active form. What I've found is that when I put the Pjax inside an active form it doesn't work (it submits the entire page) but when I put it outside the active form its works fine:
<?php
$form = ActiveForm::begin([
'id' => 'CouplePartnerForm',
'action' => ['couple-partner/savecouplepartner'],
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'validateOnSubmit' => true,
]);
?>
<div class="row">
<?php Pjax::begin([]); ?>
<?= Html::beginForm(['couple-partner/form-submission'], 'post', ['data-pjax' => '0', 'class' => 'form-inline']); ?>
<?= Html::input('text', 'string', Yii::$app->request->post('string'), ['class' => 'form-control']) ?>
<?= Html::submitButton('Hash String', ['class' => 'btn btn-lg btn-primary', 'name' => 'hash-button']) ?>
<?= Html::endForm() ?>
<h3><?= isset($stringHash) ?$stringHash :"" ?></h3>
<?php Pjax::end(); ?>
</div>
<?php ActiveForm::end(); ?>
So what is the problem here?

Action buttons only visible to admin yii2

I tried to add extra action buttons. Admin only view this button after button click update in a single field in the database.
<p>
<?php
if(!Yii::$app->user->isGuest ){
echo Html::a('Recommended', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']);
echo Html::a('Not Recommended', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to reject this application?',
'method' => 'post',
],
]);
} else if(Yii::$app->user->can('admin')){
}
?>
</p>
My problem is that I have 3 users that are: applicant, faculty and admin (or hod). In this case after faculty recommendation, the admin (or hod) sanctioned the leave.
I create leave application and faculty recommended, so now I want get the recommended data when admin login to the site.
If admin is the username you should follow this way :
<p>
<?php
if(!Yii::$app->user->isGuest ){
echo Html::a('Recommended', ['update', 'id' => $model->id],
['class' => 'btn btn-primary']);
echo Html::a('Not Recommended', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to reject this application?',
'method' => 'post',
],
]);
} else if(Yii::$app->user->identity->username == 'admin' ){
echo Html::a('Your Button Label for Admin',
['yourActionForAdmin', 'id' => $model->id],
['class' => 'btn btn-primary']);
}
?>
</p>

Categories