<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>

CC BY-SA 3.0
2015-06-03T21:21:09.873Z
Bam! Cheers dude
Alec Ritson
CC BY-SA 3.0
2015-06-03T21:21:58.510Z
No problem @AlecRitson :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-06-03T20:47:05.340Z
2015-06-03T20:55:47.460Z
2015-06-03T20:55:47.460Z
Mats Mikkel Rummelhoff
<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>

<plugin-development><entries>
Can you get only fields marked as required via a plugin?
# 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 – has an attribute called fieldLayout, which will return the field layout (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), 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 @AlecRitson :)  

---

