Reputation: 2721
I'm trying to figure out how to get this zend form to validate. I don't understand:
Are the addValidator() arguments specific validators? Is there a list somewhere of those validators?
I've got this in the forms/contact.php:
class Application_Form_Contact extends Zend_Form {
public function init()
{
$this->setAction('index/process');
$this->setMethod('post');
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Name:');
// $name->addValidator('alnum');
$name->setRequired(true);
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email:')->setRequired(true);
$confirm = new Zend_Form_Element_Text('confirm');
$confirm->setLabel('Confirm Email:')->setRequired(true);
$phone = new Zend_Form_Element_Text('phone');
$phone->setLabel('Phone:')->setRequired(true);
$subject = new Zend_Form_Element_Select('subject');
$subject->setLabel('Subject:')->setRequired(true);
$subject->setMultiOptions(array('Performance'=>'Performance',
'Workshop'=>'Workshop',
'Other'=>'Other'
));
$message = new Zend_Form_Element_Textarea('message');
$message->setLabel('Message:')->setRequired(true);
$message->setAttrib('rows','6');
$message->setAttrib('cols','30');
$submit = new Zend_Form_Element_Submit('Submit');
$this->addElements(array( $name,
$email,
$confirm,
$phone,
$subject,
$message,
$submit
));
$this->setElementDecorators(array
('ViewHelper',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label' , array('tag' => 'td')),
array(array('row' => 'HtmlTag') , array('tag' => 'tr'))
));
$submit->setDecorators(array('ViewHelper',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array(array('emptyrow' => 'HtmlTag'), array('tag' => 'td', 'placement' => 'PREPEND')),
array(array('row' => 'HtmlTag') , array('tag' => 'tr'))
));
$this->setDecorators(array(
'FormElements',
array('HtmlTag' , array('tag' => 'table' , 'class' => 'formTable')),
'Form'
)
);
}
}
my controller is:
public function indexAction()
{
$this->view->form = new Application_Form_Contact();
}
public function processAction()
{
// $this->view->form = new Application_Form_Contact();
//
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
// echo 'success';
$this->view->data = $formdata;
} else {
// $form->populate($formData);
}
}
I'm a newbie, so I'm probably making some obvious errors that I don't see. I'm trying to do basic validation:
Any help would be greatly appreciated!
Upvotes: 1
Views: 475
Reputation: 12843
Have you tried isValid() ?:
$form = new forms_ContactForm();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
echo 'success';
exit;
} else {
$form->populate($formData);
}
}
$this->view->form = $form;
About the validators:
$firstName = new Zend_Form_Element_Text('firstName');
$firstName->setLabel('First name')
->setRequired(true)
->addValidator('NotEmpty');
$lastName = new Zend_Form_Element_Text('lastName');
$lastName->setLabel('Last name')
->setRequired(true)
->addValidator('NotEmpty');
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email address')
->addFilter('StringToLower')
->setRequired(true)
->addValidator('NotEmpty', true)
->addValidator('EmailAddress');
Heres the link to the Zend documentation about Zend fiorms and validators. Creating Form Elements Using Zend_Form_Element
Upvotes: 5