yii中ajax令牌验证,Ajax 验证 Trait

今天在搜索 『Yii2 performAjaxValidation 』,然后发现一个挺好用的 Trait,分享给大家

/*

* This file is part of the Dektrium project

*

* (c) Dektrium project

*

* For the full copyright and license information, please view the LICENSE.md

* file that was distributed with this source code.

*/

namespace app\traits;

use yii\base\Model;

use yii\web\Response;

use yii\widgets\ActiveForm;

/**

* @author Dmitry Erofeev

*/

trait AjaxValidationTrait

{

/**

* Performs ajax validation.

*

* @param Model $model

*

* @throws \yii\base\ExitException

*/

protected function performAjaxValidation(Model $model)

{

if (\Yii::$app->request->isAjax && $model->load(\Yii::$app->request->post())) {

\Yii::$app->response->format = Response::FORMAT_JSON;

\Yii::$app->response->data = ActiveForm::validate($model);

\Yii::$app->response->send();

\Yii::$app->end();

}

}

}

本文由 forecho 创作,采用 知识共享署名 3.0 中国大陆许可协议 进行许可。

可自由转载、引用,但需署名作者且注明文章出处。

打赏作者

如果这篇文章对您有帮助,不妨微信小额赞助我一下,让我有动力继续写出高质量的教程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值