开发者

Zend form element errors not display

I have a simple contact form.When I submit form error messages not display.Pls help me

class Users_Form_ContactForm extends Zend_Form{
    public function init(){
        $this->setName('contact');

        $user_name = new Zend_Form_Element_Text('name');
        $user_name->setLabel($this->getView()->translate('Ismingiz:'));
        $user_name->setRequired(true)
                ->addValidator('alnum')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setAttrib('class', 'input margin_top_10');

        $email_validate = new Zend_Validate_EmailAddress();
        $email_validate->setMessages(array(
                                          Zend_Validate_EmailAddress::INVALID => "1. Invalid type given, value should be a string",
                                          Zend_Validate_EmailAddress::INVALID_FORMAT => "2. '%value%' is no valid email address in the basic format local-part@hostname",
   开发者_运维知识库                                       Zend_Validate_EmailAddress::INVALID_HOSTNAME => "3. '%hostname%' is no valid hostname for email address '%value%'",
                                          Zend_Validate_EmailAddress::INVALID_MX_RECORD => "4. '%hostname%' does not appear to have a valid MX record for the email address '%value%'",
                                          Zend_Validate_EmailAddress::INVALID_SEGMENT => "5. '%hostname%' is not in a routable network segment. The email address '%value%' should not be resolved from public network.",
                                          Zend_Validate_EmailAddress::DOT_ATOM => "6. '%localPart%' can not be matched against dot-atom format",
                                          Zend_Validate_EmailAddress::QUOTED_STRING => "7. '%localPart%' can not be matched against quoted-string format",
                                          Zend_Validate_EmailAddress::INVALID_LOCAL_PART => "8. '%localPart%' is no valid local part for email address '%value%'",
                                          Zend_Validate_EmailAddress::LENGTH_EXCEEDED => "9. '%value%' exceeds the allowed length",
                                     ));

        $email = new Zend_Form_Element_Text("email");
        $email->setLabel($this->getView()->translate("Email manzilingiz:"))
                ->setRequired(true)
                ->addValidator($email_validate)
                ->addFilter('StringTrim')
                ->setAttrib('class', 'input margin_top_10')
                ->addFilter('StringToLower');

        $captcha = new Zend_Form_Element_Captcha('captcha', array(
                                                                 'captcha' => array(
                                                                     'captcha' => 'MyImage',
                                                                     'label' => $this->getView()->translate('Rasmdagi belgilarni kiriting'),
                                                                     'required' => true,
                                                                     'wordLen' => 5,
                                                                     'width' => 150,
                                                                     'height' => 40,
                                                                     'timeout' => 300,
                                                                     'fontSize' => 20,
                                                                     'gcFreq' => 5,
                                                                     'dotNoiseLevel' => 0,
                                                                     'lineNoiseLevel' => 0,
                                                                     'background' => 0,
                                                                     'font' => 'files/captcha/fonts/1.ttf',
                                                                     'imgDir' => 'files/captcha/image',
                                                                     'imgUrl' => '/files/captcha/image',
                                                                 )
                                                            ));
        $captcha->setIgnore(true);
        $captcha->setAttrib('class', 'input margin_top_10');

        $text = new Zend_Form_Element_Textarea("text");
        $text->setRequired(true)
                ->addFilter('StringTrim')
                ->setAttrib('class', 'input margin_top_10')
                ->addFilter('StringToLower')
                ->setLabel($this->getView()->translate("Xat matni:"))
                ->setAttrib('maxlength','200')
                ->setAttrib('style','height:100px;width:360px;');

        $button = new Zend_Form_Element_Submit('users_button');
        $button->setLabel($this->getView()->translate('Kiritish'))
                ->setAttrib('class', 'input margin_top_10');

        $this->addElements(array($user_name, $email, $text,$captcha, $button));
        $this->setMethod('post');
        $lang = $_SESSION['Poputchik']['slang'];
        $this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . '/' . $lang . '/default/category/contact');
        $language = $_SESSION['Poputchik']['language'];
        $translator = new Zend_Translate('array', APPLICATION_PATH.'/languages/errors/'.$language.'.php');
        $this->setTranslator($translator);
    }
}


in your controller you should use something like

if($this->getRequest()->isPost()){
    if($form->isValid($this->getRequest()->getPost()){
        $values=$form->getValues();
        //do the stuff with (array) $values
    }
}


A primary use case for forms is validating submitted data.

If the submitted data is not valid, it has methods for retrieving the various error codes and messages for elements and sub forms.

To validate a full form, use the isValid() method in your controller action:

if (!$form->isValid($_POST)) {
// failed validation
}

give a look here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜