Yii2。什么特殊的html数据\ Yii \ grid \ dataColumn :: filter需要什么?

时间:2016-06-04 06:09:44

标签: yii2 yii2-model yii2-validation

我尝试使用HTML并且此DataColum正常工作,输入字段会进行过滤,但在发生时不会显示验证错误。我的意思是GridView没有向元素添加.has-error等。

控制器/动作



 public function actionIndex()
    {
    $searchModel = new OrderSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $dataProvider->sort->defaultOrder = ['created_at' => SORT_DESC];

    return $this->render('index', [
    'searchModel' => $searchModel,
    'dataProvider' => $dataProvider,
    ]);
    }






 public function actionIndex()
    {
        $searchModel = new OrderSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->sort->defaultOrder = ['created_at' => SORT_DESC];

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }




GridView的



<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
    //['class' => 'yii\grid\SerialColumn'],
    [
    'attribute' => 'id',
    'filter' => false,
    ],
    [
    'attribute' => 'number',
    'filter'=> '<input type="text" name="OrderSearch[number]" class="form-control">',
    'format' => 'raw',
    'value' => function($model) {
    //return sprintf('%08d', $model->number);
    return Html::a(sprintf('%08d', $model->number), ['customer/orders', 'id'=>$model->customer->id, 'highlight'=>$model->id]);
    }],
    //['class' => 'yii\grid\ActionColumn'],
    ],
    ]); ?>
&#13;
&#13;
&#13;

SearchModel

&#13;
&#13;
   <?php

    namespace app\models;

    use Yii;
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    use app\models\Order;

    /**
     * OrderSearch represents the model behind the search form about `app\models\Order`.
     */
    class OrderSearch extends Order
    {

        public $createdAtDate;
        public $createdAtDateMachineFormat;
        public $createdAtTime;
        public $createdAtTimeMachineFormat;


        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['id', 'number', 'status', 'customer_id'], 'integer'],];
        }

        public function attributeLabels()
        {
            return [
                'createdAtDate' => 'Creado en',
                'createdAtTime' => 'Hora',
                'number' => 'Número de Orden',
            ];
        }

        /**
         * @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 = Order::find();

            $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;
            }

    $query->andFilterWhere([
    'id' => $this->id,
    'number' => $this->number,
    'status' => $this->status,
    //'created_at' => $this->created_at,
    'finished_at' => $this->finished_at,
    'customer_id' => $this->customer_id,
    ]);

    return $dataProvider;
    }
    }
&#13;
&#13;
&#13;

模型

&#13;
&#13;
<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "order".
 *
 * @property integer $id
 * @property integer $number
 * @property integer $status
 * @property integer $created_at
 * @property integer $finished_at
 * @property integer $customer_id
 *
 * @property Customer $user
 */
class Order extends \yii\db\ActiveRecord
{
    
    const STATUS_IN_PROCESS = 0;
    const STATUS_COMPLETED = 1;
    
    public $created_at_date;
    public $created_at_time;
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[/*'id', */'number', /*'status', 'created_at', 'customer_id'*/], 'required'],
            [[/*'id',*/ 'number', /*'status', 'created_at', 'finished_at', */'customer_id'], 'integer'],
            [['number'], 'unique']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'number' => 'Numero de Orden',
            'status' => 'Estado',
            'created_at' => 'Creado en',
            'finished_at' => 'Finalizado en',
            'customer_id' => 'ID Cliente',
            'createdAtDate' => 'Creado en',
            'createdAtTime' => 'Hora',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $this->isNewRecord ? $this->created_at = gmdate('Y-m-d H:i:s') : null;
            return true;
        } else {
            return false;
        }
    }
    
    

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如果您需要过滤器值,则必须在过滤器键中使用,但过滤器值是以编程方式控制器,您可以使用此方式的链接来获取结果,避免过滤器条目中的输入标记。

Url::to(['/your-controller/your-action', 'YourModelSearch' => ['number'=> 'your_value']])