I was able to get your _createElement function to create elements that did
validate as expected. I have something similar but it's not using the
zend_form class, so I have to manaually test each field for validation.
Though, after making this test code - I am likely to redo my original form
now...
Code I used to test your function is listed below.
Hope it helps.
Terre
//
// default action
//
public function indexAction()
{
$this->form = new Zend_Form;
if ($this->getRequest()->isPost()) {
if($this->validateForm($this->getForm(), $_POST)) {
// success!
echo '<b>post is valid</b>';
} else {
// failure!
echo '<b>post is invalid</b>';
// dump the errors to the user
Zend_Debug::dump($this->FormErrors);
}
}
echo $this->getForm($_POST)->render();
}
//
// make the form
//
function getForm($data = array()) {
$this->form->setAction('/manage/')
->setMethod('post');
$this->form->setAttrib('id', 'login');
// quick and dirty return posted values...
if (isset($data['email'])) {
$email = $this->_createElement('email', 'Email Address',
'email', $data['email']);
} else {
$email = $this->_createElement('email', 'Email Address',
'email');
}
$elm2 = $this->_createElement('something', 'something', 'text');
$this->form->addElement($email)
->addElement($elm1)
->addElement('submit', 'login', array('label' =>
'Login'));
return $this->form;
}
//
// found one someones blog for storing the error messages , slightly
modified
//
protected function validateForm(Zend_Form $form, array $data) {
if(!$form->isValid($data)) {
foreach($form->getMessages() as $field => $message)
{
foreach($message as $error) {
$this->FormErrors[] = array($field
=> $error);
}
}
return false;
}
return true;
}
//
// added in a new case for your _createElement
//
case 'email':
$element = new Zend_Form_Element_Text($name);
$element->addValidator('EmailAddress');
$element->setOptions(array('maxlength' => 125));
break;
// sample page return
post is invalid
array(2) {
[0] => array(1) {
["email"] => string(72) "'d' is not a valid email address in the basic
format [EMAIL PROTECTED]"
}
[1] => array(1) {
["something"] => string(49) "Value is empty, but a non-empty value is
required"
}
}
-----Original Message-----
From: nwhiting [mailto:[EMAIL PROTECTED]
Sent: Monday, November 03, 2008 10:38 AM
To: [email protected]
Subject: Re: [fw-general] Zend Form Validation
nwhiting wrote:
>
>
> Matthew Weier O'Phinney-3 wrote:
>>
>> -- nwhiting <[EMAIL PROTECTED]> wrote (on Friday, 31 October
>> 2008, 07:34 AM -0700):
>>> I am having a small problem with validating input....
>>> I have everything setup correctly....but it seems that every time I
>>> submit the form it will always validate as true no matter what
>>> content is submitted in the form.
>>
>> Nobody can help you unless you provide your form details...
>>
>> --
>> Matthew Weier O'Phinney
>> Software Architect | [EMAIL PROTECTED]
>> Zend Framework | http://framework.zend.com/
>>
>>
> heres the code i am using....
>
> protected function _createElement($name, $label, $type, $value = null,
> $desc = null)
> {
> $element = null;
> switch($type)
> {
> case 'text':
> default:
> $element = new Zend_Form_Element_Text($name);
> $element->setOptions(array('maxlength' => 125));
> break;
> case 'textarea':
> $element = new Zend_Form_Element_Textarea($name);
> $element->setOptions(array('rows' => 10, 'cols' => 35));
> break;
> case 'password':
> $element = new Zend_Form_Element_Password($name);
> break;
> case 'file':
> $element = new Zend_Form_Element_File($name);
> break;
> case 'date':
> $element = new Zend_Form_Element_Text($name);
> $element->setOptions(array('dojoType' =>
> "dijit.form.DateTextBox"));
> break;
> case 'countryList':
> $locale = new Zend_Locale('en_US');
> $countryList = ($locale->getCountryTranslationList());
> asort($countryList, SORT_LOCALE_STRING);
> $element = new Zend_Form_Element_Select($name);
> $element->addMultiOptions($countryList);
> break;
> case 'stateList':
> $locale = new Zend_Locale('en_US');
> $stateList =
> array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",
> 'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delawar
> e",'DC'=>"District
> Of
> Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho"
> ,'IL'=>"Illinois",'IN'=>"Indiana",'IA'=>"Iowa",'KS'=>"Kansas",'KY'=>"K
> entucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland",'MA'=>"Massa
> chusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=
> >"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New
> Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New
> York",'NC'=>"North Carolina",'ND'=>"North
> Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma",'OR'=>"Oregon",'PA'=>"Pennsylvan
> ia",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South
>
Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"
Virginia",'WA'=>"Washington",'WV'=>"West
> Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming"); $state = new
> Zend_Form_Element_Select('state');
> $element = new Zend_Form_Element_Select($name);
> $element->addMultiOptions($stateList);
> break;
>
>
> }
>
> $element->setLabel(ucwords($label));
>
> if ($value != null)
> {
> $element->setValue($value);
> }
>
> if ($desc != null)
> {
> $element->setDescription($desc);
> }
>
> $element->addValidator('alnum')
> ->addValidator('regex', false, array('/^[a-z]/'))
> ->setRequired(true);
>
> return $element;
> }
>
> and rending a element is simply
>
> $email = $this->_createElement('email', 'Email Address', 'text',
> $accountInfo['email']);
>
> and when i enter anything that is not validating aganist that it still
> validates as true
>
> and the code i am using to process the form is
>
> if ( !$this->isMaster && $userId != $this->user->getUsername() )
> {
> throwException(PERM_MSG);
> }
> // Render out the form and check the validation
> elseif(!$this->_getParam('submit') ||
$this->getRequest()->isPost()
> &&
> !$this->form->isValid($_POST))
> {
> if ( ( null != $this->userGroupId &&
$this->userGroupId == PRODUCER
> )
> || $this->user->getLevel() == PRODUCER )
> {
> if ($this->getRequest()->isPost())
> {
> $accountInfo = $_POST;
> }
> else
> {
> $sql = '"';
> $result = $this->db->fetchAll($sql);
> $accountInfo = $result[0];
> /*
> * Set The Date Format for dojo Must
be in format YYYY-MM-DD
> */
> $birthday = explode('-',
$accountInfo['birthday']);
> $birthday[2] = substr($birthday2, 0,
2);
> $birthdayTimestamp = mktime(null,
null, null, $birthday[1],
> $birthday[2], $birthday[0]);
> $accountInfo['birthday'] =
date('Y-m-d', $birthdayTimestamp);
> }
>
>
> // Send the users Picture to the view
> $this->view->picture =
$accountInfo['image'];
>
> // Render the form
> $this->view->adminContent =
> $this->admin->renderForm('producerEditLeft', 'producerEditRight',
> $this->form, 'users/producer', $accountInfo, $accountInfo);
> }
> elseif ( ( null != $this->userGroupId &&
$this->userGroupId ==
> CORPORATE_SPONSOR ) || $this->user->getLevel() == CORPORATE_SPONSOR )
> {
> echo 'Master Admin Editing Sponsor';
> }
> elseif ( ( null != $this->userGroupId &&
$this->userGroupId ==
> PUBLISHER ) || $this->user->getLevel() == PUBLISHER )
> {
> echo 'Master Admin Editing Publisher';
> }
> elseif ( ( null != $this->userGroupId &&
$this->userGroupId ==
> CONTENT_ADMIN ) || $this->user->getLevel() == CONTENT_ADMIN )
> {
>
> }
> elseif ( ( null != $this->userGroupId &&
$this->userGroupId ==
> USERS )
> || $this->user->getLevel() == USERS )
> {
>
> }
> elseif ( !$this->isMaster )
> {
> throwException(PERM_MSG);
> }
> else
> {
> // Master Admin Account Information
> }
> }
> elseif($this->getRequest()->isPost())
> {
> if ( $this->userGroupId == PRODUCER ||
$this->user->getLevel() ==
> PRODUCER)
> {
> // User input will be proccessed using the
User_Auth getPost(); to
> validate and secure it
> $post = $this->user->getPost();
> if ($post['password'] != null)
> {
> $salt = rand(1000, 9999);
> $password =
sha1($post['password'].$salt);
> $sql = 'UPDATE '.USER.' SET password
= "'.$password.'", salt =
> "'.$salt.'" WHERE userId = "'.$this->userId.'"';
> $this->db->query($sql);
> }
>
> $birthday = $post['birthday'] . ' 00:00:00';
>
> $sql = '"
> WHERE userId =
"'.$this->userId.'"';
> //$this->db->query($sql);
>
> $sql = 'edited out';
> //$this->db->query($sql);
>
> $this->admin->message('Account has been
updated');
>
> }
> elseif ( $this->userGroupId == PUBLISHER ||
$this->user->getLevel()
> ==
> PUBLISHER)
> {
> $sql = 'Publisher Update';
> }
> elseif ( $this->userGroupId == CORPORATE_SPONSOR ||
> $this->user->getLevel() == CORPORATE_SPONSOR)
> {
> $sql = 'SPonsor Update';
> }
> elseif ( $this->userGroupId == CONTENT_ADMIN ||
> $this->user->getLevel() == ADMIN)
> {
> $sql = 'Content Admin Update';
> }
> elseif ( $this->userGroupId == USERS ||
$this->user->getLevel() ==
> USERS)
> {
> $sql = 'User Update';
> }
> else
> {
> $sql = 'Master Admin Update';
> }
> }
>
>
>
Also what im trying to do with this is put it in the _createElement method
so the error validation is parsed in automatically when the elements are
created
-----
Nickolas Whiting
Developer
http://xstudiosinc.com Xstudios
--
View this message in context:
http://www.nabble.com/Zend-Form-Validation-tp20267185p20304951.html
Sent from the Zend Framework mailing list archive at Nabble.com.