YII2 How to verify the form data submitted by the app client

Source: Internet
Author: User
Tags yii
Title.... Because just started to learn YII2 framework, know the model of the Rules method can fill in the validation rules, but he seems to only verify the PC-side page with ActiveForm generated form data, then the mobile client submitted by the form data can also use the rules method of validation rule?

Reply content:

Title.... Because just started to learn YII2 framework, know the model of the Rules method can fill in the validation rules, but he seems to only verify the PC-side page with ActiveForm generated form data, then the mobile client submitted by the form data can also use the rules method of validation rule?

He seems to be validating only the form data generated by the PC-side page with ActiveForm.

The verdict is wrong.

I guess you may have a problem, the key is to assign a value in the block?

In Yii2, ActiveForm you will default to the current generated form definition one 名称 , yes, as you can see:

Then LoginForm you can do this when you receive the data:

$form = new LoginForm;$form->load(Yii::$app->requeset->post());

And the data on the phone to the server side is long like this?

[    'username' => 'beep',    ...]

So we can do this when we use the form to receive:

$form->load(Yii::$app->request->post(), '');//亦或$form->setAttributes(Yii::$app->request->post());

Then, verify that:

$form->validate();var_dump($form->errors);

To familiarize yourself with the documentation?

    • https://github.com/yiisoft/yii2/blob/master/docs%2fguide%2fstructure-models.md#validation-rules-

    • https://github.com/yiisoft/yii2/blob/master/docs%2fguide%2fstructure-models.md#massive-assignment-

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.