Answers.Body: <p>Your <code>$entry</code> variable – i.e. an instance of <a href="https://buildwithcraft.com/classreference/models/EntryModel" rel="nofollow">EntryModel</a> – has an attribute called <code>fieldLayout</code>, which will return the field layout (<a href="https://buildwithcraft.com/classreference/models/FieldLayoutModel" rel="nofollow">FieldLayoutModel</a>) for this entry.</p>
  
  <p>Via the field layout, you can loop over all fields for the entry (each field will be an instance of <a href="https://buildwithcraft.com/classreference/models/FieldLayoutFieldModel" rel="nofollow">FieldLayoutFieldModel</a>), checking their <code>required</code> attribute:</p>
  
  <pre><code>// Get all fields for this entry
  $fields = $entry-&gt;fieldLayout-&gt;fields;
  
  // Loop over fields, check required attribute
  $requiredFields = array();
  foreach ($fields as $fieldModel) {
      if ($fieldModel-&gt;required) {
          $requiredFields[] = $fieldModel-&gt;field;
      }
  }
  </code></pre>
  
  <p>In the above example, <code>$requiredFields</code> will be an array of required fields, i.e. FieldLayoutFieldModels. If you only need an array of the required attribute names for your entry, you can opt to only store the field <em>handles</em>:</p>
  
  <pre><code>$requiredAttributes = array();
  foreach ($fields as $fieldModel) {
      if ($fieldModel-&gt;required) {
          $requiredAttributes[] = $fieldModel-&gt;field-&gt;handle;
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-03T21:21:09.873Z
Answers.Text: Bam! Cheers dude
Answers.UserDisplayName: Alec Ritson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-03T21:21:58.510Z
Answers.Text: No problem @AlecRitson :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-03T20:47:05.340Z
Answers.LastActivityDate: 2015-06-03T20:55:47.460Z
Answers.LastEditDate: 2015-06-03T20:55:47.460Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>Is it possible, on a model, to get only the fields that were marked as required?</p>
  
  <p>For example I have an entry which has just been saved and I can get it's content like:</p>
  
  <p><code>$entry-&gt;getContent()-&gt;getAttributes()</code></p>
  
  <p>But I need to filter it out by the only the fields that were required, because it already has a section assigned and is saved I thought this might be possible...I tried <code>$entry-&gt;getRequiredFields()</code> but that didn't work. </p>
  
  <p>So then I tried these to no avail</p>
  
  <pre><code>$entry-&gt;getContent()-&gt;getRequiredAttributes()
  $entry-&gt;getContent()-&gt;getRequiredFields()
  $entry-&gt;getRequiredAttributes()
  </code></pre>
  
  <p>Is this actually achievable? any help or pointers would be much appreciated! :)</p>
  
Tags: <plugin-development><entries>
Title: Can you get only fields marked as required via a plugin?
ThreadText: # Can you get only fields marked as required via a plugin?
  
  (Asked by: username_0 on 2015-06-03)
  
  Is it possible, on a model, to get only the fields that were marked as required?
  
  For example I have an entry which has just been saved and I can get it's content like:
  
  `$entry->getContent()->getAttributes()`
  
  But I need to filter it out by the only the fields that were required, because it already has a section assigned and is saved I thought this might be possible...I tried `$entry->getRequiredFields()` but that didn't work.
  
  So then I tried these to no avail
  
  ```
  $entry->getContent()->getRequiredAttributes()
  $entry->getContent()->getRequiredFields()
  $entry->getRequiredAttributes()
  
  ```
  
  Is this actually achievable? any help or pointers would be much appreciated! :)
  
  **Tags:** plugin-development, entries
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-06-03. Score: 3 | Accepted answer
  
  Your `$entry` variable – i.e. an instance of [EntryModel](https://buildwithcraft.com/classreference/models/EntryModel) – has an attribute called `fieldLayout`, which will return the field layout ([FieldLayoutModel](https://buildwithcraft.com/classreference/models/FieldLayoutModel)) for this entry.
  
  Via the field layout, you can loop over all fields for the entry (each field will be an instance of [FieldLayoutFieldModel](https://buildwithcraft.com/classreference/models/FieldLayoutFieldModel)), checking their `required` attribute:
  
  ```
  // Get all fields for this entry
  $fields = $entry->fieldLayout->fields;
  
  // Loop over fields, check required attribute
  $requiredFields = array();
  foreach ($fields as $fieldModel) {
      if ($fieldModel->required) {
          $requiredFields[] = $fieldModel->field;
      }
  }
  
  ```
  
  In the above example, `$requiredFields` will be an array of required fields, i.e. FieldLayoutFieldModels. If you only need an array of the required attribute names for your entry, you can opt to only store the field *handles*:
  
  ```
  $requiredAttributes = array();
  foreach ($fields as $fieldModel) {
      if ($fieldModel->required) {
          $requiredAttributes[] = $fieldModel->field->handle;
      }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Bam! Cheers dude  
  > **username_1**: No problem @username_0 :)  
  
  ---
  
  