How to use 'onchange' in Yii2 MaskedInput widget - yii2

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

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.

Blank page on update after including if condition 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.

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 customize Flash Message with inline css in cakephp3

In my Controller :
$this->Flash->success(__(' Your Data has been Saved Successfully. Sheets Name in this XLS file : <span style="color:#FF9900"> '.$SheetName.'</span>'));
In my success.ctp located in Element\Falsh :
<div class="alert alert-success fade in" onclick="this.classList.add('hidden')"> × <strong>Success!</strong> <?= h($message) ?> </div>
The Message is display like :
Now the question is how to use inline css in the flash message.
I think the soloution for your problem would be to use another layout for this type of message.
$this->Flash->set($message, $options[])
You can specify to use a specific element in the options. Something like this:
create a new confirmation.ctp inyour project:
<div class="[...]">
<?= h($message) ?>
<span style="color:#FF9900">
<?= h($params['sheetName']) ?>
</span>
</div>
now when you want to show the new confirmation use this code:
$this->Flash->set(__('The Message:'), [
'element' => 'confirmation,
'params' => [
'sheetName' => $SheetName
]
]);
This will display your custom flash element.

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