Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split validation into multiple methods to simplify login and registration form customizations #722

Open
wants to merge 1 commit into
base: 8.x-2.x
Choose a base branch
from

Conversation

Berdir
Copy link
Contributor

@Berdir Berdir commented Apr 18, 2017

PR for https://www.drupal.org/node/2870783

In my project, I have this now, combined with an alter hook to switch out the class:

/**
 * Customized Login pane that allows registration by e-mail.
 */
class EmailLoginPane extends Login {

  /**
   * {@inheritdoc}
   */
  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $pane_form = parent::buildPaneForm($pane_form, $form_state, $complete_form);
    $pane_form['returning_customer']['name']['#title'] = $this->t('E-mail or username');
    $pane_form['register']['name']['#access'] = FALSE;
    return $pane_form;
  }

  /**
   * {@inheritdoc}
   */
  protected function getAndValidateRegisterUserValues(array &$pane_form, FormStateInterface $form_state) {
    if ($user_values = parent::getAndValidateRegisterUserValues($pane_form, $form_state)) {
      $user_values['name'] = $user_values['mail'];
      return $user_values;
    }
  }

}

Not sure yet about spliting up the login method further.

* @param array $complete_form
* The complete form structure.
*/
protected function validateRegister(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's odd that we have a validateRegister() method that does no validation, the entire logic is in the oddly named getAndValidateRegisterUserValues, which is only used in one place. What's the reasoning behind that split?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the names are bad, suggestions welcome. The reason for the split is being able to change the user values, see my example. For that to work there needs to be a way to do something between bulding the values array and creating the entity. Another approach would be an "alter" method between or/and an event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants