views:

28

answers:

1

Hi guys, 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:

  • all fields must be filled out
  • all html gets stripped
  • email and confirm
  • email fields must match
  • email must be valid format.

Any help would be greatly appreciated!

+4  A: 

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;

from

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

Iznogood
well if I had the proper validators then it'd maybe know if it is valid, but I don't know how to use the addValidators()
Joel
@Joel I edited my post with the relevent code from the example I linked.
Iznogood
@Iznogood nice-thank you! Can you point me to a list of the validators? I've been searching high and low.
Joel
@Joel Edited it again. From there you could always search google for zend form validators and continu from there. Plenty of information out there.
Iznogood
cool! and doh-I just found them in the library itself. Thanks for your help!
Joel
@Joel No problems and thansk for accepting my answer always appreciated!
Iznogood