/** * Login form */ public function loginAction() { if ($this->zfcUserAuthentication()->hasIdentity()) { return $this->redirect()->toRoute($this->options->getLoginRedirectRoute()); } $request = $this->getRequest(); $post = $request->getPost(); $form = $this->loginForm; $fm = $this->flashMessenger()->setNamespace('zfcuser-login-form')->getMessages(); if (isset($fm[0])) { $this->loginForm->setMessages(array('identity' => array($fm[0]))); } if ($this->options->getUseRedirectParameterIfPresent()) { $redirect = $request->getQuery()->get('redirect', !empty($post['redirect']) ? $post['redirect'] : false); } else { $redirect = false; } if (!$request->isPost()) { return array('loginForm' => $form, 'redirect' => $redirect, 'enableRegistration' => $this->options->getEnableRegistration()); } $form->setData($post); if (!$form->isValid()) { $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage); return $this->redirect()->toUrl($this->url()->fromRoute(static::ROUTE_LOGIN) . ($redirect ? '?redirect=' . rawurlencode($redirect) : '')); } // clear adapters $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); return $this->forward()->dispatch(static::CONTROLLER_NAME, array('action' => 'authenticate')); }
public function testCorrectInputDataMerging() { $this->disablePhpUploadCapabilities(); $form = new Form(); $form->add(['name' => 'collection', 'type' => 'collection', 'options' => ['target_element' => new TestAsset\TestFieldset('target'), 'count' => 2]]); copy(__DIR__ . '/TestAsset/nullfile', __DIR__ . '/TestAsset/nullfile_copy'); $request = $this->request; $request->setMethod('POST'); $request->setPost(new Parameters(['collection' => [0 => ['text' => 'testvalue1'], 1 => ['text' => '']]])); $request->setFiles(new Parameters(['collection' => [0 => ['file' => ['name' => 'test.jpg', 'type' => 'image/jpeg', 'size' => 20480, 'tmp_name' => __DIR__ . '/TestAsset/nullfile_copy', 'error' => UPLOAD_ERR_OK]]]])); $this->controller->dispatch($this->request, $this->response); $plugin = $this->plugin; $plugin($form, '/test/getPage', true); $this->assertFalse($form->isValid()); $data = $form->getData(); // @codingStandardsIgnoreStart $this->assertEquals(['collection' => [0 => ['text' => 'testvalue1', 'file' => ['name' => 'test.jpg', 'type' => 'image/jpeg', 'size' => 20480, 'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . 'TestAsset' . DIRECTORY_SEPARATOR . 'testfile.jpg', 'error' => 0]], 1 => ['text' => null, 'file' => null]]], $data); // @codingStandardsIgnoreEnd $this->assertFileExists($data['collection'][0]['file']['tmp_name']); unlink($data['collection'][0]['file']['tmp_name']); $messages = $form->getMessages(); $this->assertTrue(isset($messages['collection'][1]['text'][NotEmpty::IS_EMPTY])); $requiredFound = false; foreach ($messages['collection'][1]['file'] as $message) { if (strpos($message, 'Value is required') === 0) { $requiredFound = true; break; } } $this->assertTrue($requiredFound, '"Required" message was not found in validation failure messages'); }
public function testEmptyFormNameShouldNotRenderEmptyFormId() { $form = new Form(); $form->setMethod('post')->setAction('/foo/bar')->setView($this->getView()); $html = $form->render(); $this->assertNotContains('id=""', $html, $html); }
/** * @param null $name * @return \Zend\Form\ElementInterface|Form */ public function getFilter($name = null) { if (!empty($name)) { return $this->filter->get($name); } return $this->filter; }
public function addCSRFElement($name, $value) { $csrf = new \Zend\Form\Element\Hidden($name); $csrf->setValue($value); $this->form->add($csrf); return $this; }
public static function injectValidatorPluginManager(Form $form, ServiceLocatorInterface $sl) { $plugins = $sl->get('ValidatorManager'); $chain = new FilterChain(); $chain->setPluginManager($plugins); $form->getFormFactory()->getInputFilterFactory()->setDefaultFilterChain($chain); }
/** * @param \Zend\Form\Form $form * @param $data * @return Entity\Comment|null * @throws \Exception */ public function add(\Zend\Form\Form $form, $data) { $serviceLocator = $this->getServiceLocator(); $entityManager = $serviceLocator->get('Doctrine\\ORM\\EntityManager'); $entityType = $entityManager->getRepository('\\Comment\\Entity\\EntityType')->findOneByAlias($data['alias']); if (!$entityType) { throw new EntityNotFoundException(); } $form->setData($data); if ($form->isValid()) { if ($entityType->getIsEnabled()) { $data = $form->getData(); $comment = new Entity\Comment(); $comment->setEntityType($entityType); $user = $serviceLocator->get('Zend\\Authentication\\AuthenticationService')->getIdentity()->getUser(); $comment->setUser($user); $comment->setComment($data['comment']); $entityManager->getConnection()->beginTransaction(); try { $hydrator = new DoctrineHydrator($entityManager); $hydrator->hydrate($data, $comment); $entityType->getComments()->add($comment); $entityManager->persist($comment); $entityManager->persist($entityType); $entityManager->flush(); $entityManager->getConnection()->commit(); return $comment; } catch (\Exception $e) { $entityManager->getConnection()->rollback(); throw $e; } } } return null; }
/** * Create a new post * * @param array $data * @param \Zend\Form\Form $form * @return bool */ public function create($data, &$form) { $post = new PostEntity(); $em = $this->getEntityManager(); $form->bind($post); $form->setData($data); if (!$form->isValid()) { return false; } // we rename the file with a unique name $newName = FileUtilService::rename($data['post']['thumbnail'], 'images/posts', "post"); foreach (PostEntity::$thumbnailVariations as $variation) { $result = FileUtilService::resize($newName, 'images/posts', $variation["width"], $variation["height"]); if (!$result) { $this->message = $result; return false; } } $post->setThumbnail($newName); $post->setPostDate("now"); $post->setUrl($this->getPostUrl($post)); try { $em->persist($post); $em->flush(); $this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_POST_CREATED"]); return true; } catch (\Exception $e) { $this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_POST_NOT_CREATED"]); return false; } }
/** * Check array notation for validators */ public function testValidatorsGivenArrayKeysOnValidation() { $username = new Element('username'); $username->addValidator('stringLength', true, array('min' => 5, 'max' => 20, 'ignore' => 'something')); $form = new Form(array('elements' => array($username))); $this->assertTrue($form->isValid(array('username' => 'abcde'))); }
protected function getModel() { // Dependências $form = new Form(); $inputFilter = new InputFilter(); $formSearch = new Form(); $inputFilterSearch = new InputFilter(); $persistence = $this->getMock('Balance\\Model\\Persistence\\PersistenceInterface'); // Parâmetro $form->add(new Text('id')); $inputFilter->add(new Input('id')); // Parâmetro $form->add(new Text('foo')); $inputFilter->add(new Input('foo')); // Pesquisa: Palavras Chave $formSearch->add(new Text('keywords')); $inputFilterSearch->add(new Input('keywords')); // Configurações $form->setInputFilter($inputFilter); $formSearch->setInputFilter($inputFilterSearch); // Inicialização $model = new Model($persistence); // Formulários $model->setForm($form)->setFormSearch($formSearch); // Apresentação return $model; }
/** * Create a new image * * @param array $data * @param \Zend\Form\Form $form * @return bool */ public function create($data, &$form) { $entity = new ImageEntity(); $em = $this->getEntityManager(); $form->bind($entity); $form->setData($data); if (!$form->isValid()) { return false; } // we rename the file with a unique name $newName = FileUtilService::rename($data['image']['image'], 'images/gallery', "gallery"); foreach (ImageEntity::$thumbnailVariations as $variation) { $result = FileUtilService::resize($newName, 'images/gallery', $variation["width"], $variation["height"]); if (!$result) { $this->message = $result; return false; } } $entity->setImage($newName); try { $em->persist($entity); $em->flush(); $this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_IMAGE_CREATED"]); return true; } catch (\Exception $e) { $this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_IMAGE_NOT_CREATED"]); return false; } }
public function __invoke(Form $form, $action) { $form->setAttribute('method', 'post'); $form->setAttribute('action', $action); $form->prepare(); $view = $this->getView(); $html = ''; $html .= $view->form()->openTag($form); $html .= '<table class="default-table">'; $formElements = $form->getElements(); foreach ($formElements as $formElement) { if ($formElement instanceof Checkbox) { $html .= $view->formRowCheckbox($form, $formElement); } else { if ($formElement instanceof Submit) { $html .= $view->formRowSubmit($form, $formElement); } else { $html .= $view->formRowDefault($form, $formElement); } } } $html .= '</table>'; $html .= $view->form()->closeTag(); return $html; }
protected function _recursivelyPrepareForm(Form\Form $form) { $belongsTo = $form instanceof Form\Form ? $form->getElementsBelongTo() : null; $elementContent = ''; $separator = $this->getSeparator(); $translator = $form->getTranslator(); $view = $form->getView(); foreach ($form as $item) { $item->setView($view)->setTranslator($translator); if ($item instanceof Form\Element) { $item->setBelongsTo($belongsTo); } elseif ($item instanceof Form\Form) { if (!empty($belongsTo)) { if ($item->isArray()) { $name = $this->mergeBelongsTo($belongsTo, $item->getElementsBelongTo()); $item->setElementsBelongTo($name, true); } else { $item->setElementsBelongTo($belongsTo, true); } } $this->_recursivelyPrepareForm($item); } elseif ($item instanceof Form\DisplayGroup) { if (!empty($belongsTo)) { foreach ($item as $element) { $element->setBelongsTo($belongsTo); } } } } }
/** * @param ServiceLocatorInterface $serviceLocator * @return FormInterface */ public function create() { $formElementManager = $this->serviceManager->get('FormElementManager'); $form = new Form(); $form->add($formElementManager->get('Wizard\\Form\\Element\\Button\\Previous'))->add($formElementManager->get('Wizard\\Form\\Element\\Button\\Next'))->add($formElementManager->get('Wizard\\Form\\Element\\Button\\Valid'))->add($formElementManager->get('Wizard\\Form\\Element\\Button\\Cancel')); return $form; }
public function addExtSubForm(ZendForm $form, $name) { $extsForm = $this->getExtSubForm(); $form->setIsArray(true); $form->removeDecorator('FormDecorator'); $extsForm->addSubForm($form, $name); }
public function __invoke(Form $form) { $form->setAttribute('class', self::DEFAULT_FORM_CLASS); foreach ($form->getElements() as $element) { /* * controls how far the form indents into * the page using Twitter:Bootstrap CSS * */ $defLabelAttributes = array('class' => self::DEFAULT_LABEL_CLASS); $element->setLabelAttributes($defLabelAttributes); $element->setAttribute('class', self::DEFAULT_INPUT_CLASS); /* * set the id attribute of all inputs to be equal to their names * * makes life simple when trying to make the view * dynamic */ $element->setAttribute('id', $element->getName()); } /* * the submit button is a little different, it uses * a button class to proper rendering * */ $form->get('submit')->setAttribute('class', self::DEFAULT_SUBMIT_BUTTON_CLASS); return $form; }
/** * Outputs message depending on flag * * @return string */ public function __invoke(Form $form, $url, $class = 'form-horizontal') { $form->setAttribute('action', $url); $form->setAttribute('class', $class); $form->prepare(); $output = $this->getView()->form()->openTag($form); $submitElements = array(); foreach ($form as $element) { if ($element instanceof Submit) { $submitElements[] = $element; } elseif ($element instanceof Csrf || $element instanceof Hidden) { $output .= $this->getView()->formElement($element); } else { $element->setLabelAttributes(array('class' => 'control-label')); $output .= '<div class="control-group">'; $output .= $this->getView()->formLabel($element); $output .= '<div class="controls">'; $output .= $this->getView()->formElement($element); $output .= $this->getView()->formElementErrors($element); $output .= '</div>'; $output .= '</div>'; } } $output .= '<div class="form-actions">'; foreach ($submitElements as $element) { $output .= $this->getView()->formElement($element) . ' '; } $output .= '</div>'; $output .= $this->getView()->form()->closeTag(); return $output; }
public function createService(ServiceLocatorInterface $serviceLocator) { $form = new Form(); $form->add(new \MiniModule\Form\Element\Login(), array('priority' => 1)); $form->add(new Submit('submit')); return $form; }
/** * Display a Form * * @param \Zend\Form\Form $form * @return string */ public function __invoke(ZendForm $form) { $form->prepare(); $html = $this->getFormHelper()->openTag($form); $html .= $this->render($form->getIterator()); return $html . $this->getFormHelper()->closeTag(); }
/** * Get step model * * @param string $step * @return \Core\View\Model\WizardStep */ protected function getStep($step) { $store = $this->getStore(); $formSrv = $this->getServiceLocator()->get('Form'); if ($step == $this->startStep) { $form = $formSrv->get('Grid\\Paragraph\\CreateWizard\\Start'); $model = new StartStep(array('textDomain' => 'paragraph')); } else { $store['type'] = $step; $form = new Form(); $create = $formSrv->get('Grid\\Paragraph\\Meta\\Create'); $model = new WizardStep(array('textDomain' => 'paragraph'), array('finish' => true, 'next' => 'finish')); if ($create->has($step)) { foreach ($create->get($step) as $element) { $form->add($element); } } else { $edit = $formSrv->get('Grid\\Paragraph\\Meta\\Edit'); if ($edit->has($step)) { foreach ($edit->get($step) as $element) { $form->add($element); } } else { $model->setOption('skip', true); } } } return $model->setStepForm($form); }
/** * @param mixed $model */ public function setModel($model) { $modelClass = get_class($model); $this->form = $this->annotationBuilder->createForm($modelClass); $this->form->bind($model); $this->setVariables(['form' => $this->form, 'object' => $model, 'title' => $this->getFieldSetTitle($model)]); $this->buildFormViewModel($this->form, $this); }
public function __invoke($keyData, $keyEndPoint, array $data, Form $form) { $html = ''; $options = array_key_exists('options', $data) ? $data['options'] : array(); $form->get('endpoint')->setValue($keyEndPoint); $html .= $this->getView()->partial('helpers/partials/default-process-control', array('keyData' => $keyData, 'keyEndPoint' => $keyEndPoint, 'label' => $data['label'], 'processDescription' => array_key_exists('process-description', $data) ? $data['process-description'] : '', 'form' => $form, 'class' => 'DefaultProcessControl', 'options' => $options)); return $html; }
public function createService(ServiceLocatorInterface $serviceLocator) { $form = new Form('create_cv'); //$form->add($serviceLocator->get('ApplicationFieldset')); $form->add($serviceLocator->get('EducationCollection')); $form->add($serviceLocator->get('DefaultButtonsFieldset'), array('name' => 'buttons')); return $form; }
public function addDownloadButton($id, $url, Form $form) { if ($url) { $downloadUrl = '/documents/download/' . $id; $removeUrl = '/documents/delete-attachment/' . $id; $form->add(['name' => 'download', 'type' => 'Zend\\Form\\Element\\Button', 'attributes' => ['value' => $downloadUrl, 'id' => 'download-attachment', 'class' => 'btn btn-info btn-large pull-left self-submitter state hidden-file-input'], 'options' => ['label' => 'Download Attachment', 'download-icon' => 'icon-download-alt icon-white', 'remove-icon' => 'icon-remove icon-white', 'remove-url' => $removeUrl]], ['name' => 'download', 'priority' => 9]); } }
/** * @param \Zend\Form\Form $form */ public function it_should_have_a_form($form) { $entity = new \StdClass(); $form->hasValidated()->shouldBeCalled()->willreturn(false); $form->bind($entity)->shouldBeCalled()->willreturn($form); $form->bindOnValidate()->shouldBeCalled()->willreturn($form); $this->setForm($form)->getForm($entity)->shouldReturn($form); }
public function __invoke($id) { $html = ""; $auth = $this->sm->get('ZfcRbac\\Service\\AuthorizationService'); $zfcuserauth = $this->sm->get('zfcuser_auth_service'); $objectmanager = $this->sm->get('Doctrine\\ORM\\EntityManager'); $type = $objectmanager->getRepository('Application\\Entity\\OpSupType')->find($id); if ($zfcuserauth->hasIdentity()) { $criteria = array(); $criteria['organisation'] = $zfcuserauth->getIdentity()->getOrganisation()->getId(); $criteria['type'] = $id; $query = $objectmanager->createQueryBuilder(); $query->select('o')->from('Application\\Entity\\OperationalSupervisor', 'o')->where('o.type = ?1')->groupBy('o.zone')->setParameter(1, $id); if ($zfcuserauth->getIdentity()->getZone()) { $query->andWhere($query->expr()->eq('o.zone', '?2'))->setParameter(2, $zfcuserauth->getIdentity()->getZone()->getId()); } $zones = $query->getQuery()->getResult(); foreach ($zones as $result) { $criteria['zone'] = $result->getZone()->getId(); $zoneid = $result->getZone()->getId(); $opsups = $objectmanager->getRepository('Application\\Entity\\OperationalSupervisor')->findBy($criteria, array('name' => 'asc')); $currentopsup = $objectmanager->getRepository('Application\\Entity\\OperationalSupervisor')->findOneBy(array('organisation' => $zfcuserauth->getIdentity()->getOrganisation()->getId(), 'zone' => $result->getZone()->getId(), 'type' => $id, 'current' => true)); if ($auth->isGranted('events.mod-opsup')) { $form = new Form(); $selectOpSup = new Select('nameopsup'); $opsupArray = array(); $opsupArray['-1'] = "Choisir Op Sup"; foreach ($opsups as $opsup) { $opsupArray[$opsup->getId()] = $opsup->getName(); } $selectOpSup->setValueOptions($opsupArray); if ($currentopsup) { $selectOpSup->setAttribute('value', $currentopsup->getId()); } $form->add($selectOpSup); $formView = $this->view->form(); $form->setAttributes(array('class' => 'navbar-form navbar-left opsup-form type-' . $id . ' zone-' . $zoneid, 'data-typeid' => $id, 'data-zoneid' => $zoneid)); $html .= $formView->openTag($form); $html .= '<div class="form-group">'; $html .= '<label for="nameopsup">'; $html .= ' <span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span> <b>' . $type->getName() . (count($zones) > 1 ? ' (' . $result->getZone()->getShortname() . ')' : '') . ' : </b>'; $html .= '<b class="caret"></b></label>'; $html .= $this->view->formSelect($form->get('nameopsup')->setAttribute('class', 'form-control')); $html .= '</div>'; $html .= $formView->closeTag(); } else { if ($currentopsup) { $html .= '<p class="navbar-text navbar-left opsup-name type-' . $id . ' zone-' . $zoneid . '" style="margin-left: 0px"' . ' data-typeid="' . $id . '" data-zoneid="' . $zoneid . '">' . '<span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span> <b>' . $type->getName() . (count($zones) > 1 ? ' (' . $result->getZone()->getShortname() . ')' : '') . ' : </b>' . '<span class="opsupname">' . $currentopsup->getName() . '</span><b class="caret"></b></p>'; } else { $html .= '<p class="navbar-text navbar-left" style="margin-left: 0px"><em>Aucun Op Sup configuré</em></p>'; } } } } else { $html .= '<p class="navbar-text navbar-left"><em>Connexion nécessaire</em></p>'; } return $html; }
public function getForm() { $form = new Form(); $form->addElement('text', 'foo')->addElement('text', 'bar')->addElement('text', 'baz')->addElement('text', 'bat'); $subForm = new SubForm(); $subForm->addElement('text', 'foo')->addElement('text', 'bar')->addElement('text', 'baz')->addElement('text', 'bat'); $form->addDisplayGroup(array('foo', 'bar'), 'foobar')->addSubForm($subForm, 'sub')->setView(new View\PhpRenderer()); return $form; }
/** * @param \Zend\Form\Form $form */ protected function fixUserForm(Form &$form, $userId = null) { $auth = $this->getAuthenticationService(); $groupId = $form->get('groupId'); $groups = $groupId->getValueOptions(); if (empty($groups) || $auth->getIdentity()->id == $userId) { $form->remove('groupId'); } }
public function testOpenTagUsesNameAsIdIfNoIdAttributePresent() { $form = new Form(); $attributes = array('name' => 'login-form'); $form->setAttributes($attributes); $markup = $this->helper->openTag($form); $this->assertContains('name="login-form"', $markup); $this->assertContains('id="login-form"', $markup); }
/** * @group ZF-4038 */ public function testCaptchaShouldRenderFullyQualifiedElementName() { $form = new Form(); $form->addElement($this->element)->setElementsBelongTo('bar'); $html = $form->render(new View()); $this->assertContains('name="bar[foo', $html, $html); $this->assertContains('id="bar-foo-', $html, $html); $this->form = $form; }