简体   繁体   English

如何通过通过 REST 调用发送的 POST 请求(没有 ActiveForm)使用模型验证数据 - Yii2

[英]How to validate data with a Model by POST request sent over REST call(without ActiveForm) - Yii2

I am creating a Yii application in which data is being sent over POST request to my server via REST calls.我正在创建一个Yii应用程序,其中数据通过 REST 调用通过POST请求发送到我的服务器。 As there is no ActiveForm coming in the scenario, what is the best approach to validate data with Model class for which data is being sent and after successful validation, it will be saved into db table.由于场景中没有ActiveForm ,因此使用正在发送数据的 Model 类验证数据的最佳方法是什么,并在成功验证后将其保存到 db 表中。

I had gone through official docs but didn't find anything related.我浏览了官方文档,但没有找到任何相关内容。 As inbuilt approach is given for ActiveForm $model->validate() , I am wondering if something like this is there as easy as calling a function for the data posted without ActiveForm to validate that data with my Model class.由于为ActiveForm $model->validate()提供了内置方法,我想知道这样的事情是否像调用一个函数来发布没有ActiveForm的数据一样简单,以使用我的Model类验证该数据。

I know it can be done with isset($_POST['somefield'] but it is a bit traditional approach & to be repeated for each field (personally I feel it's a boring activity to check if data is posted in a field and then perform validation for each field separately).我知道它可以用isset($_POST['somefield']来完成,但它有点传统的方法 & 对每个字段重复(我个人觉得检查数据是否发布在一个字段中然后执行是一项无聊的活动)分别对每个字段进行验证)。

Is there any official doc which I missed?有没有我错过的官方文档? Or if it is not given in doc, what would be the best approach?或者如果它没有在文档中给出,最好的方法是什么?

Suggest me something to create code magic..建议我一些东西来创造代码魔法..

Okay, After doing some more research I found solution to the said problem as below:好的,经过更多研究,我找到了上述问题的解决方案,如下所示:

    $model = new AppDetails();
    $model->setAttributes(Yii::$app->request->post(), false);
    if($model->validate() && $model->save())
    {
        return true;
    }
    return false;

As per Yii docs, setAttributes() does Sets the attribute values in a massive way.根据Yii文档, setAttributes()确实以大量方式设置属性值。 Here, I created new object for my model class and then sets attributes coming over post request.在这里,我为我的模型类创建了新对象,然后设置了来自 post 请求的属性。

Once I set the attributes to my model class object, I could then validate() them and also save them directly in db just calling save() applicable only after successful validation.一旦我将属性设置为我的模型类对象,我就可以validate()它们并将它们直接保存在数据库中,仅在成功验证后才调用save()适用。

However, with this you must have to send all attributes as per rules section of your model (including primary key auto incremented values. Also you can set them later if you are not sending it over rest call like $model->id='' ).但是,有了这个,您必须根据模型的规则部分发送所有属性(包括主键自动递增值。如果您不通过$model->id=''类的休息调用发送它,您也可以稍后设置它们)。 Otherwise, validate() will return false.否则, validate()将返回 false。

Being more specific about $model->load() it returns true if data is posted from a Form (as per what I understood from my coding experience) with fields set with object of Model class (see more here ).更具体地讲$model->load()如果数据是从Form发布的(根据我从我的编码经验中了解到的),并且字段设置为Model 类的对象,则它返回 true(请参阅此处的更多信息)。 load() automatically then loads the formName in background scenario set by Yii developers. load()然后在 Yii 开发人员设置的后台场景中自动加载formName

You may try doing like $model->load(Yii::$app->request->post()) with data sent over REST call, It will always return false because attributes are not set.您可以尝试像$model->load(Yii::$app->request->post())使用通过 REST 调用发送的数据,它总是会返回 false,因为未设置属性。

I think I have explained solution to my above said problem.我想我已经解释了上述问题的解决方案。 Do point out in case anything is wrong or not well understandable.如果有任何错误或无法很好理解,请指出。

I hope it helps future readers with same issue.我希望它可以帮助未来有同样问题的读者。

What's wrong with a traditional...传统的有什么不好...

if ($model->load(Yii::$app->request->post()) && $model->validate()) {
    //$model loaded with post data and validated
}

...? ……?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM