Kod formularza:
Kod
<?php
class ContactForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('contact_us');
$firstName = new Zend_Form_Element_Text('imie');
$firstName->setLabel('Imię')
->setRequired(true)
->addValidators(array(
array('NotEmpty', true, array('messages' => array(
'isEmpty' => 'Proszę podać Imię.',
)))
));
$Subject = new Zend_Form_Element_Text('subject');
$Subject ->setLabel('Temat')
->setRequired(true)
->addValidators(array(
array('NotEmpty', true, array('messages' => array(
'isEmpty' => 'Proszę podać Temat.',
)))
));
$telefon = new Zend_Form_Element_Text('telefon');
$telefon->setLabel('Telefon')
->setRequired(true)
->addValidators(array(
array('NotEmpty', true, array('messages' => array(
'isEmpty' => 'Proszę podać Telefon.',
)))
));
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email')
->addFilter('StringToLower')
->setRequired(true)
->addValidators(array(
array('NotEmpty', true, array('messages' => array(
'isEmpty' => 'Proszę podać Email.',
)))
))
->addValidator('EmailAddress');
$wiadomosc = new Zend_Form_Element_Textarea('wiadomosc');
$wiadomosc->setLabel('WiadomoĹÄ')
->setRequired(true)
->addFilter('StripTags')
->setAttrib("rows", 10)
->setAttrib("cols", 30)
->addValidators(array(
array('NotEmpty', true, array('messages' => array(
'isEmpty' => 'Proszę podać Wiadomość.',
)))
));
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Wyślij');
$this->addElements(array( $firstName, $Subject, $email, $telefon, $wiadomosc,/* $captcha,*/ $submit));
$this->setMethod('post');
$this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl().'/contact');
}
}
kod kontrollera:
Kod
<?php
class ContactController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$form = new ContactForm();
if ($this->getRequest()->isPost()) { /* wywoĹuje siÄ jeĹli formularz zostaĹ wysĹany */
if ($form->isValid($this->getRequest()->getPost())) { /* waliduje formularz */
$smtpServer = 'smtp.gmail.com';
$username = '';
$password = '';
$config = array('ssl' => 'tls',
'auth' => 'login',
'username' => $username,
'password' => $password);
$transport = new Zend_Mail_Transport_Smtp($smtpServer, $config);
$wiadomosc = $form->getValue('wiadomosc');
$mail = new Zend_Mail('utf-8');
$mail->setFrom('cykcykacz@o2.pl', $form->getValue('email'));
$mail->addTo('cykcykacz@o2.pl', 'Server');
$mail->setSubject($form->getValue('subject'));
$mail->setBodyText($form->getValue('wiadomosc'));
$mail->send($transport);
}
}
$this->view->form = $form;
}
}
kod widok:
Kod
<?php echo $this->form;?>