Validation in Array of Embedded Documents

51 views
Skip to first unread message

Mayur Ahir

unread,
Nov 28, 2011, 9:28:50 AM11/28/11
to yiimong...@googlegroups.com
Hi Guys,

I have a model called UserEducation which is an array of embedded documents extended from model User.

There are certain rules defined in UserEducation viz.



class UserEducation extends EMongoEmbeddedDocument {

    public $institution_name;
    public $level_of_study;
    .
    .

    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('institution_name, level_of_study, field_of_study', 'required'),
            array('institution_name', 'length', 'max' => 200),
            .
            .
        );
}

The problem is this rules doesn't validates at all.

My code for saving data using User Object is,

.
.
$user->education[$i] = new UserEducation;
$user->education[$i]->institution_name = $education['institution_name'];
.
.
if($user->validate()) { //This thing is all bypassed and data is always saved without any validation and I can't do $user->education->validate() since that is undefined method.
$user->save();
}
.
.

In my User Model I have
.
.
public $education;
.
.

public function behaviors() {
        return array(
            .
            .
            array(
                'class' => 'ext.YiiMongoDbSuite.extra.EEmbeddedArraysBehavior',
                'arrayPropertyName' => 'education', // name of property, that will be used as an array
                'arrayDocClassName' => 'UserEducation'    // class name of embedded documents in array
            ),
            .
            .
        );
    }
 

Attila Nagy

unread,
Nov 29, 2011, 10:03:04 AM11/29/11
to yiimong...@googlegroups.com
This a bug in the EEmbeddedMongoDocument class. It seems like I have
the solution, but it reveals some bugs in the ArrayBehavior too, so I
need some more testing.

--
Nagy Attila Gabor

Mayur Ahir

unread,
Nov 30, 2011, 4:20:44 AM11/30/11
to yiimong...@googlegroups.com
How much time it will take?
Reply all
Reply to author
Forward
0 new messages