ajax - Why I am getting error message when using Yii CActiveForm::validate() with array -
i have problem related cactiveform::validate()
. have form , and sending data database using ajax, form contains multiple selectable drop-down list. in data saving section of controller produced following error
mb_strlen() expects parameter 1 string, array given (.../framework/validators/cstringvalidator.php:84)
and after updating framework newer version, error gone, , got below validation message instead.
category ids invalid.
if form filled(i mean rules in model satisfied), not produce such bug or error message.
controller action
public function actioncompany() { $model = new company; if (isset($_post['company'])) { $model->attributes = $_post['company']; $category_ids = ""; if (is_array($_post['company']['category_ids'])) $category_ids = implode(',', $_post['company']['category_ids']); $model->category_ids = $category_ids; if ($model->validate()) { /*$temp = company::model()->findbypk($model->id); if ($temp !== null) { $model = $temp; }*/ $model->save(); echo cjson::encode(array('status' => 'success')); yii::app()->end(); } else { $error = cactiveform::validate($model); if ($error != '[]') echo $error; } } }
model rules
public function rules() { return array( array('...., category_ids,...', 'required'), array('..., category_ids, ...', 'length', 'max'=>255), .... ..... array('...., category_ids,...', 'safe', 'on'=>'search'), ); }
what i'm missing?
by default, cactiveform::validate($model)
loads model attributes $_post
, overrides current attribute values, destroying transformed values. pass false
third argument avoid this.
Comments
Post a Comment