Blank page on update after including if condition yii2 - yii2

When I include if condition in update form I'm getting blank page.
Otherwise without if condition update works fine.
_form.php without if condition (this works fine)
<?= $form->field($model, 'certified')->radioList(['y'=>'YES', 'n'=>'NO']) ?>
<div class="row">
<div class="row">
<?= $form->field($modelcertificate, 'description')->dropDownList(
ArrayHelper::map(CertificateDescription::find()->all(),'description','description'),
[ 'prompt'=>'select desc',
]); ?>
</div>
<div class="row">
<?= $form->field($modelqm, 'q1')->textInput(['maxlength' => true]) ?>
</div>
</div>
For the same if I include If condition, after hitting update button blank page will be shown.
_form.php with if condition ( leads to blank page)
<?= $form->field($model, 'certified')->radioList(['y'=>'YES', 'n'=>'NO']) ?>
<div class="row">
<?php if ($model->certified == 'y') : ?>
<div class="row">
<?= $form->field($modelcertificate, 'description')->dropDownList(
ArrayHelper::map(CertificateDescription::find()->all(),'description','description'),
['prompt'=>'select desc', ]); ?>
</div>
<?php else: ?>
<div class="row">
<?= $form->field($modelqm, 'q1')->textInput(['maxlength' => true]) ?>
</div>
<?php endif; ?>
</div>

The problem was in Controller, so blank page is caused by missing return point.
For debug purposes it possible to use CRUD generated by gii, and improve it line by line to fit your personal needs.

Related

yii2 from validation on array form elements

I've a form containing array elements and I've to validate it. i cant validate it by using the in-built validation rules, because the elements are not present in the database. i'm serialising the data and save them in a single field in database. so i tried to do the validation with custom validation.
my actual problem is the array field is validating but it doesn't shows the validation error message in the corresponding field.
This is the form
<div class="col-md-6">
<?php Portlet::begin(['title' => t('Shipper')]) ?>
<?= $form->field($model, 'shipper[name]')
->textInput(['maxlength' => true])->label(t('Name'))?>
<div class="row">
<div class="col-md-8">
<?= $form->field($model, 'shipper[address_line1]')
->textInput(['maxlength' => true])->label(t('Address Line 1')) ?>
<?= $form->field($model, 'shipper[address_line2]')
->textInput(['maxlength' => true])->label(t('Address Line 2')) ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'shipper[city]')
->textInput(['maxlength' => true])->label(t('City')) ?>
<?= $form->field($model, 'shipper[pin]')
->textInput(['maxlength' => true])->label(t('Pin Code')) ?>
</div>
</div>
Model
.....
['shipper', function ($attribute, $params) {
if (!filter_var($this->{$attribute}['email'], FILTER_VALIDATE_EMAIL)) {
$this->addError('shipper', 'The email format is invalid!');
}
}],
....
You could add attributes to your model class like the following:
class MyModel extends \yii\db\ActiveRecord {
public $shipperAddressLine1;
public $shipperAddressLine2;
public $shipperCity;
public $shipperPin;
...
}
Then you can add validation rules for them just like your database attributes.

Yii2: Fill form automatically only when triggered

How to fill a form automatically, but only when it is requested by the user.
That is, the fields are not populated when the form is generated.
Por example:
<?= $form->field($model, 'city')->textInput() ?>
When the form is accessed, the field is empty. And what I want is that there is a button, which when the user clicks, automatically the field is populated with the value of the "city" that is in the "User" table.
Form:
City : __________ | get my city |
After user clicked on button "get my city":
City : _London___ | get my city |
At last no ajax needed
<div class="row">
<div class="col-sm-12">
<?= Html::activeLabel($model, 'city') ?>
</div>
<div class="col-xs-9">
<?= $form->field($model, 'city')->textInput()->label(false) ?>
</div>
<div class="col-xs-3">
<?= \yii\helpers\Html::button('get my city', ['id' => 'get-user-city-btn']) ?>
</div>
</div>
<?php $this->registerJs("
$('#get-user-city-btn').on('click', function(){
$('#id-of-city-input-goes-here').val('"
. Yii::$app->user->identity->city
. "');
});
", $this::POS_END, 'get-user-city-script'); ?>

How to use 'onchange' in Yii2 MaskedInput widget

I have an Yii2 app with advanced template. There is 2 fields on my form. 1st: MaskedInput and 2nd textInput with readonly attribute.
So, I wanted to fill 2nd textInput automatically right after I have filled MaskedInput. For this I tried to use onchange. But I got following error:
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\widgets\MaskedInput::onchange
Here is my code:
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-xs-3">
<?= Html::label("Ser num")?>
<?= MaskedInput::widget(['name'=>'serNum',
'mask'=>'AA 9999999',
'onchange'=>'
$.post("index.php?r=act/actid&serNum='.'"+$(this).val(),function(data){
$("select#ser-sernum").html(data);
});
'
])?>
</div>
<div class="col-xs-3">
<?= $form->field($model, 'sernum')->textInput(['readonly'=>true]) ?>
</div>
<div class="col-xs-6">
<b id="actstatus"></b>
</div>
</div>
Try with options property:
<?= yii\widgets\MaskedInput::widget(['name'=>'serNum',
'mask'=>'AA 9999999',
'options' => [
'onchange'=>'
$.post("index.php?r=act/actid&serNum='.'"+$(this).val(),function(data){
$("select#ser-sernum").html(data);
});'
]
])?>

Login form requires second click to submit

My backend login form submits with a single click of the login button. It creates an <input type="hidden" name="login-button"> element then shortly thereafter submits. On the frontend the same is created on click but never posts the data until I click the button again.
Here is a sample of the view code:
<div id="login-form" class="col-sm-12">
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<div class="col-sm-5 col-sm-offset-4 forgotPassword">
<?= $form->field($model, 'rememberMe')->checkbox() ?>
<div style="margin:auto;color:#fff;margin:1em 0;font-size:10px;">
if you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>
</div>
<div class="form-group">
<?= Html::submitButton('login', ['class' => 'btn btn-primary loginButton', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
The only difference I am seeing between the two is that there are some additional <div> tags that have been added for styling purposes. I have even removed those and I am still seeing the same issues.
Any ideas would be appreciated.
It looks like the id on the form div was causing the issue. Removing this has cleared everything up.
It was causing a conflict because it had the same id as the form.
I just want to add that usually you assign model "formName" as your form id
<?php $form = ActiveForm::begin(['id' => $model->formName()]); ?>

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