Questions tagged [yii2-validation]

Use this tag if your question relates to the Yii2-validation-system, no matter if your validation is within a model or as standalaone validator-instances.

This tag is about the Yii2-validation-system. In most cases validators get used within models to validate their properties (rules()-method). However, you can also use all the validators as standalone instances or create your own inline-validators.

The basic usage of Yii2's validators is very well documented in the corresponding section of the official guide.

150 questions
17
votes
6 answers

Yii2: How to set default attribute values in ActiveRecord?

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…
mae
  • 12,340
  • 8
  • 28
  • 40
12
votes
2 answers

Yii2, Custom validation message with attribute names

In Login form, I need to have glyphicon-remove icon at the end of every validation message with the corresponding field names. So I used below code in the Login model. ['email', 'required', 'message' => 'Email cannot be blank
Siva.G ツ
  • 811
  • 1
  • 7
  • 20
6
votes
1 answer

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):
6
votes
3 answers

skip yii2 unique validation on update action if new value equals to prev value

Because of unique validator get error on update action ('this username is already taken') i wanna validate username on update action when new username value not equals to prev value, it's my rule: [['username'], 'unique', 'on'=>'update',…
SADi
  • 195
  • 1
  • 12
5
votes
2 answers

Yii2 Convert from integer to datetime to display and save into database with integer

I have a table with column for_date saved by type integer in database. In order to show for_date with format DateTime, I used ActiveForm with code: field($model, 'for_date')->textInput([ 'class' => 'form-control datepicker', …
Francis
  • 279
  • 1
  • 5
  • 16
4
votes
1 answer

Yii2 HTML purifier

I've got a question about Yii2's validation. So, my model validation rule's something like this: return [ ['status', 'required', 'on' => 'update'], [['status'], function ($attribute) { $this->$attribute =…
Miller Newbie
  • 75
  • 1
  • 7
4
votes
1 answer

How to set validation message for field having ajax validation in scenarios? - Yii2

I'm using one model file for 2 forms. One for SIGNUP & Other for Adding members. I didn't set any scenario for SIGNUP form. But, scenario for Adding members form is set. Model public function rules() { return [ //Add Members …
Nana Partykar
  • 10,175
  • 8
  • 43
  • 73
4
votes
2 answers

Invalid validation rule: a rule must specify both attribute names and validator type, in Yii2

In my basic app project, I'm trying to integrate the signup form, I'm getting this error: Invalid validation rule: a rule must specify both attribute names and validator type. My code is here. SignUpForm.php
Mohan Prasad
  • 572
  • 8
  • 30
4
votes
1 answer

Yii2:compare start and end time in tabular/array input

Is it possible to compare start and end time in my below form in yii2 client/ajax validation. My view file code like this: weekDaysList as $index => $value) : ?>
Hiren Bhut
  • 948
  • 1
  • 11
  • 17
4
votes
1 answer

How to put line-breaks in Yii2 validation rules messages

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'."
".PHP_EOL.'long message last line here'], …
danicotra
  • 953
  • 2
  • 10
  • 29
3
votes
1 answer

Yii2. Many inputs for one Model/DB field

How such inputs could be translated into DATE field in DB? Customer insisted on such way to input user birthdate. First my thought was simply to concat in one field in beforeValidate, but after this inputs won't be able to display date, that was…
D.R.
  • 1,676
  • 2
  • 16
  • 37
3
votes
2 answers

Validate form by ajax in yii2

I have a form which is opening in popup so I want to validate my form by ajax validation but when i click on submit button my page getting refreshed so I am not getting any validation error View file:
Akhilesh Jha
  • 31
  • 1
  • 1
  • 5
3
votes
2 answers

Custom validation message not working in Yii2

I want to display custom validation messages for max length and min length of phone number. I have written this code to display custom validation message for max length and min length of phone number field. ['phone', 'string', 'max' => 12, 'min' =>…
Vinit Singh
  • 1,113
  • 2
  • 21
  • 45
3
votes
3 answers

How to create a directory inside a directory in yii2 and upload file into that directory

I have a form ['enctype' => 'multipart/form-data']]); ?> field($userformmodel, 'user_image')->fileInput(); ?> 'btn btn-primary']) ?> …
3
votes
1 answer

Yii2 AccessControl giving Forbidden 403 when using HTTPBearerAuth in Rest API

I am authenticating on my base controller using HTTPBearerAuth public function behaviors() { $behaviors['authenticator'] = [ 'class' => CompositeAuth::className(), 'authMethods' => [ HttpBearerAuth::className(), …
learner
  • 339
  • 1
  • 2
  • 9
1
2 3
9 10