/** * @param Request $request */ public function bindRequest(Request $request) { $this->form->bind($request); foreach ($this->tabs as $tab) { $tab->bindRequest($request); } }
/** * @param EntityManager $user * * @return bool */ public function process(ProjetModel $projet) { $this->form->setData($projet); if ('POST' == $this->request->getMethod()) { $this->form->bind($this->request); if ($this->form->isValid()) { $projet->setUser($this->connectedUser); ############ Edit settings ############## $DestinationDirectory = __DIR__ . '/../../../../../web/uploads/'; //specify upload directory ends with / (slash) $Quality = 90; //jpeg quality ########################################## //Let's check allowed $ImageType, we use PHP SWITCH statement here $ext = $projet->file->getMimeType(); switch (strtolower($projet->file->getMimeType())) { case 'image/png': //Create a new image from file $CreatedImage = imagecreatefrompng($projet->file->getRealPath()); break; case 'image/gif': $CreatedImage = imagecreatefromgif($projet->file->getRealPath()); break; case 'image/jpeg': case 'image/pjpeg': $CreatedImage = imagecreatefromjpeg($projet->file->getRealPath()); break; default: die('Unsupported File!'); //output error and exit } // Crop and save image to upload directory $cropService = $this->cropper; $destImage = uniqid() . '.' . $projet->file->guessExtension(); if (!$cropService->cropImage($projet->getX(), $projet->getY(), $projet->getW(), $projet->getH(), $DestinationDirectory . $destImage, $CreatedImage, $Quality, $projet->file->getMimeType())) { die('Erreur lors du rognage de l\'image'); } $projet->setPhoto($destImage); // Create entity project $entity = new Projet(); $entity->setNomprojet($projet->getNomprojet()); $entity->setDescription($projet->getDescription()); $entity->setDuree($projet->getDuree()); $entity->setDaterealisation($projet->getDaterealisation()); $entity->setGains($projet->getGains()); $entity->setFinancement($projet->getFinancement()); $entity->setPhoto($projet->getPhoto()); $entity->setCout($projet->getCout()); $entity->setUser($projet->getUser()); $this->entityManager->persist($entity); $this->entityManager->flush(); return true; } } return false; }
/** * * @return boolean */ public function process() { $this->form->bind($this->request); if ($this->form->isValid()) { $formData = $this->form->getData(); if ($name = (string) $formData->name) { $this->qb->andWhere($this->qb->expr()->like('a.name', ':name'))->setParameter('name', '%' . $name . '%'); } return true; } return false; }
/** * checkFilters * * Build filters form * If the method POST filters are constructed and written to the session * else if in session is a form of filter-it is Otherwise, * if there is a default filters-they are * * @param array $default (stand default filter (option)) * @return */ protected function checkFilters($default = array()) { if ($this->filter_fields) { $this->filter_form = $this->createForm(new BuilderFormFilterType($this->filter_fields)); if (count($default)) { $this->filter_form->setData($default); } $method = $this->query instanceof NativeQuery ? 'buildNativeQuery' : 'buildQuery'; if ($this->get('request')->getMethod() == 'POST') { $this->get('session')->remove('_filter_' . $this->get('request')->get('_route')); $this->get('session')->remove('_pager_' . $this->get('request')->get('_route')); $this->filter_form->bind($this->get('request')); $this->get('zk2_admin_panel.query_builder')->{$method}($this->filter_form, $this->query); $filterService = $this->get('zk2_admin_panel.form_filter_session'); $filterService->serialize($this->filter_form->getData(), '_filter_' . $this->get('request')->get('_route')); } elseif ($this->get('session')->has('_filter_' . $this->get('request')->get('_route'))) { $filterService = $this->get('zk2_admin_panel.form_filter_session'); $data = $filterService->unserialize('_filter_' . $this->get('request')->get('_route'), $this->em_name); $this->filter_form->setData($data); $this->get('zk2_admin_panel.query_builder')->{$method}($this->filter_form, $this->query); } elseif (count($default)) { $this->get('zk2_admin_panel.query_builder')->{$method}($this->filter_form, $this->query); } } }
/** * @param UserInterface $user * * @return bool */ public function process(UserInterface $user) { $this->form->setData($user); if ('POST' == $this->request->getMethod()) { $this->form->bind($this->request); if ($this->form->isValid()) { $this->onSuccess($user); return true; } // Reloads the user to reset its username. This is needed when the // username or password have been changed to avoid issues with the // security layer. $this->userManager->reloadUser($user); } return false; }
/** * Processes the form with the request * * @param Form $form * @return Message|false the sent message if the form is bound and valid, false otherwise */ public function process(Form $form) { if ('POST' !== $this->request->getMethod()) { return false; } $form->bind($this->request); if ($form->isValid()) { return $this->processValidForm($form); } return false; }
/** * @param Form $form * @param Request $request * @param array $options * * @return bool */ public function handle(Form $form, Request $request, array $options = null) { if (!$request->isMethod('POST')) { return false; } $form->bind($request->request->get($form->getName())); if (!$form->isBound() || !$form->isValid()) { return false; } $this->usermanager->createUser($form->getData()->getUser()); return true; }
public function testDataIsInitializedFromBind() { $mock = $this->getMockBuilder('\\stdClass')->setMethods(array('preSetData', 'preBind'))->getMock(); $mock->expects($this->at(0))->method('preSetData'); $mock->expects($this->at(1))->method('preBind'); $config = new FormConfigBuilder('name', null, $this->dispatcher); $config->addEventListener(FormEvents::PRE_SET_DATA, array($mock, 'preSetData')); $config->addEventListener(FormEvents::PRE_BIND, array($mock, 'preBind')); $form = new Form($config); // no call to setData() or similar where the object would be // initialized otherwise $form->bind('foobar'); }
public function testFalseIsConvertedToNull() { $mock = $this->getMockBuilder('\\stdClass')->setMethods(array('preBind'))->getMock(); $mock->expects($this->once())->method('preBind')->with($this->callback(function ($event) { return null === $event->getData(); })); $config = new FormConfigBuilder('name', null, $this->dispatcher); $config->addEventListener(FormEvents::PRE_BIND, array($mock, 'preBind')); $form = new Form($config); $form->bind(false); $this->assertTrue($form->isValid()); $this->assertNull($form->getData()); }
/** * * @access public * @return bool */ public function process() { $this->getForm(); if ($this->request->getMethod() == 'POST') { $this->form->bind($this->request); // Validate if ($this->form->isValid()) { if ($this->getSubmitAction() == 'post') { $formData = $this->form->getData(); $this->onSuccess($formData); return true; } } } return false; }
protected function validateForm(Form $form, $request_data) { $form->bind($request_data); if (!$form->isValid()) { $msg = $form->getErrors(); if (sizeOf($msg)) { $msg = array_map(function ($message) { return $message->getMessage(); }, $msg); $msg = "\n- " . implode("\n- ", $msg); } else { $msg = "\n- " . $form->getErrorsAsString(); } $this->get('logger')->err('Erreur formulaire: ' . $msg); throw new InvalidArgumentException($msg); } }
/** * {@inheritDoc} */ public function process(Request $request, Form $form, UserResponseInterface $userInformation) { $user = $this->userManager->createUser(); // Try to get some properties for the initial form when coming from github if ('GET' === $request->getMethod()) { $user->setUsername($this->getUniqueUsername($userInformation->getNickname())); if ($userInformation instanceof AdvancedUserResponseInterface) { $user->setEmail($userInformation->getEmail()); } } $form->setData($user); if ('POST' === $request->getMethod()) { $form->bind($request); if ($form->isValid()) { $randomPassword = $this->tokenGenerator->generateToken(); $user->setPlainPassword($randomPassword); $user->setEnabled(true); $apiToken = substr($this->tokenGenerator->generateToken(), 0, 20); $user->setApiToken($apiToken); return true; } } return false; }
/** * Takes a form and query builder and returns filtered and paginated entities * @param QueryBuilder $filterBuilder * @param Form $form * @param string $session_key * @param int $limit * @param int $page_number * * @return mixed */ public function filterNate($filterBuilder, &$form, $session_key, $limit = 5, $page_number = 1) { $em = $this->container->get('doctrine')->getManager(); $paginator = $this->container->get('knp_paginator'); $lexik = $this->container->get('lexik_form_filter.query_builder_updater'); $session = $this->container->get('session'); $request = $this->container->get('request'); if ($request->getMethod() == "POST") { // bind values from the request $form->bind($request); // build the query from the given form object $session->set("filternator_{$session_key}", $form->getData()); } else { if ($session->has("filternator_{$session_key}")) { $data = $session->get("filternator_{$session_key}"); $this->manageObjects($data, $em); $form->setData($data); } } $lexik->addFilterConditions($form, $filterBuilder); $entities = $filterBuilder->getQuery()->getResult(); $pagination = $paginator->paginate($entities, $request->query->get('page', $page_number), $limit); return $pagination; }
public function testMultipartFormsWithParentsRequireNoFiles() { $form = new Form('author', new Author(), $this->validator); $form->add($this->createMultipartMockField('file')); $form->setParent($this->createMockField('group')); // files are expected to be converted by the parent $form->bind(array('file' => 'test.txt')); }
public function testSubformAlwaysInsertsIntoArrays() { $ref1 = new Author(); $ref2 = new Author(); $author = array('referenceCopy' => $ref1); $form = new Form('author', array('validator' => $this->createMockValidator())); $form->setData($author); $refForm = new FormTest_FormThatReturns('referenceCopy'); $refForm->setReturnValue($ref2); $form->add($refForm); $form->bind($this->createPostRequest(array('author' => array('referenceCopy' => array())))); // the new reference was inserted into the array $author = $form->getData(); $this->assertSame($ref2, $author['referenceCopy']); }
/** * Process the subscribre form * * @param Form $form * @return string HTML */ protected function processForm(Form $form) { $results = array(); $showForm = true; if ('POST' == $this->app['request']->getMethod()) { $form->bind($this->app['request']); if ($form->isValid()) { $data = $form->getData(); $results = $this->addSubscriber($data); $showForm = isset($results['error']); } else { $results['error'] = $this->config['messages']['error']; } } $formhtml = $this->app['twig']->render($this->config['template'], array("form" => $form->createView(), "message" => isset($results['message']) ? $results['message'] : '', "error" => isset($results['error']) ? $results['error'] : '', "showform" => $showForm, "button_text" => $this->config['button_text'], "secret" => isset($this->config['admin_secret']) ? $this->config['admin_secret'] : '', "secret_default" => $this->app['nls.defaults']['admin_secret'])); return $formhtml; }
/** * save form * * @param Form $form * @param array $data * * @return View */ protected function saveForm(Form $form, $data) { // clean array from non existing keys to avoid extra data foreach ($data as $key => $value) { if (!$form->has($key)) { unset($data[$key]); } } // bind data to form $form->bind($data); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $entity = $form->getData(); if (is_object($entity) && method_exists($entity, 'setUser')) { $entity->setUser($this->getCurrentUser()); } // save change to database $em->persist($entity); $em->flush(); $em->refresh($entity); // push back the new object $view = $this->createView($entity, 200); } else { $errors = array(); $text = ''; foreach ($form->getErrors() as $error) { if (!empty($text)) { $text .= "\n"; } $text .= $error->getMessage(); } if (!empty($text)) { $errors['global'] = $text; } foreach ($form as $child) { if ($child->hasErrors()) { $text = ''; foreach ($child->getErrors() as $error) { if (!empty($text)) { $text .= "\n"; } $text .= $error->getMessage(); } $errors[$child->getName()] = $text; } } // return error string from form $view = $this->createView(array('errors' => $errors), 400); } return $view; }