Yii2 : How to change URL in Action Column Grid View? - url

I want to change url in action view, from view to viewdaily, viewweekly and viewmonthly, what should i do?
[
'class' => 'yii\grid\ActionColumn',
],

The simplest way is not create/use ActionColumn, instead you can use raw-column and show there what you want:
[
'attribute' => 'some_title',
'format' => 'raw',
'value' => function ($model) {
return 'your Text';
},
],

Related

Yii 2 - authclient doesn't call onAuthSuccess

I am using Yii authclient to use social login. I had set everything as it is defined in docs but when I try to login with google it does not call onAuthSuccess method. When I try to login it just redirects me to returnUrl but not authenticated.
Here is my code;
config/main.php
'authClientCollection' => [
'class' => \yii\authclient\Collection::class,
'clients' => [
'google' => [
'class' => \yii\authclient\clients\Google::class,
'clientId' => *********, //changed for issue purpose
'clientSecret' => *********, //changed for issue purpose
'returnUrl' => 'http://localhost/site/landing',
],
],
]
controllers/SiteController
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup', 'auth'],
'rules' => [
[
'actions' => ['signup', 'auth'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['#'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
'create-storyboard' => ['post'],
],
],
];
}
/**
* {#inheritdoc}
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'onAuthSuccess'],
],
];
}
public function onAuthSuccess($client)
{
(new AuthHandler($client))->handle();
}
If you set returnUrl the user is from auth provider redirected directly to the url you've set in that property.
In your case the returnUrl says google, that it should redirect user to http://localhost/site/landing. But there is nothing in your site/landing action that would call the onAuthSuccess.
You need to let user come back to site/auth and redirect them after processing response from OAuth provider. To do that remove the returnUrl from config. That will make the authclient to use default return url which is the action that started the auth process.
Then modify your onAuthSuccess to redirect users to site/landing like this:
public function onAuthSuccess($client)
{
(new AuthHandler($client))->handle();
$this->redirect(['site/landing']);
}
I had solved the problem with the help from #Michal HynĨica. The problem was in my returnUrl which means with authentication url it must follow to authenticate rather the redirecting after authentication. So all I need to do was changing it to as below.
'returnUrl' => 'http://localhost/site/auth?authclient=google'
Also don't forget to add same returnUrl to your google console's redirect url.

How to get the selected option of a radion button element in Zend Framework 2?

In a Fieldset I have an Element\Radio foo and Element\Text bar.
public function init()
{
$this->add(
[
'type' => 'radio',
'name' => 'foo',
'options' => [
'label' => _('foo'),
'value_options' => [
[
'value' => 'a',
'label' => 'a',
'selected' => true
],
[
'value' => 'b',
'label' => 'b'
]
]
]
...
]);
$this->add(
[
'name' => 'bar',
'type' => 'text',
'options' => [
'label' => 'bar',
...
],
...
]);
}
The validation of the field bar is depending on the selected foo option. It's easy to implement, if I can get the selected value of foo:
public function getInputFilterSpecification()
{
return [
'bar' => [
'required' => $this->get('foo')->getCheckedValue() === 'a',
...
],
];
}
But there is no method Radio#getCheckedValue(). Well, I can iterate over the $this->get('foo')->getOptions()['value_options'], but is it really the only way?
How to get (in the Fieldset#getInputFilterSpecification()) the selected option of a Zend\Form\Element\Radio?
The selected option gets POSTed to the server along with everything else from the HTML form and is all of this is available in validators through the $context array.
You can create a conditionally required field by using a callback validator and the $context array like this:
public function getInputFilterSpecification() {
return [
'bar' => [
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
'required' => true,
'validators' => [
[
'name' => 'Callback',
'options' => [
'callback' => function ($value, $context) {
return $context['foo'] === 'a'
},
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'This value is required when selecting "a".'
]
]
]
]
],
];
}
That would check if 'foo' is equal to 'a', i.e. option 'a' is selected and return true when it is, which marks the input as valid, and false when it's not, marking the input invalid.

zf2 form disable select option(s)

Is it possible to disable the options in a select element?
I have a form with a select element that by default has a lot of options available. During form creation, depending on information retrieved from the database, i would like to disable certain options.
Some research came up with
$form->get('selectElement')->setAttribute("disabled", array(0, 1, 2));
...which should disable the first 3 options, but unfortunately does not.
You must use the setAttribute() method to set the attributes of your select element, not its options. For this, you should use setValueOptions():
$myOptions = $form->get('selectElement')->getValueOptions();
foreach ([0, 1, 2] as $value) {
$myOptions [$value]['disabled'] = true ;
}
$form->get('selectElement')->setValueOptions($myOptions);
$myOptionsmust be an array of options:
[
[
'label' => 'My first option',
'disabled' => false,
'value' => 1
],
[
'label' => '2nd option',
'disabled' => false,
'value' => 2
],
[
'label' => '3rd option disabled',
'disabled' => true,
'value' => 3
],
]

ZF2 Validator Context when using a Fieldset or Collection

Is it possible to pass the full form as the context to a validator?
I would like to create a conditional validator for element X in fieldset A which checks the value of element Y in a different fieldset B.
The problem is that the isValid function only receives the context for the fieldset it is in. This element X knows nothing about element Y.
All answers greatly received!
You can do this with collections and ZendCollectionInputFilter yeah.
There's not like loads of documentation for this, know the zend guys are sorting this out though (think the only mention of it is in http://framework.zend.com/apidoc/2.2/classes/Zend.InputFilter.CollectionInputFilter.html) but for now a resource that really helped me was this:
http://www.aronkerr.com/2013/11/zf2-form-collection-validation-unique.html
Very clever stuff once you get your head round these. Can't really give you much more help as your question isn't massively specific and there is no code for your form, fieldsets and input filters that you currently have implmented but hope this helps. If you get stuck at any point more than happy to run through more specific code
Let's say our fieldsets A and B belong to the form Sample. We need to add the validators from this parent form in order to access the context of this form when validating any child fieldsets:
<?php
namespace App\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;
class Sample extends Form InputFilterProviderInterface
{
public function init()
{
$this->add([
'type' => 'App:Fieldset:A',
'name' => 'fieldsetA',
]);
$this->add([
'type' => 'App:Fieldset:B',
'name' => 'fieldsetB',
]);
$this->add([
'type' => 'submit',
'name' => 'submit',
'attributes' => [
'value' => 'Submit',
],
]);
}
public function getInputFilterSpecification()
{
return [
'fieldsetA' => [
'type' => 'InputFilter',
'X' => [
'required' => true,
'allow_empty' => true,
'continue_if_empty' => true,
'validators' => [
[
'name' => 'Callback',
'options' => [
'callback' => function ($value)
{
if ($this->data['fieldsetB']['Y'])
{
// do something
}
// do something else
},
],
],
],
],
],
];
}
}
Notice how we add validators to X in A from within Sample using the InputFilter type. Next we access $this->data directly and traverse it to get Y in B.

How can i add parameter in mandrill html text?

Is there any way to include the parameter i html portion of mandrill ?
I am not using template.
here is my code.
$mandrill = new Mandrill('YOUR_API_KEY');
$message = array(
'html' => '<p>here is my email</p>',
'text' => 'Example text content',
'subject' => 'example subject',
'from_email' => 'message.from_email#example.com',
'from_name' => 'Example Name',
'to' => array(
array(
'email' => 'recipient.email#example.com',
'name' => 'Recipient Name',
'type' => 'to'
),
array(
'email' => 'recipient.email#example.com',
'name' => 'Recipient Name',
'type' => 'to'
)
),
What i want to do is to add a parameter to the html content.
'html' => '<p>here is my email{ param}</p>', // this line
How can i do this ?
Thanks
Sounds like you're looking to use Mandrill merge tags.
You can insert merge tags into your HTML content and have it inject personalized information for each recipient. They can be used in your content with the format *|MERGETAG|* and by defining "merge_vars" in your message array.
Here's an example from their docs:
"message": {
"global_merge_vars": [
{
"name": "var1",
"content": "Global Value 1"
}
],
"merge_vars": [
{
"rcpt": "emailadress#domain.com",
"vars": [
{
"name": "fname",
"content": "John"
},
{
"name": "lname",
"content": "Smith"
}
]
}
],
source:
http://help.mandrill.com/entries/21678522-How-do-I-use-merge-tags-to-add-dynamic-content-
In testing a bit, Mandrill actually supports a few merge tags "out of the box", so you should just be able to add *|EMAIL|* into your HTML content and it'll pull that email address in from your TO array.

Resources