6

My edit profile page doensn't save all fields:

<form method="post" action="" accept-charset="UTF-8" enctype="multipart/form-data">
    <input type="hidden" name="action" value="users/saveUser">
    <input type="hidden" name="redirect" value="/users/{{ user.username }}">
    <input type="hidden" name="userId" value="{{ user.id }}">

    <fieldset>
      <legend>Your Photo</legend>
    {# Check to see if the user has a photo  #}
    {% if currentUser.photoUrl %}

      <figure><img src="{{ currentUser.photoUrl(200) }}" /></figure>
    {# If a photo exists, give the option to change the user photo #}
    <div class="form-group">
    <input type="file" name="userPhoto" value="Change Photo" class="form-control">
    </div>

    {% else %}

    {# If no photo exists, give the option to upload a new one #}
    <figure><img src="/assets/img/user.gif" /></figure>
    <div class="form-group">
    <input type="file" name="userPhoto" value="Upload" class="form-control">
    </div>
    {% endif %}
  </fieldset>


  <fieldset>
    <legend>Your Login Details</legend>
    <div class="form-group">
    <label for="email">Email address</label>
    <input type="email" class="form-control" id="email" name="fields[email]" value="{{ user.email }}">
    </div>
  </fieldset>

  <fieldset>
  <legend>Your Personal Details</legend>
  <div class="form-group">
  <label for="firstName">First Name</label>
  <input type="text" class="form-control" id="firstName" name="fields[firstName}" value="{{ user.firstName }}">
  </div>

  <div class="form-group">
  <label for="lastName">Last Name</label>
  <input type="text" class="form-control" id="lastName" name="fields[lastName]" value="{{ user.lastName }}">
  </div>

  <div class="form-group">
  <label for="phoneNumber">Contact Number</label>
  <input type="text" class="form-control" id="phoneNumber" name="fields[phoneNumber]" value="{{ user.phoneNumber}}">
  </div>
  </fieldset>

  <fieldset>
    {% set pets = craft.fields.getFieldbyHandle('pets') %}

  <legend>Your Property Details</legend>
  <label for="pets">{{ pets.instructions }}</label>
  <select class="form-control margin-bottom" id="pets" name="fields[pets]">
    {% for option in user.pets.options %}
    <option {% if option.selected %}selected="selected"{% endif %}>{{ option }}</option>
    {% endfor %}
  </select>

  {% set renting = craft.fields.getFieldbyHandle('renting') %}

  <label for="renting">{{ renting.instructions }}</label>
  <select class="form-control margin-bottom" id="renting" name="fields[renting]">
    {% for option in user.renting.options %}
    <option {% if option.selected %}selected="selected"{% endif %}>{{ option }}</option>
    {% endfor %}
  </select>

  </fieldset>


  <input type="submit" class="btn btn-primary pull-right margin-top" value="Save Profile">
</form>

It only saves the last two fields which means that a user can't change their email address, name or phone number.

I've recently updated Craft to the latest build (Craft Pro 2.1.2566) and also changed user/saveProfile to user/saveUser.

  • Just corrected it but no change. – Niall Thompson Jul 26 '14 at 18:42
2

You need to define the value= attribute on your select fields.

<select class="form-control margin-bottom" id="renting" name="fields[renting]">
{% for option in user.renting.options %}
<option {% if option.selected %}selected="selected"{% endif %}>{{ option }}</option>
{% endfor %}
</select>

Should be:

<select class="form-control margin-bottom" id="renting" name="fields[renting]">
{% for option in user.renting.options %}
<option value="{{option.value}}"{% if option.selected %}selected="selected"{% endif %}>{{ option }}</option>
{% endfor %}
</select>

I was looking for the answer to the same problem. I narrowed my issue down to the field. When I removed it the form would submit properly. I fixed a typo in the value attribute and that fixed it.

7

firstName, lastName, and email are actual user fields (not custom), so their name= attributes shouldn’t have the fields[] namespace.

<input type="email" class="form-control" id="email" name="email" value="{{ user.email }}">

<input type="text" class="form-control" id="firstName" name="firstName" value="{{ user.firstName }}">

<input type="text" class="form-control" id="lastName" name="lastName" value="{{ user.lastName }}">

I’m guessing that once you fix those, your phoneNumber custom field will also start saving.

  • That all works apart from the email field. I'm using the 'useEmailAsUsername' => true, config setting if that changes anything. – Niall Thompson Jul 26 '14 at 22:30
  • I also have the verify email setting on. Ideally this would only be sent when registering new accounts and not when a user is changing their email. – Niall Thompson Jul 26 '14 at 22:45
  • 1
    @NiallThompson to allow the user to change their email you need to pass a password also I believe. – Luke Holder Jul 27 '14 at 1:48
  • 1
    @LukeHolder nails it. Since changing the password and/or email of an existing user is "a big deal", you need to pass in the currently logged in user's password in a field named "password" in order for it to work. – Brad Bell Jul 27 '14 at 4:26
  • 1
    @NiallThompson It has to be the password of the currently logged in user. Not the password for the user you're trying to change the password of. – Brad Bell Jul 27 '14 at 23:01
3

It should also be noted that to change your password or email, the current password has to be passed along with the request.

// You must pass your current password to change these fields for an existing user
2

For starters I see you have a typo...

fields[firstName}

Should be

fields[firstName]

Also:

{{ user.phoneNumber}}

Should be:

{{ user.phoneNumber }}

  • I've made the changes but the firstName, lastName and email fields won't save still. – Niall Thompson Jul 26 '14 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.