Answer the question
In order to leave comments, you need to log in
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'],
];
}
<?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(); ?>
<?= $form->field($model, 'name')->label('Логин') ?>
<?= $form->field($model, 'email')->label('Пароль') ?>
Answer the question
In order to leave comments, you need to log in
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
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.
How can I understand this rule?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question