public function manageAction() { if (false === $this->authorizationService->isGranted('user.admin')) { throw new UnauthorizedException('Insufficient Permissions'); } $userId = $this->params('id', null); if (null === $userId) { return $this->listUsers(); } $userObject = $this->userService->findById($userId); if (false === $userObject instanceof UserEntity) { return $this->listUsers(); } $request = $this->getRequest(); $viewModel = new ViewModel(); $viewModel->setTemplate('user/update'); $viewModel->setVariables(['form' => $this->form]); $this->form->bind($userObject); if ($request->isPost()) { $this->form->setData($request->getPost()); if ($this->form->isValid()) { try { $this->userService->save($this->form->getData()); $this->flashMessenger()->addSuccessMessage('User successfully updated'); return $this->redirect()->toRoute('zfcuser/manage'); } catch (\Exception $e) { var_dump($e->getMessage()); } } else { var_dump($this->form->getMessages()); } } return $viewModel; }
public function indexAction() { $request = $this->getRequest(); $formMessages = []; $isPost = false; if ($request->isPost()) { $this->form->setData($request->getPost()); $this->form->isValid(); $formMessages = $this->form->getMessages(); $isPost = true; } return new ViewModel(['form' => $this->form, 'formMessages' => $formMessages, 'isPost' => $isPost]); }
/** * @param FormInterface $form * @param string $redirect Route or URL string (default: current route) * @param bool $redirectToUrl Use $redirect as a URL string (default: false) * @return Response */ protected function handlePostRequest(FormInterface $form, $redirect, $redirectToUrl) { $container = $this->getSessionContainer(); $request = $this->getController()->getRequest(); $postFiles = $request->getFiles()->toArray(); $postOther = $request->getPost()->toArray(); $post = ArrayUtils::merge($postOther, $postFiles, true); // Fill form with the data first, collections may alter the form/filter structure $form->setData($post); // Change required flag to false for any previously uploaded files $inputFilter = $form->getInputFilter(); $previousFiles = $container->files ?: array(); $this->traverseInputs($inputFilter, $previousFiles, function ($input, $value) { if ($input instanceof FileInput) { $input->setRequired(false); } return $value; }); // Run the form validations/filters and retrieve any errors $isValid = $form->isValid(); $data = $form->getData(FormInterface::VALUES_AS_ARRAY); $errors = !$isValid ? $form->getMessages() : null; // Merge and replace previous files with new valid files $prevFileData = $this->getEmptyUploadData($inputFilter, $previousFiles); $newFileData = $this->getNonEmptyUploadData($inputFilter, $data); $postFiles = ArrayUtils::merge($prevFileData ?: array(), $newFileData ?: array(), true); $post = ArrayUtils::merge($postOther, $postFiles, true); // Save form data in session $container->setExpirationHops(1, array('post', 'errors', 'isValid')); $container->post = $post; $container->errors = $errors; $container->isValid = $isValid; $container->files = $postFiles; return $this->redirect($redirect, $redirectToUrl); }
public function indexAction() { $result = ['result' => false, 'message' => '']; $viewModel = $this->acceptableviewmodelselector($this->acceptCriteria); $request = $this->getRequest(); if ($request->isPost()) { $login = new LoginInputFilter(); $this->loginForm->setInputFilter($login->getInputFilter()); $this->loginForm->setData($request->getPost()); if ($this->loginForm->isValid()) { $result = ['result' => true, 'message' => 'Ajax request success']; } else { $result = ['result' => false, 'message' => $this->loginForm->getMessages()]; } } if (!$viewModel instanceof JsonModel && $request->isXmlHttpRequest()) { $viewModel = new JsonModel(); } $viewModel->setVariables(['form' => $this->loginForm, 'data' => $result]); return $viewModel; }
public function editPageRepository(FormInterface $form) { $page = $form->getObject(); if (!$form->isValid()) { throw new RuntimeException(print_r($form->getMessages(), true)); } $data = $form->getData(FormInterface::VALUES_AS_ARRAY); $formClone = clone $form; $formClone->bind($page); $formClone->setData($data); $formClone->isValid(); $this->assertGranted('page.update', $page); $this->getObjectManager()->persist($page); return $page; }
/** * Renders the error messages. * @param \Zend\Form\FormInterface $oForm * @return string */ public function render(\Zend\Form\FormInterface $oForm, $sMessage, $bDismissable = false) { $errorHtml = sprintf($this->messageOpenFormat, $sMessage); $sMessagesArray = array(); foreach ($oForm->getMessages() as $fieldName => $sMessages) { foreach ($sMessages as $sMessage) { if ($oForm->get($fieldName)->getAttribute('id')) { $sMessagesArray[] = sprintf('<a href="#%s">%s</a>', $oForm->get($fieldName)->getAttribute('id'), $oForm->get($fieldName)->getLabel() . ': ' . $sMessage); } else { $sMessagesArray[] = $oForm->get($fieldName)->getLabel() . ': ' . $sMessage; } } } return $this->dangerAlert($errorHtml . implode($this->messageSeparatorString, $sMessagesArray) . $this->messageCloseString, $bDismissable); }
public function createRole(FormInterface $form) { $this->assertGranted('authorization.role.create'); if (!$form->isValid()) { throw new RuntimeException(print_r($form->getMessages(), true)); } $processingForm = clone $form; $data = $processingForm->getData(FormInterface::VALUES_AS_ARRAY); $processingForm->bind(new Role()); $processingForm->setData($data); if (!$processingForm->isValid()) { throw new RuntimeException(print_r($processingForm->getMessages(), true)); } $this->objectManager->persist($processingForm->getObject()); return $processingForm->getObject(); }
/** * @param FormInterface $form * @return void * @throws RuntimeException */ public function updateParameter(FormInterface $form) { $object = $form->getObject(); $this->assertGranted('navigation.manage', $object); if (!$form->isValid()) { throw new RuntimeException(print_r($form->getMessages())); } $this->objectManager->persist($object); $this->getEventManager()->trigger('parameter.update', $this); }
/** * @param TaxonomyTermInterface $object * @param FormInterface $form * @return TaxonomyTermInterface * @throws \Taxonomy\Exception\RuntimeException */ protected function bind(TaxonomyTermInterface $object, FormInterface $form) { if (!$form->isValid()) { throw new RuntimeException(print_r([$form->getMessages(), $form->getData(FormInterface::VALUES_AS_ARRAY)], true)); } $processingForm = clone $form; $data = $form->getData(FormInterface::VALUES_AS_ARRAY); $processingForm->bind($object); $processingForm->setData($data); $processingForm->isValid(); $this->objectManager->persist($object); return $object; }
/** * @param FormInterface $form * @param Content $content * * @return object|null */ private function saveFormData(FormInterface $form, Content $content) { if (!$form->isValid()) { return $this->setLayoutMessages($form->getMessages(), 'error'); } $content->setAuthor($this->UserData()->getIdentity()); /* * We only need the name. All images ar stored in the same folder, based on the month and year */ $content->setPreview($form->getData()->getPreview()['name']); /* * Insert categories */ $contentCategories = $content->getCategory(); if (count($contentCategories) > 0) { $id = $content->getId(); $str = []; foreach ($contentCategories as $key => $cat) { $str[] = "('" . $id . "','" . $cat . "')"; } $str = implode(', ', $str); $this->contentTable->queryBuilder()->getEntityManager()->getConnection()->exec("INSERT INTO contents_categories (content_id, category_id)\n VALUES {$str}\n ON DUPLICATE KEY\n UPDATE content_id = VALUES(content_id), category_id = VALUES(category_id)"); } $this->contentTable->saveContent($content); $this->setLayoutMessages('«' . $content->getTitle() . '» ' . $this->translate('SAVE_SUCCESS'), 'success'); return $this->redirect()->toUrl('/admin/content'); }
/** * @param FormInterface $form * @param $namespace */ protected function addFormMessagesInFlashMessenger(FormInterface $form, $namespace) { $messages = $form->getMessages(); foreach ($messages as $elementMessages) { foreach ($elementMessages as $message) { $this->getFlashMessenger()->setNamespace($namespace)->addMessage($message); } } }
/** * @param CommentInterface $comment * @param FormInterface $form * @return CommentInterface * @throws Exception\RuntimeException */ protected function bind(CommentInterface $comment, FormInterface $form) { if (!$form->isValid()) { throw new Exception\RuntimeException(print_r($form->getMessages(), true)); } $processForm = clone $form; $data = $form->getData(FormInterface::VALUES_AS_ARRAY); $processForm->bind($comment); $processForm->setData($data); if (!$processForm->isValid()) { throw new Exception\RuntimeException($processForm->getMessages()); } return $comment; }