【问题标题】:Zend Form Validator File Upload goes invalidZend 表单验证器文件上传无效
【发布时间】:2013-05-16 09:55:30
【问题描述】:

我得到了 Zend Form,它可以做到这一点:

 class Products_AddForm extends Zend_Form {
  public function init() {

    $ProductImage1 = $mainform->addElement('file', 'Productimage1',     array(
                               'validators' => array(
                                    array('Count', false, '1' ),
                                    array('Size', false, '10MB'),
                                    array('Extension', false, 'jpg,jpeg,tif,eps'),
                                ),
                                'required'   => false,
                                'label' => 'Product Image1 (jpg/tif/eps)'
            ));

然后是一个检查发布数据的控制器:

public function addAction()
{

    $form = $this->getAddForm();

    if($this->getRequest()->isPost()){

        $post =  $this->getRequest()->getPost();

        // check post data
        if($form->isValid($post) )
        { 
        }
        else {
            print_r($form->getErrors());
            print_r($form->getErrorMessages());
            print_r($form->getMessages());
        }

还有一个像这样的自定义视图控制器:

echo '<form method="post" action="'.$this->baseUrl('products/add').'" enctype="multipart/form-data">';

            $image1 = $form->getElement('Productimage1');

            $helper1 = $image1->helper;
            echo '<br/>'.$this->translate('Productimage').' (jpg/tif):<br/>'.$bild1->getView()->$helper1(
            $image1->getName(),
            $image1->getValue(),
            $image1->getAttribs(),
            $image1->options
            );

当我只是在文本字段中输入一些信息并发布它时,$form->isValid() 变为 false。 当我从表单和自定义视图中删除文件部分时,它工作得很好。

Formelement 为“必需”错误。 另外控制器的false部分->getErrors()、getMessages()和getErrorMessages()都不返回错误?!

有人知道这里发生了什么吗?

【问题讨论】:

    标签: zend-framework zend-form zend-validate


    【解决方案1】:

    试试这样: 这是你的表格:

       $image = $this->createElement('file','image');
                $image->setLabel('Product Image1 (jpg/tif/eps) ')
                      ->setRequired(false)
                      ->setDestination(??)
                      ->addValidator('Count',false,1)
                      ->addValidator('Size',false,10MB)
                      ->addValidator('Extension',false,'jpg,tif,eps');
                $this->addElement($image);
    

    这是你的控制器:

    $form = new Application_Form_YOURFORM(array('action' => '', 'method' => 'POST'));
            if($this->_request->isPost() && $form->isValid($this->_request->getPost()))
            {
                if($form->image->isUploaded())
                {
                    $form->image->receive();
                    $this->image = 'destination' . basename($form->image->getFileName());
                }
                $this->_helper->redirector('..');
            }
            else
            {
                $this->view->form = $form;
            }
    

    你的看法:

    <?php echo $this->form ?>
    

    【讨论】:

    • 控制器部分没有任何作用,因为$form->isValid()返回false。该视图是自定义视图,无法通过echo $this-&gt;form 完成。
    猜你喜欢
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多