peng
controller:
$model = new RegForm(); $model->setScenario ('add'); if ( $model->load ( Yii::$app->request->post () ) ) { if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; $model->setScenario ('ajax'); return ActiveForm::validate($model); } // ... if($model->signUp()){ return $this->redirect(['site/login']); } }
model:
public function rules () { return [ [ [ 'email', 'verifycode' ], 'filter', 'filter' => 'trim', 'on'=>['add', 'ajax'] ], [ [ 'email', 'password', 'verifycode' ], 'required', 'message' => '不能为空', 'on'=>['add', 'ajax'] ], [ 'email', 'email', 'message' => '邮箱格式不正确', 'on' => [ 'add', 'ajax' ] ], [ 'verifycode', 'captcha', 'message' => '验证码不正确', 'on'=>['add'] ], ]; }
view:
<?php $form = ActiveForm::begin ( [ 'id' => 'reg-form', 'enableAjaxValidation'=>true, 'options' => [ 'class' => 'form-horizontal'], ] ); ?> ...
大概是这样
@pengcx 不大明白你什么意思,是实现弹窗还是获取错误信息, 如果是获取错误信息的话, model验证的错误信息都在 $model->errors里面, 你dump一下看看就知道了;
- peng 收藏了教程Yii2 的 redis 应用
- peng 回答了问题 求大神帮忙看看这个结构设计是否有问题:
<?php // 促销抽象类 (Component: 组件对象接口) abstract class Promotion { public $price; // 统计优惠价格 // 计算价格 abstract public function calePrice(); } // 原始订单 (ConcreteComponent: 具体的组件对象, 实现了组件接口, 被装饰器装饰的原始对象) class Order extends Promotion { public $goodsInfo; public function __construct ($goodsInfo) { $this->goodsInfo = $goodsInfo; } public function calePrice() { // 计算商品价格 foreach($this->goodsInfo as $key=>$val) { $this->price += $val['price']; } return $this->price; } } // 装饰器: 父类 (Decorator: 所有装饰器的父类, 需要定义一个与组件接口一致的接口) class Decorator extends Promotion { public $promotion; public $goodsInfo; public function __construct(Promotion $promotion) { $this->promotion = $promotion; $this->goodsInfo = $promotion->goodsInfo; } public function calePrice(){ } } // 装饰器: 代金券 (ConcreteDecorator: 具体的装饰器类) class CashCoupon extends Decorator { public function calePrice() { foreach($this->goodsInfo as $key=>$val) { if($val['couponType'] == 1) { $this->price += $val['couponVal']; } } return $this->promotion->calePrice() - $this->price; } } // 装饰器: 打折券 (ConcreteDecorator: 具体的装饰器类) class DiscountCoupon extends Decorator { public function calePrice(){ foreach($this->goodsInfo as $key=>$val) { if($val['couponType'] == 2) { $this->price += $val['price'] * (1 - $val['couponVal']); } } return $this->promotion->calePrice() - $this->price; } } // 装饰器: 免单券 (ConcreteDecorator: 具体的装饰器类) class FreeCoupon extends Decorator { public function calePrice(){ foreach($this->goodsInfo as $key=>$val) { if($val['couponType'] == 3) { $this->price += $val['price']; } } return $this->promotion->calePrice() - $this->price; } } // 装饰器: 满减 (ConcreteDecorator: 具体的装饰器类) class MoneyOff extends Decorator { // 满减规则, atLeastAmount: 满减金额, amountOff: 减免金额/优惠金额 private $moneyOffRules = [ ['atLeastAmount'=> 1000, 'amountOff'=> 10, 'start'=>0, 'end'=>1000], ['atLeastAmount'=> 2000, 'amountOff'=> 20, 'start'=>1000, 'end'=>2000], ['atLeastAmount'=> 3000, 'amountOff'=> 30, 'start'=>2000, 'end'=>3000], ['atLeastAmount'=> 4000, 'amountOff'=> 40, 'start'=>3000, 'end'=>4000], ]; public function calePrice() { $price = $this->promotion->calePrice(); foreach($this->moneyOffRules as $key=>$val) { if($price >= $val['start'] and $price < $val['end']) { $this->price = $val['amountOff']; } } return $price - $this->price; } } // couponType: 优惠券类型( 1-代金券, 2-折扣券, 3-免单券) // couponVal : 优惠金额或折扣 $goodsInfo = [ ['id'=>1, 'goodsName'=>'商品01', 'price'=>1000, 'couponType'=>1, 'couponVal'=>100], ['id'=>2, 'goodsName'=>'商品02', 'price'=>1000, 'couponType'=>2, 'couponVal'=>0.5], ['id'=>3, 'goodsName'=>'商品03', 'price'=>1000, 'couponType'=>3, 'couponVal'=>0], ['id'=>4, 'goodsName'=>'商品04', 'price'=>1000, 'couponType'=>0, 'couponVal'=>0], ['id'=>5, 'goodsName'=>'商品05', 'price'=>1000, 'couponType'=>2, 'couponVal'=>0.6], ]; $order = new Order($goodsInfo); // 5000 $order = new CashCoupon($order); // 使用优惠方式: 代金券 4900 $order = new DiscountCoupon($order); // 使用优惠方式: 打折券 4000 $order = new FreeCoupon($order); // 使用优惠方式: 免单券 3000 $order = new MoneyOff($order); // 使用优惠方式: 满减 2960 echo $order->calePrice(); // 3000
- peng 回答了问题 yii2 ajax登录
controller:
$model = new RegForm(); $model->setScenario ('add'); if ( $model->load ( Yii::$app->request->post () ) ) { if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; $model->setScenario ('ajax'); return ActiveForm::validate($model); } // ... if($model->signUp()){ return $this->redirect(['site/login']); } }
model:
public function rules () { return [ [ [ 'email', 'verifycode' ], 'filter', 'filter' => 'trim', 'on'=>['add', 'ajax'] ], [ [ 'email', 'password', 'verifycode' ], 'required', 'message' => '不能为空', 'on'=>['add', 'ajax'] ], [ 'email', 'email', 'message' => '邮箱格式不正确', 'on' => [ 'add', 'ajax' ] ], [ 'verifycode', 'captcha', 'message' => '验证码不正确', 'on'=>['add'] ], ]; }
view:
<?php $form = ActiveForm::begin ( [ 'id' => 'reg-form', 'enableAjaxValidation'=>true, 'options' => [ 'class' => 'form-horizontal'], ] ); ?> ...
大概是这样
- peng 2017-11-13 已签到连续签到1天,获得了5个金钱
- peng 收藏了话题Yii2的表单验证之三:Ajax验证
- peng 2017-11-07 已签到连续签到1天,获得了5个金钱