0

I have a registration form :

<div class="register-error" id="register-error">{{ form.status_messages_user_register_form }}</div>

<div class="row">

<div class="col-sm-6">{{ form.field_first_name }}</div>

<div class="col-sm-6">{{ form.field_last_name }}</div></div>

<div class="clear"></div>
{{ form.account.mail }}
{{ form.account.name }}
<div class="row">
<div class="col-sm-6">{{ form.account.pass.pass1 }}</div>
<div class="col-sm-6">{{ form.account.pass.pass2 }}</div></div>
<div class="clear"></div>
{{ form.form_build_id }} {# required #}
{{ form.form_token }}{# required #}
{{ form.form_id }} {# required #}
{{ form.actions }}

Now i want to add custom validation for the registration.I have a custom ajax handler for registration using form alter:

$form['actions']['submit']['#ajax']  = [
         'callback'    => 'custom_register_form_ajax_validate',
         'event'       => 'click',
         '#attributes' => [
           'class' => [
             'use-ajax',
          ],
         ],
       ];
       $form['#attached']['library'][]      = 'core/drupal.dialog.ajax';
       $form['#attached']['library'][]      = 'core/drupal.ajax';

       $form['#prefix']                     = '<div id="email-valid"></div>';

And this is my function :

function custom_register_form_ajax_validate(&$form, $form_state){

$response = new AjaxResponse();

  $form_state->setTemporaryValue('entity_validated', TRUE);

  $firstname = $form['field_first_name']['widget']['#default_value'];

  $email = $form_state->getValue('mail');

  $pass1 = $form_state->getValue('pass1');

  $pass2 = $form_state->getValue('pass2');

    if($firstname == ''){
         $text = '<h5 style="color:red;">Firstname is required.</h5>';
          $response->addCommand(new InvokeCommand('.form-text', 'css', array('border-bottom', '2px solid red')));
      $response->addCommand(new HtmlCommand('#email-valid', $text));
    }
    if($lastname == ''){
         $text = '<h5 style="color:red;">Lastname is required.</h5>';
          $response->addCommand(new InvokeCommand('.form-email', 'css', array('border-bottom', '2px solid red')));
      $response->addCommand(new HtmlCommand('#email-valid', $text));
    }
    if($email == ''){
         $text = '<h5 style="color:red;">Email field is required.</h5>';
          $response->addCommand(new InvokeCommand('.form-email', 'css', array('border-bottom', '2px solid red')));
          $response->addCommand(new HtmlCommand('#email-valid', $text));
    }
}

I want to validate "Firstname is required" condition.But when i submit and validate the form,always showing "Email field is required".

How can i solve this issue?

0

You're probably not printing something required by the form since you are printing items individually. To start debugging, you can comment your entire form twig out and just use {{ form }} and see if it triggers validation.

At the same time, you are copy pasting the wrong thing in each error case:

      $response->addCommand(new InvokeCommand('.form-email', 'css', array('border-bottom', '2px solid red')));
      $response->addCommand(new HtmlCommand('#email-valid', $text));

This will always apply to elements with .form-email class and #email-valid id.

You're trying to evaluate $lastname but that variable is never set. If the email validation error always shows, then validate that you are using the right key in $form_state->getValue().

Since you are applying it to the same id, you are going to overwrite your own message each time as well. Check the documentation on HtmlCommand. It implements jQuery's html(string):

When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content.

You need to provide different divs to print these errors for the users sake, or rethink how you are doing this.

  • Thanks for your reply.Can you help me to find out how to get value using $form_state->getValue()?.I have tried with many options,but not working anything. – salu Aug 7 '18 at 16:11
  • $form_state->getValue('field_name');... I can't really debug it for you. You'll have to do that and see what the variable/values are. – Kevin Aug 7 '18 at 16:19

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.