<p>I'm not sure if that's possible since I never used Sproud Forms but you can easily create a custom field type with the additional validation rule.</p>

<ol>
<li>Create a <a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow noreferrer">Plugin</a> you can use <a href="https://pluginfactory.io/" rel="nofollow noreferrer">Pluginfactory.io</a> for that and install it</li>
<li>Create a new <a href="https://craftcms.com/docs/plugins/field-types" rel="nofollow noreferrer">Field</a> </li>
<li><p>Just extend the basic <code>AssetsFieldType</code></p>

<pre><code>class myNewAssetField extends AssetsFieldType{

}
</code></pre></li>
<li><p>Take a look at the default validation rules for <a href="https://craftcms.com/classreference/fieldtypes/BaseElementFieldType" rel="nofollow noreferrer">BaseElementFieldType</a></p>

<pre><code>public function validate($value)
{
    $errors = array();

    if ($this-&gt;allowLimit &amp;&amp; ($limit = $this-&gt;getSettings()-&gt;limit) &amp;&amp; is_array($value) &amp;&amp; count($value) &gt; $limit)
    {
        if ($limit == 1)
        {
            $errors[] = Craft::t('There can’t be more than one selection.');
        }
        else
        {
            $errors[] = Craft::t('There can’t be more than {limit} selections.', array('limit' =&gt; $limit));
        }
    }

    if ($errors)
    {
        return $errors;
    }
    else
    {
        return true;
    }
}
</code></pre></li>
<li><p>Copy the function above in your new class and include something like this somewhere</p>

<pre><code>foreach ($value as $assetId){
    $asset = craft()-&gt;assets-&gt;getFileById($assetId);
    if($asset-&gt;size &gt;= 10){ // insert your size here
        $errors[] = Craft::t('Your file is too large');
    }
}
</code></pre></li>
</ol>

<p>It took me about 5 minutes to create this new field type to allow the validation of file sizes</p>

<p>If you want to make it look pretty and create a setting to dynamically set the individual max file size for each field you can include the function <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/fieldtypes/BaseElementFieldType.php#L101" rel="nofollow noreferrer">getSettingsHtml</a> in your new class and change add your new value (for example maxSize) you can then grab the new size with <code>$this-&gt;getSettings()-&gt;maxSize</code></p>

CC BY-SA 3.0
2018-02-08T07:23:08.363Z
2018-02-08T07:28:48.203Z
2018-02-08T07:28:48.203Z
Robin Schambach
<p>Does anyone know if it's possible to validate the size of an attachment for Sprout Forms file upload? I'd like to limit attachments to a maximum file size.</p>

<plugin-sproutforms>
Validate attachments for file size?
# Validate attachments for file size?

(Asked by: username_0 on 2018-02-07)

Does anyone know if it's possible to validate the size of an attachment for Sprout Forms file upload? I'd like to limit attachments to a maximum file size.

**Tags:** plugin-sproutforms


---

## 1 answers

### Answer by username_1 on 2018-02-08. Score: 1

I'm not sure if that's possible since I never used Sproud Forms but you can easily create a custom field type with the additional validation rule.

Create a Plugin you can use Pluginfactory.io for that and install it
Create a new Field 
Just extend the basic AssetsFieldType
class myNewAssetField extends AssetsFieldType{

}

Take a look at the default validation rules for BaseElementFieldType
public function validate($value)
{
    $errors = array();

    if ($this->allowLimit && ($limit = $this->getSettings()->limit) && is_array($value) && count($value) > $limit)
    {
        if ($limit == 1)
        {
            $errors[] = Craft::t('There can’t be more than one selection.');
        }
        else
        {
            $errors[] = Craft::t('There can’t be more than {limit} selections.', array('limit' => $limit));
        }
    }

    if ($errors)
    {
        return $errors;
    }
    else
    {
        return true;
    }
}

Copy the function above in your new class and include something like this somewhere
foreach ($value as $assetId){
    $asset = craft()->assets->getFileById($assetId);
    if($asset->size >= 10){ // insert your size here
        $errors[] = Craft::t('Your file is too large');
    }
}


It took me about 5 minutes to create this new field type to allow the validation of file sizes
If you want to make it look pretty and create a setting to dynamically set the individual max file size for each field you can include the function getSettingsHtml in your new class and change add your new value (for example maxSize) you can then grab the new size with $this->getSettings()->maxSize


---

