gridview - 具有 beforeValidate() 值的 yii2 gridview 过滤器

标签 gridview yii2

我有一个 yii2 GridView,由 gii CRUD 生成。

我将浏览器指向 /model/index。我在 GET 字符串中包含任何搜索值。但是 GridView 筛选器预填充了值。 enter image description here

即使我尝试删除或替换这些过滤器值,预先填充的值也会弹回。

我注意到这些预填充值来自模型的 beforeValidate() 方法。

public function beforeValidate()
{
    if (parent::beforeValidate()) {
        if ($this->isNewRecord) {
            $this->created_at = time();
            $this->b_soft_deleted = 0;
        }
        $this->updated_at = time();
        return true;
    }           
    return false;
}

显然,GridView 过滤器调用 beforeValidate() 并将这些值用于过滤器...

  • 为什么会这样?
  • 我可以做些什么来获得过滤值的清白记录?

(在 php 7.0.14 上运行 yii2 2.0.10)

更新 我的搜索模型是

<?php

namespace common\models\generated;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Article;

/**
 * ArticleSearch represents the model behind the search form about     `common\models\Article`.
 */
class ArticleSearch extends Article
{
/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['id', 'title', 'description', 'positive_keywords', 'negative_keywords'], 'safe'],
        [['created_at', 'updated_at', 'b_soft_deleted'], 'integer'],
    ];
}

/**
 * @inheritdoc
 */
public function scenarios()
{
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search($params)
{
    $query = Article::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
        'b_soft_deleted' => $this->b_soft_deleted,
    ]);

    $query->andFilterWhere(['like', 'id', $this->id])
        ->andFilterWhere(['like', 'title', $this->title])
        ->andFilterWhere(['like', 'description', $this->description])
        ->andFilterWhere(['like', 'positive_keywords', $this->positive_keywords])
        ->andFilterWhere(['like', 'negative_keywords', $this->negative_keywords]);

    return $dataProvider;
}

最佳答案

您正在 beforeValidate() 中设置属性,这些属性在调用 $this->validate() 时应用(无论验证结果如何)。

你需要把这个集合移到别处或者...

如果我猜对了,你确实设置了 created_atupdated_at 因为这些字段在你的数据库中被标记为 not null 并且因为gii 为它们生成了 required 规则。
处理此类属性的正确方法是删除它们的 required 规则并添加 yii\behaviors\TimestampBehavior 负责在模型保存步骤中设置它们。
这样您就不会看到模型的验证错误,也不必手动设置这些错误并且数据库列已正确填充。

整个 GridView 问题都消失了。

关于gridview - 具有 beforeValidate() 值的 yii2 gridview 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42021165/

相关文章:

android - 如何从android中的sdcard获取指定的视频?

gridview - 带标题的 Flutter GridView

php - Yii2 用户模型与关系表

php - Yii2 中的 $with 和 $joinWith 有什么区别以及何时使用它们?

c# - 无法获取嵌套在 formview 中的 gridview 上的单元格值

c# - gridview 中的分页

php - 在 yii2 gridview 中输入时进行过滤

azure - Office 365 访问已被条件访问策略阻止。访问策略不允许发行 token

yii2 - 如何从 Yii2 basic 禁用 Bootstrap ?

php - 测试PDF文件Codeception