1

I have tried all the possible approaches found, but I am struggled to get the default remote validator working.

I'm just trying to achieve to check if the mail for a registration page of a user is duplicated. Thus, I declare the input as follows:

<input type="email" name="email" required
                                   placeholder="Dirección de correo"
                                   data-parsley-trim-value="true"
                                   data-parsley-trigger="change"
                                   data-parsley-remote-options='{ "type": "POST", "dataType": "jsonp" }'
                                   data-parsley-remote="checkmail.php"
                                   data-parsley-remote-message="Mail duplicated"
                                   class="form-control">

The checkmail.php code is:

require_once("../functions/mysqli_connect.php");
if (empty($_POST['email'])){
    $errors[] = 'No llega el parametro de mail';
}else {
$email = trim($_POST['email']);

$q = ("select * from register where email ='" . $email . "'");
$r = mysqli_query($dbc, $q);

if ($r) {
    if (($r->num_rows) > 0) {
        echo json_encode(404);

    } else {
        echo json_encode(200);
    }
}

}

I've tried multiple combinations for echo response, I've read all the messages on stackoverflow but couldn't find the solution because I've always get the message : This value seems to be invalid.

As I read on another's user question I've tried:

data-parsley-remote-message="Mail duplicated"

but with no luck either.

The PHP code works as expected, so no errors there, but Parsley always treats the json_encode responses as it is was an error.

Any help would be vey appreciated.

Thanks in advance.

guillaumepotier
  • 7,369
  • 8
  • 45
  • 72
Esteve
  • 13
  • 1
  • 4

1 Answers1

1

Parsley remote validator looks for HTTP response code not for the body content itself. In your example you're returning json data with probably always HTTP 200 status code.

Maybe look here http://php.net/manual/en/function.http-response-code.php or here Set Response Status Code to see how you could change the response status code.

Best

Community
  • 1
  • 1
guillaumepotier
  • 7,369
  • 8
  • 45
  • 72
  • Hi guillaumepotier, first of all, thanks for your incredible development and answering questions like mine. In my case I'm returning response codes, because I saw your answer in another thread saying that, but I really don't know what my php server code need to answer. I tried many many things. May be the right questions are: - What the server code need to answer in order to indicate to parsley the OK and KO conditions? HTTP Responses, php key-value pairs...? - In which format (json, plain text,...) need to be answered? Again, thank you very much for your interest and software. Esteve. – Esteve Jul 22 '14 at 09:01
  • Parsley 2.0 currently only look for HTTP Response status: 200 => OK everything else => NOK. If you're more comfortable with javascript than PHP, you can in parsley remote define your custom validator to look for something else, for example the json response code, like shown here: http://parsleyjs.org/doc/index.html#extras Hope that helps – guillaumepotier Jul 22 '14 at 12:46
  • Thank very much, it worked, when I changed echo json_encode(200) for echo http_response_code(200) and echo json_encode(404) for echo http_response_code(404). All the best and again thank you for your support – Esteve Jul 22 '14 at 16:16