3

I've built a front end user registration form and everything is working well with it except that it's not showing errors for my custom fields. They're set as required in the CP, but the form submits even if they aren't filled in. When the standard required fields aren't filled in the for returns an error but none for the custom fields.

I understand first and last name can't be required. In the example below my custom required fields are participantPhone and participantCity.

<form method="post" accept-charset="UTF-8">
  <div class="form-group signup-form">
      {{ csrfInput() }}
      <input type="hidden" name="action" value="users/save-user">
      {{ redirectInput('sign-up-ty') }}

      {% macro errorList(errors) %}
        {% if errors %}
          <ul class="errors">
            {% for error in errors %}
              <li>{{ error }}</li>
            {% endfor %}
          </ul>
        {% endif %}
      {% endmacro %}

      {% from _self import errorList %}

      <input id="username" class="form-control" type="text" name="username" {%- if user is defined %} value="{{ user.username }}"{% endif -%} placeholder="username">

      {% if user is defined %}
        {{ errorList(user.getErrors('username')) }}
      {% endif %}

      <input id="firstName" class="form-control" type="text" name="firstName" {%- if user is defined %} value="{{ user.firstName }}"{% endif -%} placeholder="first name">

      <input id="lastName" class="form-control" type="text" name="lastName" {%- if user is defined %} value="{{ user.lastName }}"{% endif -%} placeholder="last name">

      <input id="email" class="form-control" type="text" name="email"{%- if user is defined %} value="{{ user.email }}"{% endif %} placeholder="email">

      {% if user is defined %}
        {{ errorList(user.getErrors('email')) }}
      {% endif %}

      <input id="password" class="form-control" type="password" name="password" placeholder="password">

      {% if user is defined %}
        {{ errorList(user.getErrors('password')) }}
      {% endif %}

      <input id="participantPhone" class="form-control" type="text" name="fields[participantPhone]" {%- if user is defined %} value="{{ user.participantPhone }}"{% endif -%} placeholder="phone">

      {% if user is defined %}
        {{ errorList(user.getErrors('participantPhone')) }}
      {% endif %}

      <input id="participantCity" class="form-control" type="text" name="fields[participantCity]" {%- if user is defined %} value="{{ user.participantCity }}"{% endif -%} placeholder="city">

      {% if user is defined %}
        {{ errorList(user.getErrors('participantCity')) }}
      {% endif %}

      <input id="register-button" class="btn btn-outline-primary" type="submit" value="Register">
    </div>
  </form>
2

From the code

// Don't validate required custom fields if it's public registration
if (!$thisIsPublicRegistration) {
    $user->setScenario(Element::SCENARIO_LIVE);
}

You have to create a custom controller

2
  • Oh wow this used to work in Craft 2. Bummer. I'm not familiar with how to create a controller. Would that be done with a plugin? – Clay Kilby Apr 26 '18 at 23:45
  • @ClayKilby Or in a module... You should read the docs or ask a new question – Robin Schambach Apr 28 '18 at 18:52
0

Ran into this issue. I have a site (updated it from Craft 2) where I need the custom fields to be validated on the public registration form.

I copied the entire controller from vendor/craftcms/cms/src/controllers/UsersController.php into my module (created with https://pluginfactory.io), adjusted the namespace and removed the conditional.

namespace modules\modulehandle\controllers;
use modules\modulehandle\ModuleHandle;

// Instead of:
// if (!$thisIsPublicRegistration) {
//    $user->setScenario(Element::SCENARIO_LIVE);
// }

$user->setScenario(Element::SCENARIO_LIVE);

This feels awful. Duplicating an entire controller, just to remove one conditional. I’ll miss updates to the controller and everything. An option in general.php would have solved this pretty great. I’ll ask the Craft folks if there is any chance that this will become a setting.

1

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.