CUniqueValidator

I have a small table (10 rows) with 2 fields, ID (smallint) and name (varchar 20)

ID is the primary key but not autoincremented

A user can enter/modify the ID and NAME, so I have to check that entered text for ID is numeric and that it’s UNIQE

I set these rules in my model:




        return array(

            array('id, name'),

            array('id','numerical','integerOnly'=>'true'),

            array('id','unique'),

            array('name', 'length', 'max'=>20),

        );



The problem is if the entered text for ID is not a number (eg. "1x")… then the numerical validator sets the error, but the UNIQUE validator IS executed and I get a CDbException !

To solve this I extender the CUniqueValidator like this:




class myUniqueValidator extends CUniqueValidator

{

    protected function validateAttribute($object,$attribute)

    {

        if(!$object->hasErrors($attribute))

            parent::validateAttribute($object,$attribute);

    }

}



but I think this check should be added to the source of CUniqueValidator so that it’s not executed if there already are some errors for that attribute.

What is the SQL statement being executed for uniqueness check? What DBMS are you using?

I’m out of office right now and there is the code, I will post here the statment as soon as I can…

I’m using postgreSQL.

Seems that it’s a behavior of the postgres database

The SQL is very simple:




SELECT "id" FROM "pcentar" "t" WHERE "id"=:value LIMIT 1



here is the error:




exception 'CDbException' with message 'CDbCommand failed to execute the SQL

statement: SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid

input syntax for integer: "5x"' in

/srv/www/htdocs/yii/framework/db/CDbCommand.php:375



I tried to run this query in phpPgAdmin for postgres and to compare in phpMyAdmin for mysql, same table ID integer

In postgres I get the error: ERROR: invalid input syntax for integer: "5x"

but in mysql I get an empty set without error.

Could you please create a ticket for this issue? Thanks!

Done.

http://code.google.com/p/yii/issues/detail?id=959