<p>Custom field validation happens automatically for you when you pass your element into <code>craft()-&gt;elements-&gt;saveElement()</code>. That function will return <code>true</code> or <code>false</code>, largely depending on whether custom field validation passes.</p>

<p>See <code>EntriesService::saveEntry()</code> for an example.</p>

CC BY-SA 3.0
2014-07-28T00:01:46.500Z
2014-07-28T00:01:46.500Z
Brandon Kelly
<p>I am finishing up the last few details on my custom element type. I have custom statuses and public submissions working. The last detail is providing validation on custom fields set through the field layout outside of the control panel.</p>

<p>Here is a brief overview of the plugin:</p>

<ol>
<li>PluginNameElementType - that allows custom fields via the field layout.</li>
<li>PluginNameController - which has an action 'actionSubmit' which is allowed public access to submit entries outside of the control panel. </li>
</ol>

<p>The code for public submission is on this post:</p>

<p><a href="https://craftcms.stackexchange.com/questions/1261/allow-anonymous-submission-on-a-custom-element-type">Allow anonymous submission on a custom element type?</a></p>

<p>I have seen some Yii examples that include <code>CActiveForm::validate()</code> and pass a  model to validate the input, but I don't see that anywhere in Craft.</p>

<p>The way I see it, there are two parts to validation on custom element types from outside of the control panel.  </p>

<p>The first part being providing authentication for the attributes defined as 'required' in the element types model.</p>

<p>For example, if you have the following code:</p>

<pre class="lang-php prettyprint-override"><code>protected function defineAttributes()
{
    return array_merge(
        parent::defineAttributes(), array(
            'formId' =&gt; array(
                AttributeType::Number,
                'required' =&gt; true
            ),
            'fieldName1' =&gt; array(
                AttributeType::String,
                'required' =&gt; true
            ),
            'fieldName2' =&gt; array(
                AttributeType::String,
                'required' =&gt; true
            ),
        )
    );
}
</code></pre>

<p>How would I access and return this in the controller action method that allows public submission? Does Craft provide an additional layer on top of Yii to assist with validation?</p>

<p>The second part of the question is similar, but unique all the same. The other part I need to provide validation for is the custom fields set in the field layout of the element type. If a user sets the fields to required in the same method they would with a regular entry field. How would I access validation on the custom fields level?</p>

<plugin-development><controller><elementtype><custom-field><validation>
Using the required field validation in custom element types/plugins?
# Using the required field validation in custom element types/plugins?

(Asked by: username_0 on 2014-07-27)

I am finishing up the last few details on my custom element type. I have custom statuses and public submissions working. The last detail is providing validation on custom fields set through the field layout outside of the control panel.
Here is a brief overview of the plugin:

PluginNameElementType - that allows custom fields via the field layout.
PluginNameController - which has an action 'actionSubmit' which is allowed public access to submit entries outside of the control panel. 

The code for public submission is on this post:
Allow anonymous submission on a custom element type?
I have seen some Yii examples that include CActiveForm::validate() and pass a  model to validate the input, but I don't see that anywhere in Craft.
The way I see it, there are two parts to validation on custom element types from outside of the control panel.  
The first part being providing authentication for the attributes defined as 'required' in the element types model.
For example, if you have the following code:
protected function defineAttributes()
{
    return array_merge(
        parent::defineAttributes(), array(
            'formId' => array(
                AttributeType::Number,
                'required' => true
            ),
            'fieldName1' => array(
                AttributeType::String,
                'required' => true
            ),
            'fieldName2' => array(
                AttributeType::String,
                'required' => true
            ),
        )
    );
}

How would I access and return this in the controller action method that allows public submission? Does Craft provide an additional layer on top of Yii to assist with validation?
The second part of the question is similar, but unique all the same. The other part I need to provide validation for is the custom fields set in the field layout of the element type. If a user sets the fields to required in the same method they would with a regular entry field. How would I access validation on the custom fields level?

**Tags:** plugin-development, controller, elementtype, custom-field, validation

---

## 1 answers

### Answer by username_1 on 2014-07-28. Score: 2 | Accepted answer

Custom field validation happens automatically for you when you pass your element into `craft()->elements->saveElement()`. That function will return `true` or `false`, largely depending on whether custom field validation passes.

See `EntriesService::saveEntry()` for an example.


---

