/** * (non-PHPdoc) * @see \Light\Controller\Crud::postValidate() */ protected function postValidate(Form $form, EntityInterface $item, array $data) { if (isset($data['description']) && $data['description']) { $test = strip_tags($data['description']); if (empty($test)) { $form->description->addError('Полето е задължително'); $form->markAsError(); } } if (isset($data['alias']) && $data['alias']) { $m = new Model\Table\Pages(); $where = array('alias = ?' => $data['alias']); if ($item['id']) { $where['id <> ?'] = $item['id']; } if ($m->fetchRow($where)) { $form->alias->addError('Псевдонимът се използва'); $form->markAsError(); } } }
/** * (non-PHPdoc) * @see \Micro\Application\Controller\Crud::postValidate() */ protected function postValidate(Form $form, EntityInterface $item, array $data) { if (isset($data['name']) && $data['name'] && isset($data['countryId']) && $data['countryId']) { $m = new \Brands\Model\Table\Brands(); $where = array('name = ?' => $data['name'], 'countryId = ?' => $data['countryId'], 'typeId = ?' => $data['typeId']); if ($item->getId()) { $where['id <> ?'] = $item->getId(); } if ($m->fetchRow($where)) { $form->countryId->addError('Тази марка и тип съществува за тази държава'); $form->markAsError(); } } if ($data['statusId'] && !$data['statusDate']) { $form->statusDate->addError('Дата на статуса е задължителна'); $form->markAsError(); } if (!$data['statusId'] && $data['statusDate']) { $form->statusId->addError('Статус на марката е задължителен'); $form->markAsError(); } }
public function wizzardAction() { $form = new Form(package_path('Brands', 'Resources/forms/admin/wizzard.php')); if ($this->request->isPost()) { $post = $this->request->getPost(); $post = Utils::arrayMapRecursive('trim', $post); if (isset($post['btnBack'])) { return new RedirectResponse(route(\null, ['action' => 'index', 'id' => \null])); } $form->isValid($post); if (isset($post['name']) && $post['name'] && isset($post['countryId']) && $post['countryId']) { $m = new \Brands\Model\Table\Brands(); foreach ($post['countryId'] as $countryId) { $where = array('name = ?' => $post['name'], 'countryId = ?' => $countryId, 'typeId = ?' => $post['typeId']); if ($m->fetchRow($where)) { $form->countryId->addError('Тази марка и тип съществува за някои от избраните държави'); $form->markAsError(); break; } } } if (!$form->hasErrors()) { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'index', 'id' => \null])); try { $post = Utils::arrayMapRecursive('trim', $post, true); $this->getModel()->multipleInsert($post); return $redirectResponse->withFlash('Информацията е записана'); } catch (\Exception $e) { return $redirectResponse->withFlash($e->getMessage(), 'danger'); } } } $this->view->assign('form', $form); }
public function addItemAction() { $menuId = $this->request->getParam('menuId'); $id = $this->request->getParam('id'); $menu = $this->getModel()->find((int) $menuId); if ($menu === null) { return new RedirectResponse(route(\null, array('action' => 'index', 'menuId' => \null))); } $model = new Model\Items(); if ($id) { $item = $model->find((int) $id); if ($item === null) { return new RedirectResponse(route(\null, array('action' => 'items', 'id' => \null))); } } else { $item = $model->createEntity(); } if ($item instanceof Model\Entity\Item) { } $form = new Form(package_path('Navigation', 'Resources/forms/admin/index-add-item.php')); $tree = new Helper\Tree($menu->getAlias()); $form->parentId->setMultiOptions($tree->flat($tree->getTree(null), '---', array((int) $id))); $form->populate($item->toArray()); if ($this->request->isPost()) { $post = $this->request->getPost(); if (isset($post['btnBack'])) { return new RedirectResponse(route(\null, array('action' => 'items', 'id' => \null))); } $form->isValid($post); if (isset($post['alias']) && $post['alias']) { $m = new Model\Table\Items(); $where = array('alias = ?' => $post['alias']); if ($item->getId()) { $where['id <> ?'] = $item->getId(); } if ($m->fetchRow($where)) { $form->alias->addError('Псевдонимът се използва'); $form->markAsError(); } } if (!$form->hasErrors()) { if (isset($post['routeData'])) { $routeData = $post['routeData']; } else { $routeData = array(); } foreach ($routeData as $k => $v) { if (empty($v)) { unset($routeData[$k]); } } $post = Utils::arrayMapRecursive('trim', $post, true); $item->setFromArray($post); $item->setMenuId($menuId); if ($item->getRoute() === \null) { if ($item->getUrl() === \null) { $item->setUrl('#'); } } else { $item->setUrl(\null); } if ($item->getRoute()) { if (($navigationHelper = $this->getNavigationHelper($item->getRoute())) !== \null) { if (method_exists($navigationHelper, 'decode')) { $navigationHelper->decode($routeData, $item); } } } $item->setRouteData(empty($routeData) ? \null : json_encode($routeData)); if ($item->getOrder() === \null) { if ($item->getParentId()) { $and = ' AND parentId = ' . (int) $item->getParentId(); } else { $and = ' AND parentId IS NULL'; } $item->setOrder($model->getTable()->getAdapter()->fetchOne('SELECT IFNULL(MAX(`order`), 0) + 1 FROM MenuItems WHERE menuId = ' . (int) $menuId . $and)); } try { $model->save($item); if (isset($post['btnApply'])) { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'add-item', 'id' => $item->getId()])); } else { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'items', 'id' => \null])); } return $redirectResponse->withFlash('Информацията е записана'); } catch (\Exception $e) { if ($item->getId()) { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'add-item', 'id' => $item->getId()])); } else { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'items', 'id' => \null])); } return $redirectResponse->withFlash(env('development') ? $e->getMessage() : 'Възникна грешка. Опитайте по-късно', 'danger'); } } } $this->view->assign('menu', $menu); $this->view->assign('item', $item); $this->view->assign('form', $form); }