1

Hi I have a plugin which is basically a form. This form has upload file field which I added in the backend side in Assets fields so I have something like this:

                <fieldset>
                    <div class="file-upload btn btn-primary">
                        <span>Upload File</span>
                        <input type="file" name="fields[myAssetNameHandle]">
                    </div>              
                </fieldset>

I send the form to the server with ajax like this:

    var formData = new FormData($form[0]);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '{{ actionUrl("myplugin/sendApplication") }}',
        data: formData,
        processData: false,
        contentType: false,
    ......

On the myPluginController I have validation

    $model = new myModel();
    $model->test_var = craft()->request->getPost('test_var');

    if ( $model->validate() ) {

    }else{

    }

Evrithing works fine except that I did some restriction to upload files on that asset field for example only documents so when I upload an image I do not have any errors. So how can I get the errors for that field and if there are no erorrs how can I get the name so After validation to add the file into the db? Thank you in advance and sorry for my English if is not so good.

1

You can get the errors from the model by using $model->getErrors(). That will return an array of errors indexed by the attribute name.

Since you submitted over AJAX, you'll want to use $this->returnErrorJson() to return them and have your JavaScript parse the errors out to display them to the user.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.