0

There are plenty of tutorials out there like here and official jQuery Validate docs. But they're not working anymore to overwrite the required messages.

So this one does not overwrite the required message. But anyway all tutorials out there say it should work in that way:

$('#my-form').validate({
        rules: {
          mail: "required",
        },
        messages: {
          mail: {
            required: "Give an email in!",
          },
        }
  });

If I do it in this way:

$('#my-form').validate({
        rules: {
          mail: "required",
        },
        messages: {
          mail: "Give an email in!",
        }
      });

it works. And it really overwrite the usual "Field Email is required". But now I'm not able to set any other message. So this is not useful!

So how is it possible to overwrite the required text right now?

I'm using Drupal 7.51 with Clientside Validation 7.x-1.42 and jQuery 1.10.2.

Other messages are correctly owerwritten anyway

So when I take the email instead of required, it correctly owerwrites it. So it's something with the required message I would assume? What are your ideas about this:

   $('#my-form').validate({
            rules: {
              mail: "email",
            },
            messages: {
              mail: {
                email: "Invalid email",
          },
            }
   });

Also setting messages globally works, except on required

Works for email, but not on required:

jQuery.extend(jQuery.validator.messages, {
  required: "required...",
  email: "email...",
});

Possible workarounds (but no solution)

Hint: I had the issue that other forms were not working properly. But this was just an issue of setting the #required property in PHP. After setting it to all the different user-register forms, they were working as well.

Community
  • 1
  • 1
kwoxer
  • 3,734
  • 4
  • 40
  • 70

2 Answers2

2

You can always do the following:

(function(Drupal, $) {
  Drupal.behaviors.myModuleBehavior = {
    attach: function() {
      $(document).bind('clientsideValidationInitialized', function() {
        var validator = Drupal.myClientsideValidation.validators['my-form'];
        if (typeof validator.settings.messages.mail === 'undefined') {
          validator.settings.messages.mail = {};
        }
        validator.settings.messages.mail.required = 'My custom message for the "required" rule for the input with name "mail".';
      });
    }
  };
})(Drupal, jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.js"></script>
<form id="my-form">
  <input name="mail" type="mail" required="required" />
  <input type="submit" value="Submit" />
</form>

Which should work just fine.

EDIT: I updated the script to work with the Clientside Validation Drupal module. It fires the clientsideValidationInitialized event when it has finished doing all its processing.

Jelle
  • 298
  • 1
  • 11
  • Well nice idea. But for me it doesn't work. And when I look into validator after setting the required message. It still shows me the "unwanted" message from the Clientside Validation Module. What else do I need to do? Or just set a timeout on the required thing? That's totally strange behavior to be honest. – kwoxer Oct 18 '16 at 07:45
  • Indeed `setTimeout(function() { validator.settings.messages.mail.required = 'My custom message for the "required" rule for the input with name "mail".'; }, 100);` works. So somehow it's an issue of event execution and time. That's very bad. Some idea why I need this timeout? – kwoxer Oct 18 '16 at 08:17
  • Works on one Form. But I have different register Forms realized. And somehow there the mail.required message isn't overwritten. Very strange. But I'll look for a solution. Because the register form on /user/register is working properly... gimme a bit time :) – kwoxer Oct 19 '16 at 08:57
  • Ok so I figured out what my Issue is. I edit my Post above so it's better to read. :) – kwoxer Oct 20 '16 at 08:01
  • Ok great, now all is working. Thank you very much for your help. :) – kwoxer Oct 20 '16 at 11:47
0

Simply change message through field id

(function(Drupal, $) {
    Drupal.behaviors.mymodBehavior = {
      attach: function(){
        jQuery('#field_id').rules("add", {
            messages: {
              required: "Custom validation message"
           }
          });
      }
    }
  }
  )(Drupal,jQuery);
Bhupendra
  • 146
  • 2
  • 5