Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-28T16:46:18.147Z
Answers.Text: 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?
Answers.UserDisplayName: Sam Hibberd
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-29T06:39:14.720Z
Answers.Text: Updated my answer.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-11T10:24:00.210Z
Answers.Text: Any way to work around this one pre Craft 3?
Answers.UserDisplayName: Sam Hibberd
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-28T16:32:27.523Z
Answers.LastActivityDate: 2015-04-29T06:39:06.417Z
Answers.LastEditDate: 2015-04-29T06:39:06.417Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <plugin-development><custom-fieldtype>
Title: How do you determine if the current field is required from a fieldtype class?
ThreadText: # 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?  
  
  ---
  
  