Implementing Upvote using Pjax - yii2

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');
}

Related

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]) ?>

how to handle multiple submit buttons in form cakephp3

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']);
}

Yii2: How to validate a form with multiple instances of the same model

In my form I update more start and end dates from the same model at once. See the simplified form:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $i=>$date): ?>
<?= $form->field($date,"[$i]start"); ?>
<?= $form->field($date,"[$i]end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
In the model I need to control, if the end date is after the start date:
public function rules() {
return [
[['end'], 'compare', 'compareAttribute' => 'start', 'operator' => '>', 'message' => '{attribute} have to be after {compareValue}.‌'],
];
}
I tried to change selectors similarly as described in: Yii2: Validation in form with two instances of same model, but I was not successful. I suppose I need to change the 'compareAttribute' from 'mymodel-start' to 'mymodel-0-start' in the validation JS:
{yii.validation.compare(value, messages, {"operator":">","type":"string","compareAttribute":"mymodel-start","skipOnEmpty":1,"message":"End have to be after start.‌"});}
So, I look for something like:
$form->field($date,"[$i]end", [
'selectors' => [
'compareAttribute' => 'mymodel-'.$i.'-start'
]
])
Solution
The solution is based on the answer of lucas.
In the model I override the formName() method, so for every date I have a unique form name (based on ID for the existing dates and based on random number for new dates):
use ReflectionClass;
...
public $randomNumber;
public function formName()
{
$this->randomNumber = $this->randomNumber ? $this->randomNumber : rand();
$number = $this->id ? $this->id : '0' . $this->randomNumber;
$reflector = new ReflectionClass($this);
return $reflector->getShortName() . '-' . $number;
}
The form then looks like this:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $date): ?>
<?= $form->field($date,"start"); ?>
<?= $form->field($date,"end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
Override the formName() method in your model class to make it unique. If you don't want to change your model class, create a subclass of it for the purpose of working for this controller action. After doing this, the html ID and name fields will automatically be unique.

DropDownList yii 2.0 example

I am using yii 2.0 Framework.
How i can make options from my database.
I found this, but it's yii 1.1:
<?php echo CHtml::dropDownList('listname', $select,
array('M' => 'Male', 'F' => 'Female'));
I want to pass it to form:
<?php $form->dropDownList() ?>
How i can fill my dropdownlist from my database table?
If you use ActiveForm widget use this:
<?php
$items = ArrayHelper::map(Model::find()->all(), 'id', 'name');
$form->field($model, 'attribute')->dropDownList($items)
?>
Use yii\helpers\Html it contains Html::dropDownList().
echo Html::dropDownList('listname', $select, ['M'=>'Male', 'F'=>'Female']);
Check Yii Framework 2.0 API
Controller
public function actionSomething() {
$sexes = ['M'=>'Male', 'F'=>'Female'];
$this->render('yourView', ['sexes'=>$sexes]);
}
View
<?php
::
echo Html::dropDownList('listname', $select, $sexes);
::
?>
Yes if you use ActiveForm widget , u dont have to change anything in the controller , in views, in the form, add this where u want the dropdown
use yii\helpers\ArrayHelper;
<?php
$city = \app\models\City::find()->all();
$listData=ArrayHelper::map($city,'cityId','cityName');
?>
<?= $form->field($model, 'cityId')->dropDownList($listData,['prompt'=>'Choose...']) ?>
<?= $form->field($model, 'name_of_field')->dropdownList(['1' => 'aaa', '2' => 'bbb'], ['prompt' => '---Select Data---']) ?>

Different sidebars for different pages on Wordpress

I'm trying to design a theme on WordPress (version 3.3.2) but I am having a few problems in having the sidebar display a different set of widgets on certain pages.
I have tried several online tutorials and this one in particular http://www.rvoodoo.com/projects/wordpress/wordpress-tip-different-sidebars-on-different-pages/ but unfortunately there is no change in the sidebar when I move to a different page
I registered two sidebars on my functions.php as shown below, one which I would consider as main, and the other as a custom sidebar, and I also added different widgets to these sidebars.
<?php register_sidebar( //register sidebar
array(
'name' => 'Right-side',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
register_sidebar( //register second sidebar
array(
'name' => 'Second-right',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
?>
Following that, I created the files sidebar.php and sidebar-second.php to be able to call them.
sidebar.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Right-side')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
sidebar-second.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Second-right')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
And then I added replace my <?php get_sidebar() ; ?> statement with following conditional
<?php if( is_page('225') ) : ?>
<?php dynamic_sidebar('second'); ?>
<?php else : ?>
<?php get_sidebar() ; ?>
<?php endif ; ?>
However only the widgets added to the sidebar.php are displayed on every page. Any help on how I could change this, or pointers on what I could be doing wrong. Thanks.
I looks like you are using <?php dynamic_sidebar('second'); ?> where you should be using <?php get-sidebar('second'); ?>
What get_sidebar('foo') does is look for a file named 'sidebar-foo.php' in the root directory of your theme and includes it. dynamic_sidebar('bar'), on the other hand, looks for a sidebar registered with:
<?php register_sidebar( array( 'name' => 'bar' ... ) ) ?>
Hope this helps!
Why don't you use the Custom Sidebar Plugin?
It's really easy to use and does not need any coding
You have two sidebars. when you want to use the sidebar-second file on a particular page, instead of calling
<?php get_sidebar('second') ; ?>
TRY
<?php get_sidebar('sidebar-second.php') ; ?>
this should do the trick