0

I have a problem related to CActiveForm::validate(). I have a form and and sending data to database using Ajax, my form contains a multiple selectable drop-down list. In data saving section of controller produced the following error initially

mb_strlen() expects parameter 1 to be string, array given (.../framework/validators/CStringValidator.php:84)

and after updating framework to newer version, that error gone, and got the below validation message instead.

Category Ids is invalid.

If the form is fully filled(I mean all the rules in the model satisfied), it will not produce any 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 is actually I'm missing?

11
  • 1) What does the model's rules() method look like? 2) Storing multiple values in a single DB column is really bad design and will cause you a lot of problems in the future. Commented Sep 4, 2013 at 3:48
  • 1.Updated my question. 2. But I don't feel like for storing the categories needs different table in this case, If Iam going for that;it'll results a table contains lots of records, with repeated company_id ? I'm not sure.. feels like this is good na? Commented Sep 4, 2013 at 3:55
  • I'm not sure I understand your current problem. If you fill out the form properly, you get no errors, if you fill it out incorrectly, you get that error message... where's the issue? Commented Sep 4, 2013 at 4:06
  • Multiple records in one field is really bad for anything other than dumb storage - for starters, you cannot effectively search for companies associated with specific categories, or join the categories table and preload the categories with the company info. Commented Sep 4, 2013 at 4:07
  • 1
    Figured it out. By default, CActiveForm::validate() loads the model attributes from $_POST and overrides current attribute values, thus destroying your work. Pass false as the third argument to avoid this. Commented Sep 4, 2013 at 4:48

1 Answer 1

2

By default, CActiveForm::validate($model) loads the model attributes from $_POST and overrides current attribute values, thus destroying your transformed values. Pass false as the third argument to avoid this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.