Yii Framework Forum: Cactiveform::dropdownlist + 'default' Validation Rule Bug - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cactiveform::dropdownlist + 'default' Validation Rule Bug I guess I found small bug connected to forms, dropdown lists and model Rate Topic: -----

#1 User is offline   sowiq 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 26-April 12

Posted 04 December 2012 - 07:25 PM

Hi Everybody,

I guess I found small bug connected to forms, dropdown lists and model validation.

How to reproduce this?

1. Create simple model with two properties, e.g. like below:

// class Example extends CActiveRecord
public function rules()
{
    return array(
        array('name', 'length', 'max' => 20),
        array('country_id', 'default', 'value' => new CDbExpression('NULL')),
    );
}


2. Create simple form, without ajax validation:
/* @var $form   CActiveForm */
/* @var $model  Example */
/* @var $data   array */

echo $form->textField($model, 'name');
echo $form->error($model, 'name');

echo $form->dropDownList($model, 'country_id', $data);
echo $form->error($model, 'country_id');



Let's suppose that controller is set up properly - it sets all properties to object of Example class and calls save() method on it. On validation errors, it renders form view again with error messages.

Everything is fine so far.

Now, enter text with length above 20 characters into 'name' field, leave 'country_id' empty and submit your form. Application will crash with this error message:
Property "CDbExpression.primaryKey" is not defined. 


This is because value of 'country_id' is after validation set to new CDbExpression('NULL'), which cannot be used as selected item of dropdown.

Solution is quite simple. Inside CHtml::activeDropDownList I changed:

// this line
$selection=self::resolveValue($model,$attribute);
// into this
$selection=(string)self::resolveValue($model,$attribute);


Additional information:
- operating system - Windows 7 64bit
- Web server - Apache on Xampp
- browser type - Firefox 17.0.1
- Yii version - 1.1.12

Regards,
KS

Attached File(s)


0

#2 User is offline   stparris 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 21-February 13

Posted 23 February 2013 - 09:50 PM

Thanks, this was very helpful!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users