不显示Zend表单元素错误

时间:2011-06-07 10:34:26

标签: php zend-framework zend-form zend-form-element

我有一个简单的联系表单。当我提交表单错误消息时不显示。请帮助我

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);
    }
}

2 个答案:

答案 0 :(得分:4)

在你的控制器中你应该使用像

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

答案 1 :(得分:0)

表单的主要用例是验证提交的数据。

如果提交的数据无效,则它具有检索元素和子表单的各种错误代码和消息的方法。

要验证完整表单,请在控制器操作中使用isValid()方法:

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

看看here