<p>The way the Craft docs do it is basically how you are already:</p>

<pre><code>&lt;label for="userLastName"&gt;Last Name *&lt;/label&gt;
&lt;input id="userLastName" type="text" name="fields[userLastName]" {%- if entry is defined %} value="{{ entry.userLastName }}"{% endif -%}&gt;
</code></pre>

CC BY-SA 3.0
2014-10-28T12:56:54.867Z
Cool. Then I guess this is the way to go forward. Cheers! :)
naboovalley
CC BY-SA 3.0
2014-10-28T14:56:05.020Z
Worth noting... You can also use Twig's [ternary syntax](http://twig.sensiolabs.org/doc/templates.html#other-operators): `{{- entry is defined ? 'value="'~entry.userLastName~'"' -}}`
Lindsey D
CC BY-SA 3.0
2014-10-28T12:52:24.690Z
2014-10-28T12:52:24.690Z
darylknight
<p>If you use that form to edit entries AND add new ones, then yes, this is probably the correct way to do this. But if this is an edit only form / template then you could check for the model once and if no <code>model is defined</code> don't show your form at all.</p>

<pre><code>{% if model is defined %}
    &lt;input type="text" name="fieldName" value="{{ model.fieldName}}"/&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-10-28T12:17:30.773Z
This is for a regular form the user submits info with and if the validation fails the user is promted with having to correct it's data before re-submitting.
naboovalley
CC BY-SA 3.0
2014-10-28T12:57:08.217Z
No. Then using it inline within your input fields is correct, @naboo!
carlcs
CC BY-SA 3.0
2014-10-28T11:44:55.413Z
2014-10-28T11:44:55.413Z
carlcs
<p>I have a Plugin which saves data from a form the user enters on my front-end. When the input data is wrong and I display my validation errors using <em>getErrors</em> from the Model I want to re-populate all my form fields with the submitted data in my Twig template.</p>

<p>Right now I'm doing this like this:</p>

<pre><code>&lt;input type="text" name="fieldName" value="{% if model is defined %}{{ model.fieldName}}{% endif %}"/&gt;
</code></pre>

<p>Is this the correct way of doing this or is there a better practice?</p>

<p>Seems like there's to many unnecessary <em>if</em> statements going on...</p>

<p>Thank you</p>

<templating><form><conditional>
Best way to re-populate input field with Model data on front-end?
# Best way to re-populate input field with Model data on front-end?

(Asked by: username_0 on 2014-10-28)

I have a Plugin which saves data from a form the user enters on my front-end. When the input data is wrong and I display my validation errors using getErrors from the Model I want to re-populate all my form fields with the submitted data in my Twig template.
Right now I'm doing this like this:


Is this the correct way of doing this or is there a better practice?
Seems like there's to many unnecessary if statements going on...
Thank you

**Tags:** templating, form, conditional


---

## 2 answers

### Answer by username_1 on 2014-10-28. Score: 3 | Accepted answer

The way the Craft docs do it is basically how you are already:
Last Name *

> **Comments:**
> **username_0**: Cool. Then I guess this is the way to go forward. Cheers! :)  
> **username_2**: Worth noting... You can also use Twig's [ternary syntax](http://twig.sensiolabs.org/doc/templates.html#other-operators): `{{- entry is defined ? 'value="'~entry.userLastName~'"' -}}`  

---

### Answer by username_3 on 2014-10-28. Score: 0

If you use that form to edit entries AND add new ones, then yes, this is probably the correct way to do this. But if this is an edit only form / template then you could check for the model once and if no model is defined don't show your form at all.
{% if model is defined %}
    
{% endif %}

> **Comments:**
> **username_0**: This is for a regular form the user submits info with and if the validation fails the user is promted with having to correct it's data before re-submitting.  
> **username_3**: No. Then using it inline within your input fields is correct, @naboo!  

---

