引用原文章 感谢博主提供思路
public function actionAdd()
{
$request = Yii::$app->request;
if ($request->isPost) {
$params = $this->requestParams;
$pvs = new ParamsValidateService();
$valid = $pvs->validate($params, [
//[['start_at', 'end_at', 'expired_at', 'prize_expired_at'], 'date'],
[['title', 'banner', 'ball_frame_img', 'start_at', 'end_at', 'max_num', 'expired_at', 'prize_expired_at', 'share_title', 'share_description', 'share_img', 'rule_content'], 'required'],
[['title', 'banner', 'ball_frame_img', 'start_at', 'end_at', 'max_num', 'expired_at', 'prize_expired_at', 'share_title', 'share_description', 'share_img', 'rule_content','pre_content'], 'trim'],
['start_at', 'datetime', 'format' => 'php:Y-m-d H:i','timestampAttribute' => 'start_at'],
['end_at', 'datetime', 'format' => 'php:Y-m-d H:i','timestampAttribute' => 'end_at'],
['expired_at', 'datetime', 'format' => 'php:Y-m-d H:i','timestampAttribute' => 'expired_at'],
['prize_expired_at', 'datetime', 'format' => 'php:Y-m-d H:i','timestampAttribute' => 'prize_expired_at'],
['start_at', 'compare', 'compareAttribute' => 'end_at', 'operator' => '<', 'enableClientValidation' => false],
['end_at', 'compare', 'compareAttribute' => 'expired_at', 'operator' => '<', 'enableClientValidation' => false],
['expired_at', 'compare', 'compareAttribute' => 'prize_expired_at', 'operator' => '<', 'enableClientValidation' => false],
['max_num', 'compare', 'compareValue' => 0, 'operator' => '>'],
['title', 'string', 'length' => [2, 13]],
], LuckyActivity::setAttributeLabels());
if (!$valid) {
return $this->error(null, $pvs->getFirstErrorSummary());
}
$this->luckyActivityLogic->add($this->requestParams);
return $this->success(null,"添加成功");
} else {
return $this->error(null, '请使用POST请求');
}
}
model = new ParamsValidateModel();
}
/**
* @param array $data 数据项
* @param array $rules 验证规则
* @param array $attributeLabels
* @return bool
*/
public function validate(&$data, $rules, $attributeLabels)
{
$this->model->reloadAttributeLabels($attributeLabels);
// 添加验证规则
$this->model->setRules($rules);
// 设置参数
$this->model->load($data, '');
// 进行验证
$valid = $this->model->validate();
// 覆盖值,使 default 验证器生效。
$data = $this->model->attributes;
return $valid;
}
/**
* 获取第一条验证错误消息内容
* @return mixed
*/
public function getFirstErrorSummary()
{
$errors = $this->getErrorSummary(false);
return current($errors);
}
public function __call($name, $params)
{
if ($this->model->hasMethod($name)) {
return call_user_func_array([$this->model, $name], $params);
} else {
return parent::__call($name, $params);
}
}
}
_rules = $rules;
foreach ($rules as $item) {
$this->_visionAttributes = array_unique(array_merge($this->_visionAttributes, (array)$item[0]));
}
}
public function reloadAttributeLabels($data){
$this->_attributeLabels = $data;
}
// 重写获取验证规则
public function rules()
{
return $this->_rules;
}
// 设置可用属性列表
public function attributes()
{
return $this->_visionAttributes;
}
public function attributeLabels()
{
return $this->_attributeLabels;
}
}
class LuckyActivity extends BaseModel
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
];
}
}
public static function setAttributeLabels()
{
return [
'title' => Yii::t('app', '活动名称'),
'banner' => Yii::t('app', '活动横幅图'),
'start_at' => Yii::t('app', '活动开始时间'),
'end_at' => Yii::t('app', '活动结束时间'),
'max_num' => Yii::t('app', '最多获得张数'),
'expired_at' => Yii::t('app', '预期作废时间'),
'prize_expired_at' => Yii::t('app', '自动放弃时间'),
];
}
public function beforeValidate()
{
parent::beforeValidate(); // TODO: Change the autogenerated stub
$this->start_at = strtotime($this->start_at);
$this->end_at = strtotime($this->end_at);
$this->expired_at = strtotime($this->expired_at);
$this->prize_expired_at = strtotime($this->prize_expired_at);
return true;
}
public function afterFind()
{
parent::afterFind(); // TODO: Change the autogenerated stub
$this->start_at = date("Y-m-d H:i", $this->start_at);
$this->end_at = date("Y-m-d H:i", $this->end_at);
$this->expired_at = date("Y-m-d H:i", $this->expired_at);
$this->prize_expired_at = date("Y-m-d H:i", $this->prize_expired_at);
}
网上的有通过行为实现时间的转化,有空的时候在实践一下。文章思路