This code works, but becomes cluttered and complicated when multiple children need to be validated. The parent’s id is required for validation, but the parent should not be saved to the database when one or several children don’t validate.
Unfortunately Yii does not provide anything as a solution as i know. But in the extensions here extension to do that(i do not know the name, you have to have a look), in order to validation u have to beforeValidate() method of parent object u just override this method inside parent object class then before validating parent obj the code inside the method will be invoked.
Thanks guys for the suggestions. I was using rules to validate the foreign key and other attributes which are not entered by the user. I removed all the rules which are not used for user input. Now I can just use:
Implementing the same functionality with a rule on parentId makes it a lot more complicated. Using validation rules to maintain database integrity is also bad practice, because an attempt to break the integrity should result in an Exception, not in a validation error.