0

Check this fiddle:http://jsfiddle.net/8b32e/

All working fine there. It validate fields. Check if digits, check if blank. If invalid error label appears, if valid it is disappears.

$("input").each(function () {
    $(this).rules("add", {
        required: true,
        digits: true,
        messages: {
            required: 'It is required',
            digits: 'Only digits can be there'
        }
    });
});

If I add remote rule to rule list, error label is appearing, but if field getting valid, only classes message and error are disappearing, but label is still shown (but must be removed). Check this fiddle : http://jsfiddle.net/2LRv7/7/

Block with rules with remoute:

$("input").each(function () {
    var fieldName = $(this).attr('name');
    $(this).rules("add", {
        required: true,
        digits: true,
        remote: {
            url: "/inc/json.php?action=get_last_counter_value",
            type: "post",
            data: {
                id: fieldName
            }
        },
        messages: {
            required: 'It is required',
            digits: 'Only digits can be there',
            remote: 'Fix this please.'
        }
    });
});

What is this?

ADD:
php script from remote url:

    $DBH = connectToDatabase();

    $user_id = $_SESSION['OplataUser_ID'];
    $counter_id = $_POST['id'];
    $newValue = $_POST["".$counter_id.""];

    $params = array();
    $params['user_id'] = $user_id;
    $params['counter_id'] = $counter_id;
    $STH = $DBH->prepare("
        SELECT CounterValue as Value FROM CounterValues
        WHERE 
            UserID = :user_id
        AND 
            UserCounterID = :counter_id
        ORDER BY 
            DateCreate
            DESC
        LIMIT 1
    ");
    $STH->execute($params);

    $lastValue = $STH->fetch(PDO::FETCH_OBJ)->Value;

    if($lastValue > $newValue)
    { $result = "false"; }
    else
    { $result = "true"; }
    print $result;

    $DBH = null;

So this script returns only true or false.

Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
  • Your jsFiddle is screwy. The jQuery Validation is completely broken until I [remove your `errorPlacement` callback](http://jsfiddle.net/2LRv7/8/). Not to mention that your `remote` rule is not going to work from within jsFiddle anyway. Also, you need to show the `php` code for the `/inc/json.php?action=get_last_counter_value` function or explain exactly what it's returning back. – Sparky Mar 21 '14 at 19:44
  • I will edit my question in a few mins and explain. – Sharikov Vladislav Mar 21 '14 at 19:48
  • If you want the jQuery Validate plugin's `remote` rule to function properly, you need to `echo true` or `echo false` to it... not `print`. – Sparky Mar 21 '14 at 19:53
  • Okay, I will edit my php code, thank you. But check fiddle, there are no php code. There are same situation on my local machine. Classes `message` and `error` removing from error label element, but element is not removing. Same as on fiddle there. – Sharikov Vladislav Mar 21 '14 at 19:59
  • I'm not sure what we're even talking about... must be some language barrier. I thought your `remote` rule wasn't working... we cannot even use jsFiddle to test that. – Sparky Mar 21 '14 at 20:00
  • You still think remote is not working? I have same problem at jsFiddle and my localhost. Look [this gif](http://i.gyazo.com/49f8026fd6397d4c4f9661c71e7dfbc2.gif). placeholder (default value) is last value. I input value less than last and got error. After that I added `33`, value 1133 is greated than old value, so field have to get `valid` now. It is getting. Classes got removed, but label not. Now [this image](http://i.gyazo.com/a4998b34b1a208e782a192512e0d5d37.png) it shows requests (firebug). They are fine too. – Sharikov Vladislav Mar 21 '14 at 20:24
  • 1
    What else can it be? The plugin automatically toggles the error messages. If the `remote` rule gets a `true` response then the error label should be removed. It's not that complicated. To make a simple demo, strip out all of the options... just use `.validate()` with the rules and test again. If it suddenly starts working, you'll know where to look. If not, then `remote` is not working. – Sparky Mar 21 '14 at 20:28
  • [check this gif](http://i.gyazo.com/cc6ce0a456b7c700ca8e6a5f491d923a.gif) . It is working now :( But the question is still opened :( I need my format :( – Sharikov Vladislav Mar 21 '14 at 20:32
  • Go back to one of my first comments. Focus on `errorPlacement`... it totally broke your second jsFiddle. – Sparky Mar 21 '14 at 20:34
  • What exactly is broken? I checked it 10 times and its okay. If I will comment `errorPlacemen` as you did, I have same error (label is not removing). Mhm. This error appears on jsFiddle, but not appears on my localhost (if `errorPlacement` commented). Can you check this section? Is it right way to put `
    ` there like I did?
    – Sharikov Vladislav Mar 21 '14 at 20:42
  • That's the part that breaks it. This one is working for me: http://jsfiddle.net/c5xZL/ ~ I have no idea what you're trying to achieve with `
    ` & jQuery that you couldn't just do with regular CSS.
    – Sparky Mar 21 '14 at 20:43
  • I am trying to move block with error on another lane with `
    ` tag. I tried your fiddle on my localhost and it still didn't working (don't working properly. jQuery removing classes, but not element still). If `errorPlacement` is removed all working fine.
    – Sharikov Vladislav Mar 21 '14 at 20:46
  • Yeah, just remove `errorPlacement` and take care of your `label` position with your CSS. Clearly, you're breaking something with it, but since I cannot test `remote` method with jsFiddle, I cannot see what you see. – Sparky Mar 21 '14 at 20:52
  • Well this will the solve problem, but (I rly don't know how to say it on english) it is called reinventing the wheel :) I had 1 problem: how to move error label on the another string via jQuery, now I had another: how to do this with css :) – Sharikov Vladislav Mar 21 '14 at 20:55
  • First explain where you want this element to appear. Under the `input`? – Sparky Mar 21 '14 at 20:58
  • Yes. I want this element to appear right under input. – Sharikov Vladislav Mar 21 '14 at 20:59
  • EASY!! Just use the `errorElement: "div"` option and that will change the `label` into a `div` and it will naturally appear _under_ the `input` because a `div` is a block. – Sparky Mar 21 '14 at 21:01
  • Okay. Soooo. I think you are right. Sounds good. Can you edit your question so will give you a bounty? – Sharikov Vladislav Mar 21 '14 at 21:03
  • Also. Can I give you link to another question? Maybe you will help there. They are connected to each other. Oops. I can award your bounty only in 22 hours: "You may award your bounty in 22 hours". – Sharikov Vladislav Mar 21 '14 at 21:06
  • Sry, I can award bounty only in 22 hours. I will do it later. The question : http://stackoverflow.com/questions/22492862/validating-input-fields-usability . Check @ml242 answer please. I think 200-300 ms delay is what I need. Is it possible to add this delay in my script? – Sharikov Vladislav Mar 21 '14 at 21:08

3 Answers3

1

As long as the remote rule receives the proper response from the server, the plugin will automatically toggle the message.

If your PHP result needs validation to fail, do this...

echo 'false';

OR

echo json_encode('some error message');

If your PHP result needs validation to pass, do this...

echo 'true';

As per documentation:

"The serverside resource is called via jQuery.ajax (XMLHttpRequest) and gets a key/value pair corresponding to the name of the validated element and its value as a GET parameter. The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message."


Your second jsFiddle is totally broken, not by the remote method, but by the code within your errorPlacement callback.

Removing the errorPlacement callback, or putting back to its default, at least gets the plugin working again:

http://jsfiddle.net/c5xZL/


Instead of creating br elements with jQuery to place the label, just use the errorElement: "div" option and that will change the label into a div and it will naturally appear under the input because a div is a block. This way, you can avoid the errorPlacement option entirely.

errorElement: "div"
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • I edited my php code, changed `print $results;` to `echo $result;`. Same error. Label not removing. – Sharikov Vladislav Mar 21 '14 at 20:05
  • @SharikovVladislav, may I suggest that you strip down your jQuery Valdiate options to almost nothing except the rules. Just to make this simple. The plugin automatically toggles the error message as long the response for `remote` is correct and received. Also look at your console to see what error codes you get when `remote` is invoked. – Sparky Mar 21 '14 at 20:16
  • Do you mean remove all other code, except rules? [this is what remote request is returning](http://i.gyazo.com/a4998b34b1a208e782a192512e0d5d37.png) – Sharikov Vladislav Mar 21 '14 at 20:27
  • @SharikovVladislav, yes, strip all code out of `.validate()`. Only apply the rules. – Sparky Mar 21 '14 at 20:32
0

Is this what you needed? http://jsfiddle.net/YHF3b/

I just changed .removeClass(errorClass).removeClass('message');

to .removeClass(errorClass).remove()

actually just .remove() would suffice

andrew
  • 9,313
  • 7
  • 30
  • 61
0

Your issue is on the unhighlight event, you're only removing the class instead of removing the label.

Replace

   $(element.form).find("label[for=" + element.id + "]").removeClass(errorClass).removeClass('message');

with

$(element.form).find("label[for=" + element.id + "]").remove();
David Freire
  • 671
  • 9
  • 23