K
K
Konstantin Prudnikov2017-11-03 03:04:38
Yii
Konstantin Prudnikov, 2017-11-03 03:04:38

How can you shorten rules in form validation in Yii2?

We have a code from a Yii2 model

<?php

namespace app\models;

use yii\base\Model;

class EntryForm extends Model
{
    public $name;
    public $email;

    public function rules() {
       return [
           [['name'], 'required'],
           ['email', 'email']
       ];
    }

}

Is it possible to shorten the rules (ideally, not validate at all) and by how much (if I leave it empty, the data from the form is not transmitted)?
You have to transfer your project from the procedure ( new.elenachezelle.ru/pattern/startpattern.php ) to Yii2.
There are a lot of fields, and I have already stalled on passing data from the form.
Thank you for your attention)

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2017-11-03
@KonstantinPR

Not only do you not use tags, but you also don’t read the documentation at all!
Why do you write such a footcloth ?!
Here, everything is short and clear.
The way you wrote is for validating a single field: That's all the abbreviation for you! And you are still validating it. Try to set a field with type string to a numeric value. Immediately, the error will come out and the red frame will be ... Everything will be. PS Or in general, try to write it down in the rules, maybe it will work
[$this->attributes(), 'string']

B
Boris Korobkov, 2017-11-03
@BorisKorobkov

If you want to write shitty code and assign values ​​yourself without validation, you can not use rules().
If you want to accept array values ​​via ->attributes($post), but without validation, you need to list the allowed fields with at least one rule() line.
If you want to write smart code, you need to validate everything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question