1. Create a model
a. Add a base class
Use yii/base/Model
b. Create a class that inherits from the base class
c. Create the required variables
e. Define rules
f. Note that it is enclosed in []
For example:
<span>php namespace app\models; </span><span>use</span><span> yii\base\Model; </span><span>class</span> EntryForm <span>extends</span><span> Model { </span><span>public</span> <span>$name</span><span>; </span><span>public</span> <span>$email</span><span>; </span><span>public</span> <span>function</span><span> rules() { </span><span>return</span><span> [ [[</span>'name', 'email'], 'required'],<span> [</span>'email', 'email'],<span> ]; } }</span>This class inherits from a base class [[yiibaseModel]] provided by Yii, which is usually used to represent data
Added: [[yiibaseModel]] is used as the parent class of ordinary model classes and has nothing to do with data tables. [[yiidbActiveRecord]] is usually the parent class of a normal model class but is related to the data table (Translation: The [[yiidbActiveRecord]] class actually inherits from [[yiibaseModel]], adding database processing).
The
EntryForm
class contains two public members:name
andrules()
that returns a collection of data validation rules. The validation rule declared above means:Both the
name
andIf you have an
EntryForm
object that handles user-submitted data, you can call its [[yiibaseModel::validate()|validate()]] method to trigger data validation. If data validation fails, the [[yiibaseModel::hasErrors|hasErrors]] attribute will be set to true. If you want to know what error occurred, call [[yiibaseModel::getErrors|getErrors]].<span>php </span><span>$model</span> = <span>new</span><span> EntryForm(); </span><span>$model</span>->name = 'Qiang'<span>; </span><span>$model</span>->email = 'bad'<span>; </span><span>if</span> (<span>$model</span>-><span>validate()) { </span><span>//</span><span> 验证成功!</span> } <span>else</span><span> { </span><span>//</span><span> 失败! // 使用 $model->getErrors() 获取错误详情</span> }2. Create operation
Next you have to create an
entry
operation in thesite
controller for the new model. The creation and use of actions has been explained in the Say hello section.<span>php namespace app\controllers; </span><span>use</span><span> Yii; </span><span>use</span><span> yii\web\Controller; </span><span>use</span><span> app\models\EntryForm; </span><span>class</span> SiteController <span>extends</span><span> Controller { </span><span>//</span><span> ...其它代码...</span> <span>public</span> <span>function</span><span> actionEntry() { </span><span>$model</span> = <span>new</span><span> EntryForm; </span><span>if</span> (<span>$model</span>->load(Yii::<span>$app</span>->request->post()) && <span>$model</span>-><span>validate()) { </span><span>//</span><span> 验证 $model 收到的数据 // 做些有意义的事 ...</span> <span>return</span> <span>$this</span>->render('entry-confirm', ['model' => <span>$model</span><span>]); } </span><span>else</span><span> { </span><span>//</span><span> 无论是初始化显示还是数据验证错误</span> <span>return</span> <span>$this</span>->render('entry', ['model' => <span>$model</span><span>]); } } }</span>This operation first creates an
EntryForm
object. Then try to collect user-submitted data from$_POST
, which is collected by Yii's [[yiiwebRequest::post()]] method. If the model is successfully populated with data (that is, the user has submitted the HTML form), the operation will call [[yiibaseModel::validate()|validate()]] to ensure that the user submitted valid data.Added: The expression
Yii::$app
represents the application instance, which is a globally accessible singleton. At the same time, it is also a service locator, which can provide components with specific functions such asrequest
,response
,db
and so on. In the above code, therequest
component is used to access the$_POST
data received by the application instance.After the user submits the form, the operation will render a view named
entry-confirm
to confirm the data entered by the user. If the form is submitted without filling it out, or the data contains errors (Translator: such as the email format is incorrect), theentry
view will render the output, along with the form and the details of the validation error.Note: In this simple example we only present the confirmation page with valid data. In practice, you should consider using [[yiiwebController::refresh()|refresh()]] or [[yiiwebController::redirect()|redirect()]] to avoid the problem of repeated form submission.
3. Create a view
Finally create two view files
entry-confirm
andentry
. They will be rendered by theentry
operation just created. The
entry-confirm
view simply displays the submitted name and email data. View files are saved atviews/site/entry-confirm.php
.<span>php </span><span>use</span><span> yii\helpers\Html; </span>?> <p>You have entered the following information:</p>
- : = Html::encode($model->name) ?>
- : = Html::encode($model->email) ?>
The
entry
view displays an HTML form. View files are saved inviews/site/entry.php
<span>php </span><span>use</span><span> yii\helpers\Html; </span><span>use</span><span> yii\widgets\ActiveForm; </span>?> <?php <span>$form = ActiveForm::begin(); ?> = <span>$form</span>->field(<span>$model</span>, 'name') ?> = <span>$form</span>->field(<span>$model</span>, 'email') ?> <div>class="form-group"> = Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::<span>end(); ?>The view uses a powerful widget [[yiiwidgetsActiveForm|ActiveForm]] to generate HTML forms. Among them,
begin()
andend()
are used to render the opening and closing tags of the form respectively. The [[yiiwidgetsActiveForm::field()|field()]] method is used between these two methods to create the input box. The first input box is for "name" and the second input box is for "email". Then use the [[yiihelpersHtml::submitButton()]] method to generate a submit button.<span>use</span><span> yii\helpers\Html; </span><span>use</span> yii\wigets\ActiveForm;Remember to use widgets, you need to introduce these two
The above introduces the use of Forms in Yii, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools