|
If anyone could point me in a good direction that would be great
Im trying to get this form to validate here is my ini - ; Name Field advertiser.campaignCreate.elements.name.type = "text" advertiser.campaignCreate.elements.name.options.label = "Name:" advertiser.campaignCreate.elements.name.options.validators.notempty.validator = "NotEmpty" advertiser.campaignCreate.elements.name.options.validators.notempty.options.messages.isEmpty = "A Campaign Name required." advertiser.campaignCreate.elements.name.options.validators.notempty.breakChainOnFailure = true advertiser.campaignCreate.elements.name.options.validators.alpha.validator = "Alpha" advertiser.campaignCreate.elements.name.options.validators.alpha.options.messages.notAlpha = "Invalid Campaign Name. Can only contain letter." advertiser.campaignCreate.elements.name.options.validators.strlen.validator = "StringLength" advertiser.campaignCreate.elements.name.options.validators.strlen.options.min = "3" advertiser.campaignCreate.elements.name.options.validators.strlen.options.messages.stringLengthTooShort = "Campaign Name must be between 3 and 8 characters." advertiser.campaignCreate.elements.name.options.validators.strlen.options.max = "8" advertiser.campaignCreate.elements.name.options.validators.strlen.options.messages.stringLengthTooLong = "Campaign Name must be between 3 and 8 characters." advertiser.campaignCreate.elements.name.options.filters.ucase.filter = "StringToUpper" advertiser.campaignCreate.elements.name.options.required = true advertiser.campaignCreate.elements.name.options.attribs.tabIndex = "60" advertiser.campaignCreate.elements.name.options.attribs.alt = "Create campaign campaign name field" advertiser.campaignCreate.elements.name.options.decorators.type = "ViewHelper" This is my view - <div class="form_data_wrap"> <div class="form_data_label">Name:</div> <div class="form_data_value"> <?php echo $this->campaignCreateForm->name; ?> </div> </div> this is my Controller code - $config = new Zend_Config_Ini(APPLICATION_PATH . '/config/campaignCreateForm.ini'); $this->_campaignCreateForm = new Zend_Form($config->advertiser->campaignCreate); But im at a loss as to how the validations work? thanks |
|
Administrator
|
-- Bob O <[hidden email]> wrote
(on Saturday, 14 February 2009, 05:54 PM -0800): > > If anyone could point me in a good direction that would be great > > Im trying to get this form to validate <snip - INI and view code> > this is my Controller code - > > $config = new Zend_Config_Ini(APPLICATION_PATH . > '/config/campaignCreateForm.ini'); > $this->_campaignCreateForm = new > Zend_Form($config->advertiser->campaignCreate); > > But im at a loss as to how the validations work? In your controller, pass data from the request to the form's isValid() method: if ($this->_campaignCreateForm->isValid($this->getRequest()->getPost())) { // Validates! } -- Matthew Weier O'Phinney Software Architect | [hidden email] Zend Framework | http://framework.zend.com/ |
|
Fantastic..That worked..thank you Matthew.
my final struggle is this foreach ($this->_campaignCreateForm->getMessages() as $message) { $this->flash(("Invalid Data: $message\n"), 'error', '/advertiser/create-campaign'); } returns the flash message, but the $message is simply an Array, and not the message set in the ini. So i think im confused there as well these are the messages i have set in the ini based on the particular validation. advertiser.campaignCreate.elements.name.options.validators.notempty.options.messages.isEmpty = "A Campaign Name required." advertiser.campaignCreate.elements.name.options.validators.alpha.options.messages.notAlpha = "Invalid Campaign Name. Can only contain letters."
|
|
Administrator
|
-- Bob O <[hidden email]> wrote
(on Monday, 16 February 2009, 12:49 PM -0800): > > Fantastic..That worked..thank you Matthew. > > my final struggle is this > > foreach ($this->_campaignCreateForm->getMessages() as $message) > { > $this->flash(("Invalid Data: $message\n"), 'error', > '/advertiser/create-campaign'); > } > > returns the flash message, but the $message is simply an Array, and not the > message set in the ini. So i think im confused there as well Each validator can return an array of error messages, and each element returns an array of elements that have error messages... So what you need to do is traverse the entire thing. There is a special FormErrors decorator used for this. You can grab _just_ that content if you want: $form->addDecorator('FormErrors'); $messages = $form->renderFormErrors(); > these are the messages i have set in the ini based on the particular > validation. > > advertiser.campaignCreate.elements.name.options.validators.notempty.options.messages.isEmpty > = "A Campaign Name required." > advertiser.campaignCreate.elements.name.options.validators.alpha.options.messages.notAlpha > = "Invalid Campaign Name. Can only contain letters." > > > > > Matthew Weier O'Phinney-3 wrote: > > > > -- Bob O <[hidden email]> wrote > > (on Saturday, 14 February 2009, 05:54 PM -0800): > >> > >> If anyone could point me in a good direction that would be great > >> > >> Im trying to get this form to validate > > > > <snip - INI and view code> > > > >> this is my Controller code - > >> > >> $config = new Zend_Config_Ini(APPLICATION_PATH . > >> '/config/campaignCreateForm.ini'); > >> $this->_campaignCreateForm = new > >> Zend_Form($config->advertiser->campaignCreate); > >> > >> But im at a loss as to how the validations work? > > > > In your controller, pass data from the request to the form's isValid() > > method: > > > > if > > ($this->_campaignCreateForm->isValid($this->getRequest()->getPost())) { > > // Validates! > > } > > > > -- > > Matthew Weier O'Phinney > > Software Architect | [hidden email] > > Zend Framework | http://framework.zend.com/ > > > > > > > ----- > Bob Hanson > Web Developer > SLC, UT > -- > View this message in context: http://www.nabble.com/Zend_Form%2C-Validators-%2C-Filters%2C-and-ini-files-tp22019106p22045553.html > Sent from the Zend Framework mailing list archive at Nabble.com. > -- Matthew Weier O'Phinney Software Architect | [hidden email] Zend Framework | http://framework.zend.com/ |
|
This post was updated on .
In reply to this post by Bob O
Thanks for the help Matthew, its really helping me get my head around it.
So i iterate over the array and Im getting the messages as needed. my final issue I have a select item that i create in Controller and populate it dynamically. i then add it to my _campaignCreateForm. but when i run the validation, i get back that its not part of the Haystack? Ive found the InArray in the Zend Libs where this is set, but im not sure why its not being added. if I setMessage() and then addElement to the _campaignCreateForm. i would think that it would become part of the isValid() method when invoked..? CODE: //******CAMPAIGN CREATE FORM******* $config = new Zend_Config_Ini(APPLICATION_PATH . '/config/campaignCreateForm.ini'); $this->_campaignCreateForm = new Zend_Form($config->advertiser->campaignCreate); $keywordAssignSelect = new Zend_Form_Element_Select('keywordAssignSelect', array('multiOptions' => array(), 'label'=>'Assign a Keyword')); $keywordAssignSelect->removeDecorator('HtmlTag'); $keywordAssignSelect->removeDecorator('Label'); $keywordAssignSelect->addValidators( array( array( 'validator' => 'NotEmpty', 'breakChainOnFailure' => true), array('validator' => 'Alnum'), )); $keywordAssignSelect->getValidator('Alnum')->setMessage('Keywords can only contain Numbers or Letters.'); $keywordAssignSelect->getValidator('NotEmpty')->setMessage('You must have a keyword to establish a campaign.'); $keywordAssignSelect->setRequired(true); $this->_campaignCreateForm->addElement($keywordAssignSelect); and this was the Action method code where is do the iteration if ($this->getRequest()->isPost()) { $values = $this->getRequest()->getPost(); if ($this->_fonrootsAdvertiser) { if ($this->_campaignCreateForm->isValid($this->getRequest()->getPost())) { $campaign = $this->_fonrootsAdvertiser->createCampaign($values); $this->flash(("Your Campaign has been created"), '', '/advertiser/campaigns'); } else { foreach ($this->_campaignCreateForm->getMessages() as $messages) { foreach ($messages as $message) { $this->flash(("Invalid Data: $message\n"), 'error', '/advertiser/create-campaign'); } } } }
|
| Powered by Nabble | See how NAML generates this page |
