riyas wrote:
>
>>>>everything works fine and mail is gone if I fill this form except that
the form is not validated. For example the mail may sent without ‘FullName’,
which is a required field
>
>>>>another problem is unable to display messages like ‘'Thank you’ .
>
>>>>this may because of $this->_helper->redirector method I used. The form
is redirected and hence lost the values. If I use $this->_helper->forwarded
or $this_forward() it also doesn’t work
>
> Any one can please suggest a method for me to dipsply validation message
> and other messages properly? Sorry for my poor English and thanks in
> advance
>
>
>
Strange validation should work ...
Use the flashMessenger helper to create your messages, something like this
...
class Form_ContactForm extends Zend_Form {
public function __construct($options = null, $moduleName = '',
$controllerName = '', $actionName = '') {
parent::__construct($options);
$this ->setName('testform')
->setAction('')
->setMethod('post')
->setAttrib('id', 'testform')
->setAttrib('accept-charset', 'UTF-8')
//->setAttrib('enctype', 'multipart/form-data');
->setAttrib('enctype',
'application/x-www-form-urlencoded');
$moduleName = $this->createElement('hidden','moduleName');
$moduleName->setValue(moduleName);
$controllerName = $this->createElement('hidden','controllerName');
$controllerName->setValue($controllerName);
$actionName = $this->createElement('hidden','actionName');
$actionName->setValue($actionName);
$FullName = $this->createElement('text','FullName');
$FullName->setLabel('Full Name')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$Email = $this->createElement('text','Email');
$Email->setLabel('Email')
->setRequired(true)
->addFilter('StringTrim')
->addValidator('EmailAddress')
->addValidator('NotEmpty');
$Message = $this->createElement('textarea','Message');
$Message->setLabel('Message')
->setAttribs( array('rows' => 3, 'cols' =>
20 ))
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = $this->createElement('submit','submit');
$submit->setLabel('Submit')
->setIgnore(true);
$this->addElements(array( $FullName,
$Email,
$Message,
$submit, ));
}
}
-----
$front = Zend_Controller_Front::getInstance();
$moduleName = $front->getRequest()->getModuleName();
$controllerName = $front->getRequest()->getControllerName();
$actionName = $front->getRequest()->getActionName();
$options = null;
$this->view->form = $form;
-----
public function indexAction() {
$options = null;
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$form = new Form_ContactForm($options, $moduleName, $controllerName,
$actionName);
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
$moduleName = $formData['moduleName'];
$controllerName
= $formData['controllerName'];
$actionName =
$formData['actionName'];
$FullName = $formData['FullName'];
$Email = $formData['Email'];
$Message = $formData['Message'];
if( strlen(trim( $FullName) ) ){
$mailBody .= "Name:\r\n\t".$FullName
."\r\n\r\n";
$mailBody .= "Email:\r\n\t".$Email
."\r\n\r\n";
$mailBody .=
"Message:\r\n\t".$Message ."\r\n\r\n";
$mail = new Zend_Mail();
$transport = new
Zend_Mail_Transport_Smtp('localhost');
Zend_Mail::setDefaultTransport($transport);
$mail->setSubject('Contact
Enquiry.');
$mail->setFrom($Email, $FullName);
$mail->addTo('chris.we...@saint-paul.lu', 'chris');
$mail->setBodyText($mailBody);
if( $mail->send() ){
$flashMessenger =
$this->_helper->getHelper('FlashMessenger');
$flashMessenger->addMessage('The Message');
$this->_helper->redirector('index', 'index', 'default');
}else{
$flashMessenger =
$this->_helper->getHelper('FlashMessenger');
$flashMessenger->addMessage('The Message');
$this->_helper->redirector('index', 'index', 'default');
}
}
} else {
$flashMessenger =
$this->_helper->getHelper('FlashMessenger');
$flashMessenger->addMessage('The Message');
$this->_helper->redirector('index', 'index', 'default');
}
}
}
--
View this message in context:
http://www.nabble.com/zend_form%3Adisplay-validation-and-error-messages-tp25340836p25363099.html
Sent from the Zend Framework mailing list archive at Nabble.com.