2

Edit, I fixed it by changing my JS to:

$('.zend_form input:not([type="file"]), .zend_form textarea').each(function() {
    data[$(this).attr('name')] = $(this).val();
});

Hello,

As I posted earlier, I followed a ZendCast that allowed you to use jQuery to detect and display to users problem with their form.

However, file fields always return: fileUploadErrorIniSize (File 'image_front_url' exceeds the defined ini size" even if the file is within size limits.

TPL For Forms:

<?php $this->headScript()->captureStart(); ?>

$(function() { 

    $('.zend_form input, .zend_form textarea').blur(function() {
        var formElementId = ($(this).parent().prev().find('label').attr('for'));
        doValidation(formElementId);
    });
});


function doValidation(id) {

    var url = '/<?php echo MODULE; ?>/json/validateform/form_name/<?php echo get_class($this->form); ?>';
    var data = {};

    $('.zend_form input, .zend_form textarea').each(function() {
        data[$(this).attr('name')] = $(this).val();
    });

    $.post(url, data, function(resp) {
        $('#'+id).parent().find('.errors').remove();
        $('#'+id).parent().append(getErrorHtml(resp[id], id));
    }, 'json');

};

function getErrorHtml(formErrors, id) {

    var o = '';
    if (formErrors != null) {
    var o = '<ul id="errors-'+id+'" class="errors">';

    for (errorKey in formErrors) {
        o += '<li>'+formErrors[errorKey]+'</li>';
    }
    o += '</ul>';
    }
    return o;
}

<?php $this->headScript()->captureEnd(); ?>


<?php 
if (is_object($this->form) && $this->form->getErrorMessages()) {
    echo $this->partial('partials/errors.phtml', array('errors' => $this->form->getErrorMessages(), 'translate' => $this->translate));
}
?>

<?php if (isset($this->errorMsg)) { ?>
    <p><?php echo $this->errorMsg; ?></p>
<?php } ?>

<?php echo $this->form; ?>

Which is directed to

<?php

class Administration_JsonController extends Zend_Controller_Action {


    public function validateformAction() {

        $form_name  = $this->_getParam('form_name');
        $form       = new $form_name();
        $data       = $this->_getAllParams();

        $form->isValidPartial($data);
        $json = $form->getMessages();
        $this->_helper->json($json);
    }

}

Example of returned json:

{"name":{"isEmpty":"Value is required and can't be empty"},"name_url":{"isEmpty":"Value is required and can't be empty"},"image_site_url":{"fileUploadErrorIniSize":"File 'image_site_url' exceeds the defined ini size"},"image_url":{"fileUploadErrorIniSize":"File 'image_url' exceeds the defined ini size"},"image_front_url":{"fileUploadErrorIniSize":"File 'image_front_url' exceeds the defined ini size"},"image_back_url":{"fileUploadErrorIniSize":"File 'image_back_url' exceeds the defined ini size"}}

I noticed a few people had this issue and they said that isValidPartial fixes it, so I changed

$form->isValid($data);

to

$form->isValidPartial($data);

but it didn't fix this issue.

Any ideas?

azz0r
  • 3,283
  • 7
  • 42
  • 85

2 Answers2

4

The problem is that you can't treat file fields in the same manner as regular text fields.

When you call $('input').val(), you get an actual text value for the text field, but for the file field you get the file name - and not the file contents.

Then your script tries to validate your file name as a file and, apparently, fails. In order for file validator to succeed you need to pass actual file contents to the script.

So, basically, you need to upload a file asynchronously to the server to perform all the necessary validations.

Unfortunately, uploading files via Ajax is not quite a trivial thing to do. Your basic options are uploading files via iFrame or swfObject. You can take a look at the broad selection of plugins suitable for this purpose here.

My personal choice for asynchronous file upload would be file-uploader jQuery plugin.

Community
  • 1
  • 1
Vika
  • 3,275
  • 18
  • 16
  • Thank you, this makes sense. What confuses me is the comment made here: http://www.zendcasts.com/ajaxify-your-zend_form-validation-with-jquery/2010/04/ comment by dantan. He claims to have solved it by using isValidPartial. What I would think is perhaps maybe to pass that its a file field somehow and then it can be ignored by the ajax request? – azz0r Apr 14 '11 at 11:51
  • @azz0r Yes, I think the easiest way to go is to just skip Ajax validations for file fields and to validate files only when the form gets submitted. That's where you might need to use `isValidPartial`- it skips check for required flags; this way, if your file element is required and you don't pass its value to the Ajax request, you won't get `isEmpty` error message. – Vika Apr 14 '11 at 14:54
2

Are you putting an Encrypt type on your form?

I have found two different forum posts about this, including a stack post:

odd Zend_Form_Element_File behavior

You need to add enctype="multipart/form-data" to your form tag.

Basically what is happening is the form is using its default "application/x-www-form-urlencoded" method of encryption before it is sent to the server. File uploading is not supported with this method.

Community
  • 1
  • 1
Bodman
  • 7,938
  • 3
  • 29
  • 34
  • Thats not the issue as when you submit the form usually the forms submitted and this is set for the form: $this->setAttrib('enctype', 'multipart/form-data');//image upload – azz0r Apr 12 '11 at 21:24