Zend表单输入元素

时间:2015-03-20 18:31:50

标签: php arrays forms post zend-framework

这是我的表格类:

<?php

class Application_Form_Message extends Zend_Form
{

    public function init()
    {
        $this->setMethod('post');

        $this->addElement(
            'text',
            'email',
            array(
                'label' => 'E-mail',
                'filters'  => array('StringTrim'),
                'class'    => 'size-large-big',
                'required' => true
            )
        );

        $this->addElement(
            'text',
            'subject',
            array(
                'label' => 'Subject',
                'filters'  => array('StringTrim'),
                'class'    => 'size-large-big',
                'required' => true
            )
        );

        $this->addElement(
            'textarea',
            'content',
            array(
                'label' => 'Content',
                'filters'  => array('StringTrim'),
                'class'    => 'size-large-big',
                'required' => true
            )
        );

        $this->addElement(
            'file',
            'file',
            array(
                'label' => 'Attachments',
                'filters'  => array('StringTrim'),
                'class'    => 'size-large-big',
                'isArray' => true,
                'required' => false,
                'allowEmpty' => true, 
                'style' => 'display: inline;'
            )
        );

        $this->file->setDestination(realpath(APPLICATION_PATH . '/../public/uploads'))
            ->addValidator('Extension', false, 'png, jpg, jpeg', 'pdf', 'doc', 'docx');       


        $this->file->getValidator('Upload')->setMessages(array(
            Zend_Validate_File_Upload::NO_FILE => 'Empty filename',
        ));

        $this->addElement(
            'submit',
            'submit',
            array(
                'ignore'   => true,
                'label'    => 'Send',
                'class'    => 'btn'
            )
        );
    }


}

我的观点:

 <div class="container" > 
    <section class="main">
        <form action="<?php echo  $this->form->getAction(); ?>" method="<?php echo  $this->form->getMethod(); ?>">
            <ul class="form-inputs login">
              <h2>Message</h2>

              <p><?php echo $this->page->content; ?></p>
              <li>
                <?php echo  $this->form->email->renderLabel(); ?>
                <?php echo  $this->form->email->renderViewHelper(); ?>
              </li>
              <li>
                <?php echo  $this->form->subject->renderLabel(); ?>
                <?php echo  $this->form->subject->renderViewHelper(); ?>
              </li>
              <li>
                <?php echo  $this->form->content->renderLabel(); ?>
                <?php echo  $this->form->content->renderViewHelper(); ?>
              </li>
              <li>
                <?php echo  $this->form->file->renderLabel(); ?>
                <?php echo  $this->form->file->renderFile(); ?>
              </li>
              <li>
                <?php echo  $this->form->submit->renderViewHelper(); ?>
              </li>
            </ul>
        </form>
    </section>
  </div>

问题是表单无效,即使我填写所有输入并添加一个文件。

 print_r($form->getMessages()); //error messages
                print_r($form->getErrors()); //error codes
                print_r($form->getErrorMessages()); //any custom error messages

控制器:

public function newAction()
    {
        $page = new Application_Model_DbTable_Page();

        $form = new Application_Form_Message();

        if($this->getRequest()->isPost())
        {
            if ($form->isValid($this->getRequest()->getPost()))
            {
                $data = $form->getValues();
                die('its ok');
                $this->_sendComplaint($data['subject'], $data['content'], $data['email']);
            }
            else 
            {
                print_r($form->getMessages()); //error messages
                print_r($form->getErrors()); //error codes
                print_r($form->getErrorMessages()); //any custom error messages
            }
        }   
        else
            $form->email->setValue($this->view->userDetails->email); 


        $this->view->page = $page->getPageBySlug('message');

        $this->view->form = $form;

        $this->view->pageName = 'Message';
    }

什么都不返回,只是空数组:Array()数组([email] =&gt;数组()[subject] =&gt;数组()[内容] =&gt;数组()[文件] =&gt;数组()[submit] =&gt; Array())Array()

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我自己发现了问题,原因是表单标记中缺少enctype="multipart/form-data"属性。