Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values array validation (from multiple select) #3376

Closed
RomeroMsk opened this issue May 6, 2014 · 13 comments
Closed

Values array validation (from multiple select) #3376

RomeroMsk opened this issue May 6, 2014 · 13 comments

Comments

@RomeroMsk
Copy link
Contributor

I have model Order with attribute status_id. This attribute must have only one of this values: [1, 2, 3]. I want to make search model and form with multiple select tag, which correspond to status_id attribute. What validation rule can I use for status_id to allow multiple values (to call Order::find()->andFilterWhere(['in', 'status_id', $this->status_id]) in search model) and validate each of them against an array [1, 2, 3]?
I think this is common usecase, so if Yii doesn't have an out-of-box solution for this, it needs an enhancement.

@cebe
Copy link
Member

cebe commented May 6, 2014

duplicate of #987 and #3191
@qiangxue this has been requested multiple times now, should we add a validator for this?

@RomeroMsk
Copy link
Contributor Author

It wiil be very usefull to have universal validator, which we can use with any other validator. Not only range. For example:

[['status_id'], 'each', 'filter', 'filter' => 'trim']

or

[['status_id'], 'each', 'integer']

Search forms with multiple selects or checkbox groups are very common, imo.

@qiangxue
Copy link
Member

qiangxue commented May 6, 2014

Which validators need array support?

@RomeroMsk
Copy link
Contributor Author

In my case: range and integer. Also it will be usefull for filter.

@lynicidn
Copy link
Contributor

lynicidn commented May 7, 2014

it good idea for multiselect :)

[['status_id'], 'each', 'in', 'range' => array_keys(static::getStatusLabels())]

@bryglen
Copy link

bryglen commented May 7, 2014

Imo. This is common scenario, i also encounter this scenario on Yii1 for 3-4projects. Would be good if this is on the core. 👍 for this.

@tecnologiaterabyte
Copy link

I have the same problem described, which was the best found solution, since I have searched and read and I can not find solution to the issue, I opened the question in the forum and facebook and no answer, I would like to be able to establish this enhancement.

Solved use extension https://github.com/mickgeek/yii2-actionbar for filter multiple gridview via ajax.

@lynicidn
Copy link
Contributor

i solve via rules

['name', 'exist', 'allowArray' => true, 'when' => function ($model, $attribute) {return is_array($model->$attribute);}],

and in search if string do explode by comma and trim and use $query->andFilterWhere(['name' => $name]) else $query->andFilterWhere(['like', 'name', $this->name])

Notice: need do implode by comma to string before render value into input filter

@tecnologiaterabyte
Copy link

thank you very much for the reply, I commented that the extension because besides making a multiple filter in the gridview, I can also filter the checkboxcolumn to delete selected records, download selected records.

mickgeek/yii2-actionbar#2

Solution provided by: @mickgeek

I hope it will serve as a guide to someone.

@klimov-paul
Copy link
Member

Resolved by #8023

@RomeroMsk
Copy link
Contributor Author

@klimov-paul, thanks!

@tecnologiaterabyte
Copy link

👍

@klimov-paul
Copy link
Member

You are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants