/** * For JS AJAX validation - validates a property of Equipment object. * @return JSON status=ok|error,message={string} */ public function validateJSONAction() { try { $propName = $this->params()->fromPost('propName'); if (!in_array($propName, Equipment::propertyNames())) { throw new \Exception('Wrong property provided.'); } $value = $this->params()->fromPost('value'); if (!$propName || !$value) { throw new \Exception('No property and/or value given.'); } $equipment = new Equipment(); $builder = new AnnotationBuilder(); $form = $builder->createForm($equipment); $form->setData(array($propName => $value))->isValid(); $elements = $form->getElements(); $element = $elements[$propName]; if (count($element->getMessages())) { return $this->myJsonModel(array('status' => 'error', 'message' => $element->getMessages())); } else { return $this->myJsonModel(array('status' => 'ok', 'message' => '')); } } catch (\Exception $e) { // @TODO: set status to syserror so JS doesn't confuse with // validation error return $this->myJsonModel(array('status' => 'error', 'message' => $e->getMessage())); } }
public function create($data) { $cid = $this->params()->fromRoute('cid'); if (empty($data['id'])) { $data['id'] = 0; } $data['cid'] = $cid; $form = new EquipmentForm(); $equipment = new Equipment(); //$form->setInputFilter($equipment->getInputFilter()); $form->setData($data); if ($form->isValid()) { $equipment->exchangeArray($form->getData()); $id = $this->getEquipmentTable()->saveEquipment($equipment, $cid); } return $this->get($id); }