Parse a javascript serverside

Is there a way to parse javascript code server side? Like a way to pass to a javascript engine the code to be eval’ed within the server and get a result back? The reason I’m wondering is it would be nice to write my validation rules for fields once and in once language. Since javascript can be disabled on the clientside it cannot be relied on for validation. However, if I have a serverside parser I can pass the code to be validated over to the js parser, have it return the results of the validation and proceed based on this.

Advantage -> the coding should be faster to write since validations only need to be written once, and DRY methodology is preserved by having those validations expressed only once. Disadvantage -> This will be likely be slower than handling the validation purely in PHP, but unless the performance hit is sizable I’m willing to live with it on the application I’m planning on applying this to.

Thoughts?

(The reason this post is in PHP and not in Javascript is I intend to call the js parser from PHP and the majority of the code will be PHP. That is, I want to call a javascript parser from a PHP environment).

There is a node.js

I am not sure how you would call it from php, but probably can pass it using exec() or passthru()

Off Topic:

Assuming that the validation has to be done on the server in all cases (because, as you said, you can never tell if JS is enabled or not), why not have all the validation done in PHP, instead? Then when you can validate on the client, AJAX it to the appropriate page and wait for the response?

Off Topic:

I hate it when I ask a question and, instead of simply answering, some jerk says, “Why don’t just do {whatever}…” Because. That’s why. :stuck_out_tongue:

This might be useful:

http://devzone.zend.com/1480/using-javascript-in-php-with-pecl-and-spidermonkey/

There’s also the V8JS PECL extension.

So what you want to do is make something really complicated?

I am probably stupid, but why not use php validation and AJAX requests to validate the form on the fly? You can make an AJAX request to the server on every keyUp or Blur event and even if javascript is disabled the server will still validate the form in the same matter/using the same code.

If Javascript is disable then you dont have onKeyUp and onBlur events. Dah

Doesn’t matter the form will still be validated by the server.
if your form action is “validate.php”
and your ajax requests goes to “validate.php?field=the_field_that_triggered_the_blur_or_keyup&value=the_value_of_the_field&return=json”

Or something.

and on validate.php something like:

<?php
if(isset($_GET['field'])){
    $value = $_GET['value']
    // validate the particular field and put errors in an array called $errors for example
} else {
    // validate the entire form from the $_POST
}

if(isset($_GET['return']) && $_GET['return'] == 'json'){
    return json_encode($errors);
    exit;
}

That will chew through a considerable amount of bandwidth - more than doubling the data transmission per form. It would also be at the mercy of the internet for its responsiveness.

Aha! I think I understand what the problem is now :slight_smile:

I think i have seen something like this done using a combination of Zend Framework and jQuery validate plugin. It basically functioned like this:
<input name=“email” class=“required email” />
That would be generated using Zend Form in which the validators would be set and then jQuery validate would validate those input fields based on the values in the class attribute.

I understand it’s not really what you want though. It lacks custom validation rules and is kinda messy because it uses the class attribute.

This is a nice challange. I am going to try and write a parser like what you suggest one of these days. It will be a nice addition to the “framework” I use.