<p>I could not find a way to get a specific field for a model but you can get all and loop through them</p>

<pre><code>&lt;!-- fetch all fields for your entryType --&gt;
{% for field in craft.entries.section('yourSection').type('yourEntryType').first.getFieldLayout().getFields() %}
    &lt;!-- loop them and check the handle if it is your field --&gt;
    {% if field.getField().handle == "yourFieldHandle" %}
        &lt;!-- the required attribute --&gt;
        {% set required = field.required %}
        {{ field.getField().name }}
        {{ field.getField().instructions }}
    {% endif %}
{% endfor %}
</code></pre>

<p>Edit: if you really want to have only one field instead of the array I could create php code for you but since many people refuse to use php in craft I presented the "slower" twig method</p>

<p>Edit: There is a <code>required</code> property in both the <code>FieldLayoutFieldModel</code> (stored above in <code>field</code>) and in the <code>FieldModel</code> (<code>field.getField()</code> above). What we want as the required flag of the entry type is the first: <code>field.required</code>. This is what appears to be the little red asterisk in Craft's backend.</p>

CC BY-SA 3.0
2017-11-11T11:08:14.080Z
Wow, thanks for the fast reply. No I want all fields - was just written generally. What I noticed: Calling `getFields()` on a FieldLayout returns an array of `FieldLayoutFieldModel`s with the desired property `required` for this entry type but without properties like `name` or `instructions`. These two can only(?) be found in the appropriate `FieldModel` which I can get with `...getFieldLayout().getFields()[0].field`. Wow this gets almost too clunky for a Twig template.
Johannes
CC BY-SA 3.0
2017-11-11T11:13:25.880Z
I updated my answer in order to display the name and the instructions. You just have to use the `getField()` function
Robin Schambach
CC BY-SA 3.0
2017-11-11T17:33:24.097Z
I updated your answer as there are multiple required properties where `field.required` is the right one in the example.
Johannes
CC BY-SA 3.0
2017-11-11T08:49:09.690Z
2017-11-12T00:11:36.013Z
2017-11-12T00:11:36.013Z
Robin Schambach
<p>I am trying to get the required attribute of a field in a frontend form. I know this attribute only makes sense in the context of an entry type. Doing this</p>

<p><code>craft.fields.getFieldByHandle('handle')</code></p>

<p>returns a FieldModel object which has no context to any entry type. This way I can get the field name and the instructions for example. In Craft we set the required attribute in the entry type editor. So my question is:</p>

<p><em>How can I get a field's required attribute in the context of a specific entry type?</em></p>

<p>Edit: My goal is to create a frontend form of an entry type and have every change to the field's required flags in the backend automatically reflected in the frontend form: <code>&lt;input required ...&gt;</code>.</p>

<templating><field><frontend><frontend-form>
Get a field's required attribute in frontend form
# Get a field's required attribute in frontend form

(Asked by: username_0 on 2017-11-11)

I am trying to get the required attribute of a field in a frontend form. I know this attribute only makes sense in the context of an entry type. Doing this

`craft.fields.getFieldByHandle('handle')`

returns a FieldModel object which has no context to any entry type. This way I can get the field name and the instructions for example. In Craft we set the required attribute in the entry type editor. So my question is:

*How can I get a field's required attribute in the context of a specific entry type?*

Edit: My goal is to create a frontend form of an entry type and have every change to the field's required flags in the backend automatically reflected in the frontend form: .

**Tags:** templating, field, frontend, frontend-form

---

## 1 answers

### Answer by username_1 on 2017-11-11. Score: 2 | Accepted answer

I could not find a way to get a specific field for a model but you can get all and loop through them

{% for field in craft.entries.section('yourSection').type('yourEntryType').first.getFieldLayout().getFields() %}
    
    {% if field.getField().handle == "yourFieldHandle" %}
        
        {% set required = field.required %}
        {{ field.getField().name }}
        {{ field.getField().instructions }}
    {% endif %}
{% endfor %}

Edit: if you really want to have only one field instead of the array I could create php code for you but since many people refuse to use php in craft I presented the "slower" twig method
Edit: There is a required property in both the FieldLayoutFieldModel (stored above in field) and in the FieldModel (field.getField() above). What we want as the required flag of the entry type is the first: field.required. This is what appears to be the little red asterisk in Craft's backend.

> **Comments:**
> **username_0**: Wow, thanks for the fast reply. No I want all fields - was just written generally. What I noticed: Calling `getFields()` on a FieldLayout returns an array of `FieldLayoutFieldModel`s with the desired property `required` for this entry type but without properties like `name` or `instructions`. These two can only(?) be found in the appropriate `FieldModel` which I can get with `...getFieldLayout().getFields()[0].field`. Wow this gets almost too clunky for a Twig template.  
> **username_1**: I updated my answer in order to display the name and the instructions. You just have to use the `getField()` function  
> **username_0**: I updated your answer as there are multiple required properties where `field.required` is the right one in the example.  

---

