<p>You can get the errors from the model by using <code>$model-&gt;getErrors()</code>.  That will return an array of errors indexed by the attribute name.</p>

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

CC BY-SA 3.0
2015-04-29T18:58:48.280Z
2015-04-29T18:58:48.280Z
Brad Bell
<p>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:</p>

<pre><code>                &lt;fieldset&gt;
                    &lt;div class="file-upload btn btn-primary"&gt;
                        &lt;span&gt;Upload File&lt;/span&gt;
                        &lt;input type="file" name="fields[myAssetNameHandle]"&gt;
                    &lt;/div&gt;              
                &lt;/fieldset&gt;
</code></pre>

<p>I send the form to the server with ajax like this:</p>

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

<p>On the myPluginController I have validation</p>

<pre><code>    $model = new myModel();
    $model-&gt;test_var = craft()-&gt;request-&gt;getPost('test_var');

    if ( $model-&gt;validate() ) {

    }else{

    }
</code></pre>

<p>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.</p>

<plugin-development><assets><upload>
Get file errors after upload
# Get file errors after upload

(Asked by: username_0 on 2015-04-29)

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:
                
                    
                        Upload File
                        
                                  
                

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.

**Tags:** plugin-development, assets, upload


---

## 1 answers

### Answer by username_1 on 2015-04-29. Score: 1 | Accepted answer

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.


---

