Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T12:56:54.867Z
Answers.Text: Cool. Then I guess this is the way to go forward. Cheers! :)
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T14:56:05.020Z
Answers.Text: 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~'"' -}}`
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T12:52:24.690Z
Answers.LastActivityDate: 2014-10-28T12:52:24.690Z
Answers.UserDisplayName: darylknight
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T12:17:30.773Z
Answers.Text: 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.
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T12:57:08.217Z
Answers.Text: No. Then using it inline within your input fields is correct, @naboo!
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T11:44:55.413Z
Answers.LastActivityDate: 2014-10-28T11:44:55.413Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <templating><form><conditional>
Title: Best way to re-populate input field with Model data on front-end?
ThreadText: # 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:
  
  ```
  <input type="text" name="fieldName" value="{% if model is defined %}{{ model.fieldName}}{% endif %}"/>
  
  ```
  
  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:
  
  ```
  <label for="userLastName">Last Name *</label>
  <input id="userLastName" type="text" name="fields[userLastName]" {%- if entry is defined %} value="{{ entry.userLastName }}"{% endif -%}>
  
  ```
  
  > **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 %}
      <input type="text" name="fieldName" value="{{ model.fieldName}}"/>
  {% 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!  
  
  ---
  
  