Hello, thank you for this module and the evident effort being put into documenting and supporting it.

I'm defining a custom method very much like the one in clientside_validation.api.js.

In it, i'm trying to modify the message shown for a failed result. I've tried JQuery.extend:

              jQuery.extend(jQuery.validator.messages, {
                "ajaxFormMessage": result['message']
              });

and also the jQuery.format addition to $(document).bind, but neither have any effect-- the default message defined in PHP, $js_rules[$element_name]['messages'][ajaxFormMessage] is always used.

I've found the section that checks for $.metadata in jquery.validate.js, which looks like it may be the key, but haven't figured out how to set it.

(Rationale for a message variable in the JavaScript-- using Clientside Validation to do serverside validation, such that the message may change based on the result of a MySQL query, for instance, "That title is already used by node/123".)

(Meta question-- ought i to be developing a major dependent module using the 7.x dev branch of the code?)

Comments

attiks’s picture

This sounds similar as what fapi_validation is doing, we added a new rule regexMatchPCRE inside http://drupalcode.org/project/clientside_validation.git/blob/refs/heads/... that basically does an ajax callback to check the values and it uses the return value for the message.

To add this to your module, you'll need to provide a new rule for the javascript that calls a callback function inside your module, you also have to implement clientside_validation_form_alter to add everything to the $js_rules array.

7.x-1.x (dev) is the version you need to develop against

attiks’s picture

Status:Active» Postponed (maintainer needs more info)
mlncn’s picture

Version:7.x-1.25» 7.x-1.x-dev
Status:Postponed (maintainer needs more info)» Active

The code pointed to, in its one setting of message.

500       if (result['result'] === false) {
501         if (result['message'].length) {
502           jQuery.extend(jQuery.validator.messages, {
503             "regexMatchPCRE": result['message']
504           });
505         }
506       }
507       return result['result'];

is the same as i am using: http://drupalcode.org/project/formmsgs.git/blob/refs/heads/7.x-1.x:/form...

And the code pointed to is in clientside_validation.js, not in a clientside_validation_fapi.js file-- which leads me to believe the way it works might be hard-coded? I wish i could figure out where the ball is dropped and give more information but i haven't yet.

mlncn’s picture

To be clear, i have all the callback stuff working fine-- it's overriding the default message that i cannot figure out how to do. Will try to follow what Clientside Validation FAPI does more closely.

mlncn’s picture

Status:Active» Fixed

Wow. My mistake, but wow hard to debug.

I was defining the default message in the rule like this:

<?php
      $js_rules
[$name]['messages'][$method] = $formmsg['default_message'];
?>

Instead of, and then in addition to, like this:

<?php
      $js_rules
[$name][$method]['messages'] = $formmsg['default_message'];
?>

I recall getting that alternate way ('messages' first) from looking at how it was actually done, i think right in jquery validate. So the problem here wasn't that this other way doesn't work, but that it works way too well-- that's what made the unshakable default message, that wouldn't stop until i deleted that first line.

mlncn’s picture

Category:support» bug
Status:Fixed» Active

And... defining a default message with 'messages' at the end does not in fact work. But at least the messages changed on the fly work, which is more important to me. I'm thinking there is a bug here?

mlncn’s picture

Title:How to have a message that varies» If a default message is set in a way that works, AJAX callbacks cannot override that message
mlncn’s picture

Adding an array level to it [] also isn't working for me.

<?php
  $js_rules
[$name][$method]['messages'][] = $formmsg['default_message'];
?>
attiks’s picture

The right format is

<?php
      $js_rules
[$name][$method]['messages'] = $formmsg['default_message'];
?>

An array isn't going to work, I guess your problem is that you want to be able to return more than 1 message for a certain element, which isn't possible; jquery.validate only allows one message for a certain rule/element combination.

Or am I missing your point? If so can you link some code so I can have a look.

attiks’s picture

Status:Active» Closed (works as designed)

closing, if you need more information feel free to re-open

MarcElbichon’s picture

Can you explain what is the difference between

<?php
$js_rules
[$name]['messages'][$method] = theme('clientside_error', $variables);
?>

and

<?php
$js_rules
[$name][$method]['messages'] = theme('clientside_error', $variables);
?>
attiks’s picture

It should be $js_rules[$name]['messages'][$method] = theme('clientside_error', $variables);, so my comment in #9 is wrong, sorry

MarcElbichon’s picture

I'm not sure because i've create a new rule with ajax like this :

<?php
function _clientside_validation_set_toto($name, $title, &$js_rules, $message = "") {
 
$title = _clientside_validation_set_title($title);
 
$variables = array(
   
'message' => empty($message) ? 'toto error.' : $message,
   
'placeholders' => empty($message) ? array('!title' => $title) : array(),
   
'error_type' => 'toto',
   
'element_name' => $name
 
);
 
$js_rules[$name]['toto'] = TRUE;
 
$js_rules[$name]['messages']['toto'] = theme('clientside_error', $variables);
}
?>

and in hook_clientside_validation_rule_alter()

<?php
_clientside_validation_set_toto
($element['und'][0]['value']['#name'], $element['und'][0]['value']['#title'], $js_rules, "tttt");
?>

In my javascript, i call ajax and try to set error message by

jQuery.extend(jQuery.validator.messages, {"toto": toto['msg']});

This always print "ttttt" error message.
When i change to

<?php
 
//$js_rules[$name]['toto'] = TRUE;
  //$js_rules[$name]['messages']['toto'] = theme('clientside_error', $variables);
 
$js_rules[$name]['toto']['messages'] = theme('clientside_error', $variables);
?>

Miracle, this is working !!!!

So, how must i do ?

MarcElbichon’s picture

My bad, if i only do

<?php
  $js_rules
[$name]['toto'] = TRUE;
 
//$js_rules[$name]['messages']['toto'] = theme('clientside_error', $variables);
?>

this is working too.
Does this mean that if $js_rules[$name]['messages']['toto'] is set, message never could be overriden ?