<p>In Craft 1/2, there isn't a particularly clean or easy way to do this because <code>$this-&gt;model-&gt;required</code> is only set in the context of when it's being called from a field layout.</p>

<p>In the context of a field type, Craft doesn't set it because <code>required</code> is a concept that belongs to a field layout and a given field can belong to multiple field layouts.</p>

<p>This whole process has been refactored and is much cleaner in Craft 3 so that <code>$this-&gt;model-&gt;required</code> works in the context of a field type where you'd expect it to.</p>

CC BY-SA 3.0
2015-04-28T16:46:18.147Z
Hmm, doesn't play ball for me, it returns false whatever the field setting. Calling it from within a custom validate() method, that should work right?
Sam Hibberd
CC BY-SA 3.0
2015-04-29T06:39:14.720Z
Updated my answer.
Brad Bell
CC BY-SA 3.0
2015-05-11T10:24:00.210Z
Any way to work around this one pre Craft 3?
Sam Hibberd
CC BY-SA 3.0
2015-04-28T16:32:27.523Z
2015-04-29T06:39:06.417Z
2015-04-29T06:39:06.417Z
Brad Bell
<p>I need to run validation in a custom fieldtype but only if a field is required.</p>

<p>How do you determine if the current field is required from within a fieldtype class?</p>

<plugin-development><custom-fieldtype>
How do you determine if the current field is required from a fieldtype class?
# How do you determine if the current field is required from a fieldtype class?

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

I need to run validation in a custom fieldtype but only if a field is required.

How do you determine if the current field is required from within a fieldtype class?

**Tags:** plugin-development, custom-fieldtype

---

## 1 answers

### Answer by username_1 on 2015-04-28. Score: 1

In Craft 1/2, there isn't a particularly clean or easy way to do this because `$this->model->required` is only set in the context of when it's being called from a field layout.

In the context of a field type, Craft doesn't set it because `required` is a concept that belongs to a field layout and a given field can belong to multiple field layouts.

This whole process has been refactored and is much cleaner in Craft 3 so that `$this->model->required` works in the context of a field type where you'd expect it to.

> **Comments:**
> **username_0**: Hmm, doesn't play ball for me, it returns false whatever the field setting. Calling it from within a custom validate() method, that should work right?  
> **username_1**: Updated my answer.  
> **username_0**: Any way to work around this one pre Craft 3?  

---

