public function validate($data) { $this->form->setData($data); $isValid = $this->form->isValid(); $normalized = $this->form->getData(); unset($normalized['ViewReportControl']); $validated = new \SSRS\Form\ValidateResult(); $validated->isValid = $isValid; $validated->parameters = $normalized; return $validated; }
/** * @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; }
public function testExtractDataHydratorStrategy() { $this->populateHydratorStrategyForm(); $hydrator = new Hydrator\ObjectProperty(); $hydrator->addStrategy('entities', new TestAsset\HydratorStrategy()); $this->form->setHydrator($hydrator); $model = new TestAsset\HydratorStrategyEntityA(); $this->form->bind($model); $validSet = array( 'entities' => array( 111, 333 ), ); $this->form->setData($validSet); $this->form->isValid(); $data = $this->form->getData(Form::VALUES_AS_ARRAY); $this->assertEquals($validSet, $data); $entities = $model->getEntities(); $this->assertCount(2, $entities); $this->assertEquals(111, $entities[0]->getField1()); $this->assertEquals(333, $entities[1]->getField1()); $this->assertEquals('AAA', $entities[0]->getField2()); $this->assertEquals('CCC', $entities[1]->getField2()); }
public function updateAction() { if (!($seo = $this->loadObject($this->seoService, "Seo record doesn't exist."))) { return $this->redirect()->toRoute('seo', array('action' => 'list')); } $this->seoForm->bind($seo); if ($this->request->isPost()) { $this->seoForm->setData($this->request->getPost()); if ($this->seoForm->isValid()) { $this->seoService->update($this->seoForm->getData()); $this->flashMessenger()->addSuccessMessage(sprintf('Seo "%s" has been successfully updated.', $seo->getId())); return $this->redirect()->toRoute('seo', array('action' => 'list')); } } $view = new ViewModel(array('id' => $seo->getId(), 'form' => $this->seoForm)); $view->setTerminal(true); return $view; }
/** * createFromForm * * @param Form $form * @return CdliTwoStageSignup\Model\EmailVerification */ public function createFromForm(Form $form) { $form->bind(new Model()); if (!$form->isValid()) { return false; } $model = $form->getData(); $model->generateRequestKey(); $this->getEventManager()->trigger(__FUNCTION__, $this, array('record' => $model, 'form' => $form)); $this->getEmailVerificationMapper()->insert($model); return $model; }
public function persist(Form $form) { $data = $form->getData(); foreach ($data['products'] as $productId => $options) { foreach ($options as $optionId => $choiceId) { $row = array('product_id' => $productId, 'option_id' => $optionId, 'choice_id' => $choiceId); $this->getEntityMapper()->persist($row); } } $builder = $this->newBuilderForProduct($data['product_id'], $data['parent_product_id']); return $builder; }
/** * @author VanCK * @see \Zend\Form\Form::getData() */ public function getData($flag = FormInterface::VALUES_NORMALIZED) { $data = parent::getData($flag); // populate default values foreach ($this->getElements() as $element) { /* @var $element \Zend\Form\Element */ if ((!count($this->data) || !array_key_exists($element->getName(), $this->data)) && $element->getValue()) { $data[$element->getName()] = $element->getValue(); } } return $data; }
public function getData($flag = FormInterface::VALUES_NORMALIZED) { $data = parent::getData($flag); if (false === $this->isNew()) { foreach ($this->getOriginalFields() as $field) { $key = 'original_' . $field; if (array_key_exists($key, $data)) { unset($data[$key]); } } } return parent::getData($flag); }
public function saveAction(Request $request, Create $createService, Form $form, View $view, Redirect $redirect) { if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $createService->create($form->getData()); return $redirect->toRoute('admin-translate-words'); } } $view->setForm($form); $view->setTemplate('translate/admin/word/edit'); return $view; }
protected function getPreparedFormData(Form $form) { $formData = $form->getData(); foreach ($form->getElements() as $element) { if ($element instanceof File) { if ($formData[$element->getName()]['size'] == 0) { unset($formData[$element->getName()]); } else { $formData[$element->getName()] = $formData[$element->getName()]['tmp_name']; } } } unset($formData['save']); return $formData; }
/** * {@inheritdoc} * * @param \Dms\Document\Document $document * * @see \Dms\Storage\StorageInterface::write() */ public function write(\Dms\Document\Document $document) { $ret = null; $name = $document->getId(); $nameMod = substr($name, 4); $f = substr($name, 0, 2) . '/' . substr($name, 2, 2) . '/'; $path = $this->getBasePath() . $f; if (!is_dir($path)) { mkdir($path, 0777, true); } $p = $path . $nameMod . '.dat'; if ($document->getSupport() === Document::SUPPORT_FILE_MULTI_PART_STR) { $fileInput = new FileInput(key($document->getDatas())); $fileInput->getFilterChain()->attachByName('filerenameupload', ['target' => $p]); $inputFilter = new InputFilter(); $inputFilter->add($fileInput); $form = new Form(); $form->setInputFilter($inputFilter); $form->setData($document->getDatas()); if ($form->isValid()) { $form->getData(); } } else { $fp = fopen($p, 'w'); fwrite($fp, $document->getDatas()); $document->setWeight(strlen($document->getDatas())); fclose($fp); } $conf_storage = $this->options->getStorage(); if (isset($conf_storage['name']) && $conf_storage['name'] === 's3') { print_r($_FILES); $this->s3Client->copyObject(['Bucket' => $conf_storage['bucket'], 'Key' => $f . $nameMod . '.dat', 'CopySource' => $conf_storage['bucket'] . '/' . $f . $nameMod . '.dat', 'ContentType' => $document->getType(), 'ContentDisposition' => sprintf('filename=%s', null === $document->getName() ? substr($file, -1 * strlen($document->getFormat())) === $document->getFormat() ? $file : $file . '.' . $document->getFormat() : $document->getName()), 'MetadataDirective' => 'REPLACE']); } $document->setSupport(Document::SUPPORT_FILE_STR); $this->getEventManager()->trigger(__FUNCTION__, $this, array('path' => $path, 'short_name' => $nameMod, 'all_path' => $path . $nameMod . '.dat', 'support' => $document->getSupport(), 'name' => $name)); $serialize = serialize($document); $fp = fopen($path . $nameMod . '.inf', 'w'); $ret += fwrite($fp, $serialize); fclose($fp); $this->getEventManager()->trigger(__FUNCTION__, $this, array('path' => $path, 'short_name' => $nameMod, 'all_path' => $path . $nameMod . 'inf', 'support' => $document->getSupport(), 'name' => $name)); return $ret; }
public function formAction() { // Build a name element. $name = new Element('name'); $name->setLabel('Your name'); $name->setAttributes(array('type' => 'text')); // Build a submit button element. $send = new Element('send'); $send->setLabel('Send'); $send->setAttributes(array('type' => 'submit', 'value' => 'Send')); // Build a checkbox element. $check = new Checkbox('check'); $check->setLabel('Checkbox example'); // Build a multi-checkbox element. $multicheck = new MultiCheckbox('multicheck'); $multicheck->setLabel('Multi checkbox example'); $multicheck->setOptions(array('value_options' => array('One' => 'one', 'Two' => 'two'))); // Assemble the form. $form = new Form('contact'); $form->add($name); $form->add($check); $form->add($multicheck); $form->add($send); // Get the request if any. $request = $this->getRequest(); $data = $request->getPost(); $form->setData($data); // Validate the form if ($form->isValid()) { $validatedData = $form->getData(); $success = 'Form submit was successful'; } else { $success = 'Form submit failed'; $messages = $form->getMessages(); } // Set the method attribute for the form $form->setAttribute('method', 'post'); return new ViewModel(array('form' => $form, 'success' => $success, 'messages' => $messages, 'data' => $data)); }
/** * @param Form $form * @param array $data * @return UserInterface|null */ public function create(Form $form, array $data) { $zfcUserOptions = $this->getZfcUserOptions(); $user = $form->getData(); $argv = array(); if ($this->getOptions()->getCreateUserAutoPassword()) { $argv['password'] = $this->generatePassword(); } else { $argv['password'] = $user->getPassword(); } $bcrypt = new Bcrypt(); $bcrypt->setCost($zfcUserOptions->getPasswordCost()); $user->setPassword($bcrypt->create($argv['password'])); foreach ($this->getOptions()->getCreateFormElements() as $element) { call_user_func(array($user, $this->getAccessorName($element)), $data[$element]); } $argv += array('user' => $user, 'form' => $form, 'data' => $data); $this->getEventManager()->trigger(__FUNCTION__, $this, $argv); $this->getUserMapper()->insert($user); $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, $argv); return $user; }
public function testElementInAFieldsetForSomeModel() { $element = $this->getMoneyFieldset(); $element->init(); $fieldset = new Fieldset('hasMoneyElementFieldset'); $fieldset->add($element, ['name' => 'price']); $fieldset->setHydrator(new ClassMethods()); $fieldset->setUseAsBaseFieldset(true); $form = new Form(); $form->add($fieldset); // todo: can't load this $form->bind(new HasMoneyPropertyModel()); $data = ['hasMoneyElementFieldset' => ['price' => ['amount' => '500.25', 'currency' => 'BRL']]]; $form->setData($data); $this->assertTrue($form->isValid()); $amountValue = $form->get('hasMoneyElementFieldset')->get('price')->get('amount')->getValue(); $currencyValue = $form->get('hasMoneyElementFieldset')->get('price')->get('currency')->getValue(); $object = $form->getData(); $this->assertSame('500.25', $amountValue); $this->assertSame('BRL', $currencyValue); $this->assertInstanceOf(Money::class, $object->getPrice()); $this->assertSame(50025, $object->getPrice()->getAmount()); $this->assertSame('BRL', $object->getPrice()->getCurrency()->getName()); }
/** * Verarbeitet das Formular zum Erstellen eines Nodes. * * @param \Zend\Form\Form $form * @return \Zend\Http\PhpEnvironment\Response|boolean */ public function processNodeForm(\Zend\Form\Form $form) { $prg = $this->getServiceLocator()->get('controllerpluginmanager')->get('prg')->__invoke(); if ($prg instanceof \Zend\Http\PhpEnvironment\Response) { return $prg; } elseif (true === is_array($prg)) { $form->setData($prg); if (true == $form->isValid()) { $node = $form->getData(); $this->saveNode($node); return true; } } return false; }
public function getData($flag = FormInterface::VALUES_NORMALIZED) { $data = parent::getData($flag); $data = $this->prepareData($data); return $data; }
/** * Verifica se o formulario e valido * @return boolean */ public function subFormIsValid(\Zend\Form\Form $subForm, array $data) { $name = $subForm->getName(); if (isset($data[$name])) { $subForm->setData($data[$name]); } else { $subForm->setData(array()); } if ($subForm->isValid()) { $this->getContainer()->{$name} = $subForm->getData(FormInterface::VALUES_AS_ARRAY); return true; } return false; }
/** * Retrieve the validated data * * By default, retrieves normalized values; pass one of the * FormInterface::VALUES_* constants to shape the behavior. * * @param boolean $localizeData * @param int $flag * @throws Exception\DomainException * @return array|object */ public function getData($localizeData = true, $flag = FormInterface::VALUES_NORMALIZED) { $formData = parent::getData($flag); // process form data $processedData = []; foreach ($formData as $fieldName => $fieldValue) { // skip all ignored elements if (in_array($fieldName, $this->ignoredElements)) { continue; } // convert from localized data $processedData[$fieldName] = $localizeData ? LocaleUtility::convertFromLocalizedValue($fieldValue, $this->customElements[$fieldName]) : $fieldValue; } return $processedData; }
/** * Hydrate an array of data onto an entity using a form * * @param AbstractEntity $entity * @param array $values * @param Form $form * @return AbstractEntity */ private function hydrate(AbstractEntity $entity, array $values, Form $form) { $form->setObject($entity); if ($form->getBaseFieldset()) { $form->getBaseFieldset()->setObject($entity); } $form->setData($values); if (!$form->isValid()) { return $this->setErrorMessages($form->getMessages()); } $result = $form->getData(); if (!$result instanceof AbstractEntity) { throw new RuntimeException('Unable to retrieve entity from Form'); } return $result; }
/** getData **/ public function getData($flag = FormInterface::VALUES_NORMALIZED) { $this->getEventManager()->trigger('pre.' . __FUNCTION__, $this, (array) $this->getformOption()); $data = parent::getData($flag = FormInterface::VALUES_NORMALIZED); $this->getEventManager()->trigger('post.' . __FUNCTION__, $this, (array) $this->getformOption()); return $data; }
public function getData($flag = FormInterface::VALUES_AS_ARRAY) { return parent::getData($flag); }