<p>You can pass the element off to <a href="http://buildwithcraft.com/classreference/services/ContentService#validateContent-detail" rel="nofollow">ContentService::validateContent()</a> in the event that it doesn't pass the record’s validation, just to get those  errors mixed into the element as well.</p>

<pre class="lang-php prettyprint-override"><code>if (!$record-&gt;validate())
{
    // Copy the record's errors over to the element model
    $element-&gt;addErrors($record-&gt;getErrors());

    // Might as well validate the content as well,
    // so we get a complete list of validation errors
    if (!craft()-&gt;content-&gt;validateContent($element))
    {
        // Copy the content model's errors over to the element model
        $element-&gt;addErrors($element-&gt;getContent()-&gt;getErrors());
    }

    return false;
}
</code></pre>

CC BY-SA 3.0
2014-10-31T05:34:07.560Z
Brilliant, this worked like a charm. Thanks Brandon!
Lindsey D
CC BY-SA 3.0
2014-10-30T20:40:53.717Z
2014-10-30T20:40:53.717Z
Brandon Kelly
<p>My plugin has a custom element type. The element type uses a standard "title" field, implemented by <code>MyElementType::hasTitles()</code> returning <code>true</code>.</p>

<p>In addition, my element type contains a few hard-coded fields. The whole plugin has been built out and working great, I'm very happy with how all of the components interact.</p>

<p>However, I'm trying to refine the <strong>validation</strong> of when an element is saved. It's easy to make a field required by simply adding <code>'required' =&gt; true</code> when defining the field in my Record. And when a user attempts to save an element without filling in that field, the proper error message will be triggered and displayed in my template.</p>

<p>But here's the catch...</p>

<p>If the user leaves empty both the <strong>title field</strong> and my <strong>hard-coded field</strong>, only one of them will display an error message at a time. Specifically, only the hard-coded field will display the error message first. Once that has been filled in, the title error message will be triggered on the second submission.</p>

<p>What am I missing? Is there a specific way to merge these error messages together so that they are both returned to the template at the same time?</p>

<plugin-development><validation>
Merge element type errors? (title + hard-coded field)
# Merge element type errors? (title + hard-coded field)

(Asked by: username_0 on 2014-10-30)

My plugin has a custom element type. The element type uses a standard "title" field, implemented by `MyElementType::hasTitles()` returning `true`.

In addition, my element type contains a few hard-coded fields. The whole plugin has been built out and working great, I'm very happy with how all of the components interact.

However, I'm trying to refine the **validation** of when an element is saved. It's easy to make a field required by simply adding `'required' => true` when defining the field in my Record. And when a user attempts to save an element without filling in that field, the proper error message will be triggered and displayed in my template.

But here's the catch...

If the user leaves empty both the **title field** and my **hard-coded field**, only one of them will display an error message at a time. Specifically, only the hard-coded field will display the error message first. Once that has been filled in, the title error message will be triggered on the second submission.

What am I missing? Is there a specific way to merge these error messages together so that they are both returned to the template at the same time?

**Tags:** plugin-development, validation

---

## 1 answers

### Answer by username_1 on 2014-10-30. Score: 4 | Accepted answer

You can pass the element off to ContentService::validateContent() in the event that it doesn't pass the record’s validation, just to get those  errors mixed into the element as well.
if (!$record->validate())
{
    // Copy the record's errors over to the element model
    $element->addErrors($record->getErrors());

    // Might as well validate the content as well,
    // so we get a complete list of validation errors
    if (!craft()->content->validateContent($element))
    {
        // Copy the content model's errors over to the element model
        $element->addErrors($element->getContent()->getErrors());
    }

    return false;
}

> **Comments:**
> **username_0**: Brilliant, this worked like a charm. Thanks Brandon!  

---

