[Enh] Gii could filter ENUM fields (rules and forms) #292

Closed
qiangxue opened this Issue Feb 15, 2012 · 8 comments

Comments

Projects
None yet
7 participants
@qiangxue
Member

qiangxue commented Feb 15, 2012

As for now, Yii treats ENUM columns as normal string fields: it finds the widest possible value and uses it as max length. But it would be easy for Gii to add a rule that checks if the value entered is among the authorized ones of the ENUM.

Here is an ugly hack as a proof of concept (there's no visible doc on the allow syntax of this description, I hope it will appear below):

foreach ($columns as $name => $column) {
    if (strncmp($column->dbType, 'enum', 4) === 0 && preg_match('/\((.*)\)/', $column->dbType, $matches)) {
        $values = str_replace("''", "\'", $matches[1]); // won't work if there are "\" characters
        echo "\t\t\tarray('{$name}', 'in', array({$values})), // enum\n";
    }
}

It won't work with some pathological values of the enum, but it doesn't matter. The developper can fix thoses rare cases himself. The model would still be DB-vendor independant, as this only affects Gii.

Though this works, I believe it could be better if CDbColumnSchema had a "$enum = null" attribute. It would then allow CrudCode::generateActiveField() to use it and build a HTML select for the enum fields. This would enhance the view "_form.php" produced by the default Gii CRUD templates. CGridView could also use this for its default filters.

There have been several posts about this in the forums, but AFAIK with no word from the staff. If you feel this enhancement is useful but have more urgent tasks, I could submit a patch (or a git fork).

Migrated from http://code.google.com/p/yii/issues/detail?id=2945


earlier comments

qiang.xue said, at 2012-01-01T03:37:10.000Z:

set for 1.1.10 milestone

qiang.xue said, at 2012-01-01T03:37:36.000Z:

set for 1.1.10 milestone

@samdark

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Mar 9, 2012

Member

This one can be useful.

Member

samdark commented Mar 9, 2012

This one can be useful.

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Apr 27, 2012

Member

@cebe will try to work on this when I find some time...

Member

cebe commented Apr 27, 2012

@cebe will try to work on this when I find some time...

@leokolln

This comment has been minimized.

Show comment
Hide comment
@leokolln

leokolln Jul 18, 2012

So.. no news on this?

So.. no news on this?

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Jul 19, 2012

Member

nope, sorry. Possibility for me finding time is near to zero.

Member

cebe commented Jul 19, 2012

nope, sorry. Possibility for me finding time is near to zero.

@SL1210

This comment has been minimized.

Show comment
Hide comment
@SL1210

SL1210 Sep 13, 2012

Would love this functionality please!

SL1210 commented Sep 13, 2012

Would love this functionality please!

@enigmatix

This comment has been minimized.

Show comment
Hide comment
@enigmatix

enigmatix Jun 1, 2014

@cebe cebe I would deploy this an enum field as a dropdown through Gii. I've written some funcitonal code for it, and would love to contribute actively to this - especially if you don't have time yourself.

@cebe cebe I would deploy this an enum field as a dropdown through Gii. I've written some funcitonal code for it, and would love to contribute actively to this - especially if you don't have time yourself.

@samdark

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Jun 1, 2014

Member

Would be great. Please prepare a pull request and we'll check it.

Member

samdark commented Jun 1, 2014

Would be great. Please prepare a pull request and we'll check it.

qiangxue pushed a commit to yiisoft/yii2-gii that referenced this issue Jun 4, 2014

Added ability to generate dropdown from Enum field
Alternatively, if statement could be: if(substr($column->dbType, 0, 4) === 'enum'), however expected implemented option to be more robust across other database formats.

Created after followup on this issue:  yiisoft/yii#292 (comment)
@evan-king

This comment has been minimized.

Show comment
Hide comment
@evan-king

evan-king Jul 24, 2014

I recently wanted to use an enum but felt the lack of correct validation in our auto-generated models (no tweaking allowed) in conjunction with MySQL's silent acceptance of invalid inputs lent too great a vector for coding errors.

Though I'd like to contribute code against this, our resources are currently too constrained for it - perhaps I can set aside some personal time for it but that's a long shot. As a result, we have ended up using a lookup table just for a single "context-explicit values" case of the dreaded 3-value boolean storage.

Mark me among those wishing for more robust and restrictive enum handling.

I recently wanted to use an enum but felt the lack of correct validation in our auto-generated models (no tweaking allowed) in conjunction with MySQL's silent acceptance of invalid inputs lent too great a vector for coding errors.

Though I'd like to contribute code against this, our resources are currently too constrained for it - perhaps I can set aside some personal time for it but that's a long shot. As a result, we have ended up using a lookup table just for a single "context-explicit values" case of the dreaded 3-value boolean storage.

Mark me among those wishing for more robust and restrictive enum handling.

@samdark samdark closed this Nov 17, 2015

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