Skip to content

Commit

Permalink
Better validation handling in EEmbeddedArrayBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
canni committed Dec 4, 2010
1 parent 2b4fc72 commit 860f79e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions extra/EEmbeddedArraysBehavior.php
Expand Up @@ -41,28 +41,34 @@ private function parseExistingArray()
if(is_array($this->getOwner()->{$this->arrayPropertyName}))
{
$arrayOfDocs = array();
foreach($this->getOwner()->{$this->arrayPropertyName} as $key=>$doc)
foreach($this->getOwner()->{$this->arrayPropertyName} as $doc)
{
$arrayOfDocs[$key] = new $this->arrayDocClassName;
$arrayOfDocs[$key]->setAttributes($doc, false);
$obj = new $this->arrayDocClassName;
$obj->setAttributes($doc, false);
$arrayOfDocs[] = $obj;
}
$this->getOwner()->{$this->arrayPropertyName} = $arrayOfDocs;
}
}

public function afterValidate($event)
{
parent::afterValidate($event);
foreach($this->getOwner()->{$this->arrayPropertyName} as $doc)
{
if(!$doc->validate())
$this->getOwner()->addErrors($doc->getErrors());
}
}

public function beforeSave($event)
{
if(is_array($this->getOwner()->{$this->arrayPropertyName}))
{
$arrayOfDocs = array();
foreach($this->getOwner()->{$this->arrayPropertyName} as $key=>$doc)
foreach($this->getOwner()->{$this->arrayPropertyName} as $doc)
{
if($this->getOwner()->{$this->arrayPropertyName}[$key]->validate())
{
$arrayOfDocs[$key] = $this->getOwner()->{$this->arrayPropertyName}[$key]->toArray();
}
else
return false;
$arrayOfDocs[] = $doc->toArray();
}
$this->getOwner()->{$this->arrayPropertyName} = $arrayOfDocs;
return true;
Expand Down

0 comments on commit 860f79e

Please sign in to comment.