yii2-validation

Yii 2 file input renders hidden file input tag

你离开我真会死。 提交于 2021-01-29 07:15:52
问题 I am using yii2 to build a simple application with a signup feature. Problem is when I render a file input tag using active forms, it render a file input field and a hidden field. The validator then picks the one which is hidden and always says that profile image is required though it saves it in my upload directory and also adds the path to the database but still returns with this error. Thanks for any help. Here is the code: View: <?php $form = ActiveForm::begin(['id' => 'form-signup' ,

how to place yii2 form error in title of anchor tag

拥有回忆 提交于 2020-01-15 03:45:06
问题 how to place yii2 form error in title of anchor tag This is my code $form = ActiveForm::begin([ 'id' => 'login-form', 'options' => ['class' => 'form-horizontal'], 'fieldConfig' => [ 'template' => ' {label} <div class="error-block"> <a href="#" title="{error}">error</a> </div> {input} ', 'errorOptions' => ['tag' => null] ], ]); I want to add error in title of anchor tag in YII2 <a href="#" title="{error}">error</a> 回答1: You need to display the error text inside the anchor tags title attribute,

Yii2 form validation - compare password repeat only when password field is filled

╄→尐↘猪︶ㄣ 提交于 2020-01-14 03:10:26
问题 My form validation uses the following rules: [['password', 'password_repeat'], 'required'], ['password_repeat', 'compare', 'compareAttribute' => 'password', 'message' => "Passwords don't match"], How to write rules for password_repeat to compare it with password only if user fill password field. If user skip password , validation for password_repeat should be also skipped. 回答1: You can use scenarios for that: public function rules() { return [ [['username', 'password'], 'required', 'on' =>

Validating unique email using DynamicFormWidget - Yii2

邮差的信 提交于 2019-12-24 00:34:20
问题 I am using DynamicFormWidget for multiple input fields like: first_name , last_name , email & mobile_no . And, I don't want user to type the existing email . Means, email should be unique. It is working when I am not using DynamicFormWidget . Actually, I don't know how to validate unique email in multiple input forms using Yii2. addmembers.php (View) <?php use yii\helpers\Html; use yii\widgets\ActiveForm; use wbraganca\dynamicform\DynamicFormWidget; ?> <?php DynamicFormWidget::begin([

how to keep selected value on Yii2 html::dropddownlist?

老子叫甜甜 提交于 2019-12-20 07:48:42
问题 i have the following code in my view: <?=Html::dropdownList('region',null, ArrayHelper::map(Ethioregion::find()->all(),'region','region'),[ 'prompt' => 'Select Region..','style'=>'width:200px',]) ?> and submit button <?= Html::submitButton('Search', ['name' => 'dele','class' => 'btn btn-primary']) ?> my code working well but I have one big problem. I select a region and click the search button, the result comes correctly, and 10 result is shown per page when I click the next page, it gives me

Yii2 unique validator ignored

只谈情不闲聊 提交于 2019-12-12 17:35:34
问题 In the rules() of my RegisterForm model: [ 'user_username', 'unique', 'targetClass' => 'app\models\User', 'message' => 'This username is already been taken.' ], In my controller: $model = new RegisterForm(); if ( $model->load( Yii::$app->request->post() ) ) { if ( $user = $model->register() ) { return $this->redirect( [ '/login' ] ); } } In RegisterForm: public function register() { $user = new User(); $user->user_firstname = $this->user_firstname; $user->user_lastname = $this->user_lastname;

How to put line-breaks in Yii2 validation rules messages

拜拜、爱过 提交于 2019-12-12 12:14:50
问题 I need to break a long message used in Yii2 validation rule. I tried like this: public function rules() { return [ ['username', 'required', 'message' => 'long message first line here'."<br>".PHP_EOL.'long message last line here'], ]; } but the <br> appears in the message and the line doesn't break where I need. Just to be clear, what I get is: long message first line here<br>long message last line here and not: long message first line here long message last line here Anyone who can help with

Disable Yii Validation Error Message on focus / key up - Yii2

喜你入骨 提交于 2019-12-11 04:25:00
问题 As default, error message coming on keyup and after pressing submit button in form (If any error exist for that particular attribute). Which Is Ok. Working perfectly fine. But, Is it possible to disable error message on key up ? Means, error message, if any, should come only on pressing submit button. View <?php $form = ActiveForm::begin([ 'id' => 'register-form']); ?> <?= $form->field($model, 'first_name',['inputOptions' => ['class' => 'form-control fname','placeholder'=>'First Name']])-

Yii2: How to set default attribute values in ActiveRecord?

江枫思渺然 提交于 2019-12-09 08:46:50
问题 This may seem like a trivial question, however all of the obvious solutions that I can think of have their own flaws. What we want is to be able to set any default ActiveRecord attribute value for new records only, in a way that makes it readable before and during validation and does not interfere with derived classes used for search. The default values need to be set and ready as soon as we instantiate the class, so that (new MyModel)->attr returns the default attr value. Here are some of

Yii2: How to set default attribute values in ActiveRecord?

折月煮酒 提交于 2019-12-03 11:23:06
This may seem like a trivial question, however all of the obvious solutions that I can think of have their own flaws. What we want is to be able to set any default ActiveRecord attribute value for new records only, in a way that makes it readable before and during validation and does not interfere with derived classes used for search. The default values need to be set and ready as soon as we instantiate the class, so that (new MyModel)->attr returns the default attr value. Here are some of the possibilities and the problems they have: A) In MyModel override the init() method and assign default