0

I'm having hard times on validating a file input from a zend form with ajax, basically I'm able to get the error messages from the normal text inputs but never from the file input, even if hardcode the html request and send it to the validate function, something might be wrong but zend doesn't validate the file input sent by ajax but it does though with normal isvalid(post) method. I tried either $.ajax or $post jquery functions but doesn't change anything... Here are the different parts: form:

class Form_ContactForm extends Zend_Form
{
    public function init ()
    {
        $this->setName("email");

        $this->setMethod('post');



        $mailsubject =new Zend_Form_Element_Text('mailsubject', array(

        'required'   => true,

        'filters'    => array('StringTrim')


        ));
        $mailsubject->removeDecorator('Errors');
        $this->addElement($mailsubject, 'mailsubject');

        $mailattcht = new Zend_Form_Element_File('mailattcht');
        $mailattcht->setDestination(APPLICATION_PATH.'/../public/mails');
        $mailattcht->addValidator('Count', false, 1);
        $mailattcht->addValidator('Size', false, 8000000);
        $mailattcht->addValidator('Extension', false, 
        'jpg,png,gif,ppt,pptx,doc,docx,xls,xslx,pdf');
        $mailattcht->removeDecorator('label');
        //$mailattcht->removeDecorator('Errors');

        $this->addElement($mailattcht, 'mailattcht');


        $mailbody =new Zend_Form_Element_Textarea('mailbody', array(

        'required'   => true,

        'filters'    => array('StringTrim'),

        'cols' => 40, 

        'rows' => 10


        ));
        $mailbody->removeDecorator('Errors');
        $this->addElement($mailbody, 'mailbody');


        $sendcopy = new Zend_Form_Element_Checkbox('sendcopy');
        $sendcopy->removeDecorator('label');
        $this->addElement($sendcopy, 'sendcopy');

        $this->addElement('submit', 'send', 
        array('required' => false, 'ignore' => true, 'label' => 'Send'));

       /* $this->addElement('hidden', 'return', array(
        'value' => Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(),                         
                ));*/



        $this->setAttrib('enctype', 'multipart/form-data');

    }

}

the view:

jQuery(document).ready(function() 
        {

    $('#send').click(function(event)
                    {

                        event.preventDefault();

                        doValidation(); 

                    });

        });

function doValidation()
    {
        var url = '/ceramstar/public/contact/validateform';
        var data = {};
        $("input").each(function(){
                data[$(this).attr('name')] = $(this).val();

        });
        data[$("#mailbody").attr('name')] = $("#mailbody").val();


        $.ajax({
              type: 'POST',
              url: url,
              enctype: 'multipart/form-data',
              data: "mailsubject=&MAX_FILE_SIZE=8388608&mailattcht=2012.srt&sendcopy=1&send=Send&mailbody=",
              success: function(resp){
                   alert(resp);
                },

              dataType: "json"
            });


//      $.post(url,data,function(resp)
//          {
//          
//                      $('#contact').submit();
//                      
//                      if (resp == "[]"){
//                      window.top.location.href='<?php echo $this->baseUrl().'/contact/thankyou';?>';
//                      parent.$.fancybox.close();
//                      
//                      
//                      }
//                      
//                      
//
//          },"json");




    }

the validate function:

public function validateformAction()
    {

        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $form = new Form_ContactForm();

        $form->isValidPartial($this->_getAllParams());
        //$form->isValidPartial($_FILES);

        $json = $form->processAjax($form->getValues());
        header('Content-type: application/json');
        echo Zend_Json::encode($json);
    }

thanks a lot for just even reading it...

1

1 Answer 1

1

You can not read from a file input field with jQuery/JavaScript, this is due to js security restrictions. If I remember correctly any read from a file input element will return an empty string.

The $form->isValidPartial() method will not perform the validation as request param mailattcht is empty.

Also you can not perform a file upload through AJAX if this was your ultimate intention.

Kind regards

Garry

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.