A
A
Alexander2017-01-14 19:16:49
Yii
Alexander, 2017-01-14 19:16:49

How to understand yii2 code?

Hello. Literally today I started to get acquainted with yii2 and immediately encountered difficulties in understanding the code.
The documentation perfectly describes the functionality of yii, but does not pay attention to code analysis at all. For example: when creating a form, we create an EntryForm model in which we describe the function

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

How can I understand this rule? Next, we create a form in the view
<?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'name')->label('Логин') ?>
    <?= $form->field($model, 'email')->label('Пароль') ?>

    <div class="form-group">
        <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
    </div>

<?php ActiveForm::end(); ?>

How to understand these lines, and specifically what is in brackets after field
<?= $form->field($model, 'name')->label('Логин') ?>
 <?= $form->field($model, 'email')->label('Пароль') ?>

I hope that there will be people who will be able to help understand the yii2 code in the future. Thanks in advance for your replies!

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-01-14
@Sentim

rules in the model are validation rules, you can read about what validators are in yii2 and how to write your own here:
https://github.com/yiisoft/yii2/blob/master/docs/g...
Regarding the form, it is created using the ActiveForm widget, and in order to fully understand everything, you need to understand how widgets are arranged in yii, but specifically on the issue:
$form is a variable in which the widget object, ->field() is the model method to which we pass the model itself and the name of the attribute for which the input should be generated, the label() method for changing the label, but they are usually specified in the model. I advise you to put an ide of type phpStorm, which would make it easier to jump through classes and methods and see the activeForm code in full.
In general, I advise you to read the completely official guide, a lot will become clear:
www.yiiframework.com/doc-2.0/guide-README.html

D
Dmitry, 2017-01-14
@slo_nik

Good evening.
The documentation for yii2 is quite well written.
I don’t know which framework you are trying to learn, but try this one from the developers.
On the following question.
Your first example checks the data for validity, whether the form is filled out correctly. The fields "name", "email" must be filled in, in addition "email" must be a valid e-mail address.
The next two examples generate a normal html form. One text input for entering a name, the second for entering an email.
ps
Literally today I started to get acquainted with yii2 and immediately encountered difficulties in understanding the code.
If you started learning the framework at the same time as php, then first learn the basics of php.

I
index0h, 2017-01-14
@index0h

How can I understand this rule?

Use your cognitive abilities.
Write the code according to the examples in the documentation, get it executed, and then to explore how and what works - arm yourself with xdebug.
Specifically on rules - find the code in the framework that uses this method and understand how it works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question