Ejemplo n.º 1
0
 public function isValid($data)
 {
     $ndata = $data;
     if ($this->isArray()) {
         $key = $this->_getArrayName($this->getElementsBelongTo());
         if (isset($data[$key])) {
             $ndata = $data[$key];
         }
     }
     foreach ($this->_adapter->getRelations() as $name => $relation) {
         if ($this->_isIgnoredRelation($relation)) {
             continue;
         }
         if ($relation['type'] != CU_Form_Model::RELATION_MANY) {
             continue;
         }
         if (isset($ndata[$this->_getNewButtonName($name)]) || isset($ndata[$this->_getFormName($name)])) {
             if (isset($ndata[$this->_getFormName($name)]) && isset($ndata[$this->_getFormName($name)][$this->_getDeleteButtonName($name)])) {
                 return false;
             }
             $cls = $this->_relationForms[$relation['model']];
             if ($cls !== null) {
                 $form = new $cls();
             } else {
                 $form = new CU_Form_Model(array('model' => $relation['model']));
             }
             $form->setIsArray(true);
             $form->removeDecorator('Form');
             $form->addElement('submit', $this->_getDeleteButtonName($name), array('label' => 'Delete'));
             $this->addSubForm($form, $this->_getFormName($name));
             if (isset($ndata[$this->_getNewButtonName($name)])) {
                 return false;
             }
         }
         $record = $this->getRecord();
         foreach ($this->_adapter->getOneRecords($record, $name) as $rec) {
             $formName = $this->_getFormName($name, $rec);
             if (isset($ndata[$formName]) && isset($ndata[$formName][$this->_getDeleteButtonName($name, $rec)])) {
                 $this->removeSubForm($formName);
                 $this->_adapter->deleteRecord($rec);
                 return false;
             }
         }
     }
     return parent::isValid($data);
 }
Ejemplo n.º 2
0
 public function testOneRelationSaving()
 {
     $this->_initAdapter('Comment');
     $article = array('id' => 1, 'name' => 'Test');
     $this->_adapter->expects($this->once())->method('getOneRecords')->will($this->returnValue(array($article)));
     $form = new CU_Form_Model(array('model' => 'Comment', 'adapter' => $this->_adapter));
     $form->getElementForRelation('Article')->setValue(1);
     $form->getElementForColumn('sender')->setValue('Sender');
     $this->_adapter->expects($this->any())->method('setRecordValue');
     $form->save();
 }