public function testBindingWorksAsExpected() { $form = new \Zend\Form\Form(); $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); $fieldset = new MoneyFieldset(); $fieldset->init(); $form->add($fieldset, array('name' => 'money')); $model = new TestModel(); $form->bind($model); $this->assertEquals(5432.1, $fieldset->get('amount')->getValue()); $this->assertEquals('ZAR', $fieldset->get('currency')->getValue()); $form->setData(array('money' => array('amount' => 1234.56, 'currency' => 'GBP'))); $this->assertTrue($form->isValid()); $bound = $form->getData(); $this->assertInstanceOf('NetglueMoney\\Form\\TestModel', $bound); $this->assertInstanceOf('NetglueMoney\\Money\\Money', $bound->money); $this->assertSame(123456, $bound->money->getAmount()); $this->assertSame('GBP', $bound->money->getCurrencyCode()); }
public function testExtractFromObjectDoesntTouchOriginalObject() { $form = new \Zend\Form\Form(); $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); $this->productFieldset->setUseAsBaseFieldset(true); $form->add($this->productFieldset); $originalObjectHash = spl_object_hash($this->productFieldset->get("categories")->getTargetElement()->getObject()); $product = new Product(); $product->setName("foo"); $product->setPrice(42); $cat1 = new \ZendTest\Form\TestAsset\Entity\Category(); $cat1->setName("bar"); $cat2 = new \ZendTest\Form\TestAsset\Entity\Category(); $cat2->setName("bar2"); $product->setCategories(array($cat1, $cat2)); $form->bind($product); $form->setData(array("product" => array("name" => "franz", "price" => 13, "categories" => array(array("name" => "sepp"), array("name" => "herbert"))))); $objectAfterExtractHash = spl_object_hash($this->productFieldset->get("categories")->getTargetElement()->getObject()); $this->assertSame($originalObjectHash, $objectAfterExtractHash); }
/** * Method to process a simple form * User by createStatus() and createLink() * * @param Zend\Form\Form $form * @param string $user * @param array $data * @return mixed */ protected function processSimpleForm($form, $user, array $data) { $form->setData($data); if ($form->isValid()) { $data = $form->getData(); $data['user_id'] = $user->getId(); unset($data['submit']); unset($data['csrf']); $response = ApiClient::postWallContent($user->getUsername(), $data); return $response['result']; } return $form; }
public function getjurisdictionproductformAction() { //$response = new \Zend\Http\Response(); //$response->getHeaders()->addHeaderLine('Content-Type', 'text/xml; charset=utf-8'); //$response->setContent($xml); $marketid = (int) $this->params()->fromQuery('marketid', 0); $marketproductId = (int) $this->params()->fromQuery('marketproductId', 0); $form = new \Zend\Form\Form(); $marketJurisdictions = $this->getEntityManager()->getRepository('GDI\\Entity\\RMarketJurisdiction')->findByMarket($marketid); foreach ($marketJurisdictions as $key => $marketJurisdiction) { $jurisAbbr = $marketJurisdiction->getJurisdiction()->getJurisdictionAbbr(); //$form->setHydrator(new DoctrineHydrator($this->getEntityManager(), 'GDI\Entity\TJurisdictionProduct')); $jurisdictionProduct = $this->getEntityManager()->getRepository('GDI\\Entity\\TJurisdictionProduct')->findOneBy(array('marketProduct' => $marketproductId, 'jurisdiction' => $marketJurisdiction->getJurisdiction()->getJurisdictionId())); //var_dump($jurisdictionProduct->getRSubmissionDate()); //exit; //var_dump($jurisdictionProduct); //$form->bind($jurisdictionProduct); $form->setName('jurisdictionProduct'); $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][eSubmissionDate]", 'options' => array('label' => 'Submission (Estimated)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getESubmissionDate(), 'class' => 'form-control'))); $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][eApprovalDate]", 'options' => array('label' => 'Approval (Estimated)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getEApprovalDate(), 'class' => 'form-control'))); $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][eReleaseDate]", 'options' => array('label' => 'Master Release (Estimated)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getEReleaseDate(), 'class' => 'form-control'))); $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][eLaunchDate]", 'options' => array('label' => 'Launch (Estimated)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getELaunchDate(), 'class' => 'form-control'))); $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][eRegulatorDate]", 'options' => array('label' => 'Regulator (Estimated)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getERegulatorDate(), 'class' => 'form-control'))); // result $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][rSubmissionDate]", 'options' => array('label' => 'Submission (Result)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getRSubmissionDate(), 'class' => 'form-control'))); $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][rApprovalDate]", 'options' => array('label' => 'Approval (Result)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getRApprovalDate(), 'class' => 'form-control'))); $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][rReleaseDate]", 'options' => array('label' => 'Master Release (Result)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getRReleaseDate(), 'class' => 'form-control'))); $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][rLaunchDate]", 'options' => array('label' => 'Launch (Result)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getRLaunchDate(), 'class' => 'form-control'))); $form->add(array('type' => 'Zend\\Form\\Element\\Date', 'name' => "jurs_prod[{$jurisAbbr}][rRegulatorDate]", 'options' => array('label' => 'Regulator (Result)'), 'attributes' => array('value' => is_null($jurisdictionProduct) ? "" : $jurisdictionProduct->getRRegulatorDate(), 'class' => 'form-control'))); /*if ($key>0) { var_dump($key); break; }*/ } $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { //var_dump($product); } } $view = new JsonModel(array('form' => $form, 'marketJurisdictions' => $marketJurisdictions)); //$view->setTemplate('Application/InputProductInformation/jurisdictionproductform.phtml'); // path to phtml file under view folder $view->setTemplate('GDI/Index2/jurisdictionproductform.phtml'); // path to phtml file under view folder return $view; }
public function selectProfileFormAction() { $request = $this->getRequest(); $form = new \Zend\Form\Form(); $redirect = $this->params()->fromQuery("redirect", ""); //load contact profile forms $arr_forms = $this->getContactsModel()->getContactProfileForm(); //create radio button options foreach ($arr_forms as $key => $form_name) { $arr_element_value_options[$key] = $form_name; } //end foreach //add radio group to form $form->add(array("type" => "radio", "name" => "cpp_form_id", "options" => array("label" => "Please select the form you would like to use:", "value_options" => $arr_element_value_options))); //add remember option radio button $form->add(array('type' => 'checkbox', 'name' => 'remember_form', 'options' => array('label' => 'Remember my option', 'use_hidden_element' => true, 'checked_value' => '1', 'unchecked_value' => '0'))); $form->add(array("name" => "submit", "attributes" => array("value" => "Submit"), "options" => array("ignore" => TRUE))); //check if local storage has been enabled $arr_config = $this->getServiceLocator()->get("config"); if (!isset($arr_config["logged_in_user_settings"])) { $storage_disabled = TRUE; } elseif (isset($arr_config["logged_in_user_settings"]) && $arr_config["logged_in_user_settings"]["storage_enabled"] !== TRUE) { $storage_disabled = TRUE; } //end if if (isset($storage_disabled)) { $form->remove("remember_form"); } //end if //load user session data $objUserStorage = FrontUserSession::getUserLocalStorageObject(); if (is_numeric($objUserStorage->readUserNativePreferences()->cpp_form_id)) { $form->get("cpp_form_id")->setValue($objUserStorage->readUserNativePreferences()->cpp_form_id); } //end if if ($request->isPost()) { //validate form submitted $form->setData($request->getPost()); if ($form->isValid()) { $arr_form_data = $form->getData(); $form_id = $arr_form_data["cpp_form_id"]; if (isset($arr_form_data["remember_form"]) && $arr_form_data["remember_form"] == 1) { //persist user preference if (is_object($objUserStorage)) { $objUserStorage->setUserNativePreferences('cpp_form_id', $form_id); $objUserData->cookie_data->cpp_form_id = $form_id; } //end if } //end if //check if redirect has been specified if ($redirect != "") { //redirect received return $this->redirect()->toUrl($redirect . "?fid={$form_id}"); } //end if //redirect back to the contact edit screen with form id specified $url = $this->url()->fromRoute("front-contacts", array("action" => "create-contact")); //execute redirect $response = $this->getResponse(); $response->getHeaders()->addHeaderLine('Location', $url . "?fid={$form_id}"); $response->setStatusCode(302); return $response; } //end if } //end if return array("form" => $form, "redirect" => $redirect); }
public function testDoesNotCreateNewObjects() { $form = new \Zend\Form\Form(); $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); $this->productFieldset->setUseAsBaseFieldset(true); $form->add($this->productFieldset); $product = new Product(); $product->setName("foo"); $product->setPrice(42); $cat1 = new \ZendTest\Form\TestAsset\Entity\Category(); $cat1->setName("bar"); $cat2 = new \ZendTest\Form\TestAsset\Entity\Category(); $cat2->setName("bar2"); $product->setCategories(array($cat1,$cat2)); $form->bind($product); $form->setData( array("product"=> array( "name" => "franz", "price" => 13, "categories" => array( array("name" => "sepp"), array("name" => "herbert") ) ) ) ); $form->isValid(); $categories = $product->getCategories(); $this->assertSame($categories[0], $cat1); $this->assertSame($categories[1], $cat2); }
/** * Submit a webform * @return Ambigous <\Zend\Http\Response, \Zend\Stdlib\ResponseInterface>|Ambigous <unknown, \Zend\Form\Form> */ public function bfAction() { //set container for additional params $arr_additional_params = array(); $form_id = $this->params()->fromRoute("fid"); $reg_id = $this->params()->fromRoute("reg_id", NULL); $arr_additional_params["reg_id"] = $reg_id; //check if reg id is encoded, if not, do not process if (is_numeric($reg_id)) { $this->flashMessenger()->addErrorMessage("An error occured attempting to load data"); //redirect back to form return $this->redirect()->toRoute("majestic-external-forms/bf", array("fid" => $form_id)); } //end if //load comm history id $comm_history_id = $this->params()->fromQuery("cid", ""); if ($comm_history_id != "") { $arr_additional_params["cid"] = $comm_history_id; } //end if //check form id has been set if (!is_string($form_id)) { echo "Form could not be loaded. Required information is not available."; exit; } //end if try { //load form details $arr_return = $this->getExternalFormsModel()->loadForm($form_id, $reg_id, $arr_additional_params); $arr_return["additional_data"] = $arr_additional_params; //add plain form url $arr_return["form_url"] = $this->url()->fromRoute("majestic-external-forms/bf", array("fid" => $form_id)); } catch (\Exception $e) { //@TODO do something with the error // var_dump($e->getMessage() . " : " . $e->getPrevious()); exit; die("The requested form could not be loaded. Response: " . $this->frontControllerErrorHelper()->formatErrors($e)); } //end catch if ($arr_return["objFormRawData"]->secure_form == "1") { if (!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != "on" && $_SERVER["HTTPS"] != 1 && $_SERVER["SERVER_PORT"] != "443") { header("location:https://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI]); } //end if } //end if //should the user be logged in? if ($arr_return["objFormRawData"]->user_login == 1) { $objUserSession = FrontUserSession::isLoggedIn(); if (!$objUserSession) { $this->flashMessenger()->addInfoMessage("User must be logged in in order to access form"); //redirect to login screen return $this->redirect()->toRoute("front-user-login"); } //end if } //end if //should the contact be specified if ($arr_return["objFormRawData"]->id_required == 1 && $reg_id == "") { echo "Form could not be loaded. Contact ID is not set"; exit; } //end if //should the form be redirected on loading? if ($arr_return["objFormRawData"]->redirect_on_load != "") { header("location:" . $arr_return["objFormRawData"]->redirect_on_load); exit; } //end if //extract form from result $form = $arr_return["objForm"]; //does form have password access enabled? //@TODO set proper session data if ($arr_return["objFormRawData"]->form_password != "" && $_SESSION["form_data"]["password"] != $arr_return["objFormRawData"]->form_password) { $form = new \Zend\Form\Form(); $form->add(array("type" => "password", "name" => "password", "attributes" => array("id" => "password", "required" => "required"), "options" => array("label" => "Form Password"))); $form->add(array("type" => "submit", "name" => "submit", "attributes" => array("value" => "Submit"))); $request = $this->getRequest(); if ($request->isPost()) { if ($request->getPost("password") == $arr_return["objFormRawData"]->form_password) { $_SESSION["form_data"]["password"] = $request->getPost("password"); return $this->redirect()->toRoute("majestic-external-forms/bf", array("fid" => $form_id, "reg_id" => $reg_id)); } //end if } //end if if ($_SESSION["form_data"]["password"] != $arr_return["objFormRawData"]->form_password) { $arr_return["form"] = $form; return $arr_return; } //end if } //end if //is form captcha enabled? if ($arr_return["objFormRawData"]->captcha == 1) { if (!is_dir("./public/captcha")) { mkdir("./public/captcha", 0755, TRUE); } //end if $objCaptcha = new \Zend\Captcha\Image(array('expiration' => '300', 'wordlen' => '7', 'font' => 'data/fonts/arial.ttf', 'fontSize' => '20', 'imgDir' => 'public/captcha', 'imgUrl' => '/captcha', 'lineNoiseLevel' => 1, 'dotNoiseLevel' => 1)); $form->add(array("name" => "captcha", "type" => "Zend\\Form\\Element\\Captcha", "attributes" => array("id" => "captcha", "required" => "required", "autocomplete" => "off"), "options" => array("label" => "Human verification", "captcha" => $objCaptcha))); } //end if $arr_return["form_posted"] = FALSE; $request = $this->getRequest(); if ($request->isPost()) { if ($form->has("captcha")) { if (!$objCaptcha->isValid($request->getPost("captcha"), $request->getPost())) { $form->setData($request->getPost()); $this->flashMessenger()->addErrorMessage("CAPTCHA validation failed"); $arr_return["form"] = $form; return $arr_return; } //end if } //end if //set form post flag to stop javascript loading on form error $arr_return["form_posted"] = TRUE; $form->setData($request->getPost()); if ($form->isValid($request->getPost())) { try { //submit the form $objResult = $this->getExternalFormsModel()->processFormSubmit($form_id, $form->getData(), $arr_additional_params); //unset form password if (isset($_SESSION["form_data"]["password"])) { unset($_SESSION["form_data"]["password"]); } //end if //redirect to post submit page return $this->redirect()->toRoute("majestic-external-forms/bfs", array("fid" => $form_id, "reg_id" => $objResult->data->reg_id_encoded)); } catch (\Exception $e) { //extract errors from the request return by the API $arr_response = explode("||", $e->getMessage()); $objResponse = json_decode($arr_response[1]); //check if user is logged in to display links to duplicate contacts $objUserSession = FrontUserSession::isLoggedIn(); if (is_object($objResponse) && is_object($objUserSession)) { switch ($objResponse->HTTP_RESPONSE_CODE) { case 409: //duplicates found //extract message $arr_t = explode(":", $objResponse->HTTP_RESPONSE_MESSAGE); $id_string = array_pop($arr_t); $this->flashMessenger()->addErrorMessage(trim(str_replace(array("{", "}"), "", $id_string))); //extract ids and create links to each preg_match('~{(.*?)}~', $id_string, $output); $arr_contact_ids = explode(",", $output[1]); if (is_array($arr_contact_ids) && count($arr_contact_ids) > 0) { foreach ($arr_contact_ids as $k => $id) { $this->flashMessenger()->addInfoMessage("<a href=\"" . $this->url()->fromRoute("front-contacts", array("action" => "view-contact", "id" => $id)) . "\" target=\"_blank\" title=\"View Contact\">Click to view duplicate {$id}</a>"); if ($k > 19) { break; } //end if } //end foreach } //end if break; default: //add errors to the form already where set //@TODO this needs some work, messages should be generated back into the form directly... if (is_object($objResponse) && isset($objResponse->data)) { foreach ($objResponse->data as $k => $objField) { if (is_object($objField) && isset($objField->messages) && isset($objField->attributes->name)) { if ($form->has($objField->attributes->name)) { $arr_message = (array) $objField->messages; $form->get($objField->attributes->name)->setMessages($arr_message); $form->get($objField->attributes->name)->setValue($request->getPost($objField->attributes->name)); } //end if } //end if } //end if } //end if //set form errors $form = $this->frontFormHelper()->formatFormErrors($form, $e->getMessage()); break; } //end switch } else { //@TODO this needs some work, messages should be generated back into the form directly... if (is_object($objResponse) && isset($objResponse->data)) { foreach ($objResponse->data as $k => $objField) { if (is_object($objField) && isset($objField->messages) && isset($objField->attributes->name)) { if ($form->has($objField->attributes->name)) { $arr_message = (array) $objField->messages; $form->get($objField->attributes->name)->setMessages($arr_message); $form->get($objField->attributes->name)->setValue($request->getPost($objField->attributes->name)); } //end if } //end if } //end if } //end if //set form errors $form = $this->frontFormHelper()->formatFormErrors($form, $e->getMessage()); } //end if } //end catch } //end if } //end if $arr_return["form"] = $form; $arr_return["form_id"] = $form_id; if ($reg_id != "") { $arr_return["reg_id"] = $reg_id; } //end if return $arr_return; }
<?php require_once './autoloader.php'; $form = new \Zend\Form\Form(); $select = array('name' => 'cat_id', 'type' => 'Zend\\Form\\Element\\Select', 'options' => array('label' => 'Categoria', 'value_options' => array('' => ''))); $form->add($select); $form->get('cat_id')->setValueOptions(array('foo' => 'bar')); $helper = new Zend\Form\View\Helper\FormSelect(); echo $helper($form->get('cat_id')); $formV = new \Zend\Form\Form(); $formV->add(array('name' => 'cat_id', 'type' => 'Zend\\Form\\Element\\Select', 'options' => array('label' => 'Categoria', 'value_options' => array('' => '')), 'require' => true, 'filters' => array(array('name' => 'Int')))); $formV->setData(array('cat_id' => 'bar')); $formV->prepare(); echo $formV->isValid();
<?php require_once './autoloader.php'; $translator = Zend\I18n\Translator\Translator::factory(array('locale' => 'zh', 'translation_file_patterns' => array('zf' => array('type' => 'PhpArray', 'base_dir' => EVA_LIB_PATH . '/Zend/resources/languages/', 'pattern' => '%s/Zend_Validate.php')))); \Zend\Validator\AbstractValidator::setDefaultTranslator($translator); $form = new \Zend\Form\Form(); $name = array('name' => 'username', 'options' => array('label' => 'Your name'), 'attributes' => array('type' => 'text')); $form->add($name); $filter = $form->getInputFilter(); $filter->remove('username'); $filter->add(array('name' => 'username', 'required' => true, 'validators' => array('stringLength' => array('name' => 'StringLength', 'options' => array('max' => '3'))))); $form->setInputFilter($filter); //$form->prepare(); $form->setData(array('username' => 'sadjksafjas:')); $form->prepare(); //p($form->getInputFilter()); $form->isValid(); p($form->getMessages());