0

I am trying to construct a JS on the fly for a remote validator function using the validation plugin. but for some reason, it's not converting the JS as an object and is treating it as a string and embedding double quotes.

Ex:

The PHP code I have is:

$remoteUrl = '/test/checkusername';
$remoteValidatorJs = "{url: '". $remoteUrl . "',
                      type: 'post',
                      async:false,
                      dataType: 'html',
                      beforeSend: function(){
                         alert('Validating Form Field');
                       },
                       complete: function(){
                         alert('Completed Validation of Form Field');
                       },
                      dataFilter: function(html) {
                          return html;
                      }
                      }";
$validation[ 'rules' ][ 'Name' ][ 'remote' ] = $remoteValidatorJs;

How do I frame or convert the JS in $remoteValidatorJs variable so, it eventually looks like the content in the following "remote" section, when the array is printed:

$("#testForm").validate( {
    "rules":{
        "Name":{
            "remote":{
                url: '/test/checkusername',
                type: 'post',
                async:false,
                dataType: 'html',
                beforeSend: function(){
                    alert('Validating Form Field');                     
                },complete: function(){
                    alert('Completed Validation of Form Field');                      
                },
                dataFilter: function(html) {
                    return html;                     
                }
            }
        }
    }
} );

Thanks,

3
  • Why do you have quotes at the beginning of the variable? $remoteValidatorJs = " - this will make the entire variable a string Commented Aug 15, 2012 at 18:41
  • That is PHP syntax to store a string in a variable Commented Aug 15, 2012 at 18:42
  • There's no such thing as a 'json object' (other than the object which handles translating between string<->native). There's json strings, which can be converted back to native objects/arrays. Commented Aug 15, 2012 at 18:45

1 Answer 1

2

JSON is a subset of javascript, your example isn't valid JSON as it's a javascript string.

The only way to evaluate it would be to use Function or eval

But without knowing what you're trying to solve I doubt evaling a string is the solution.

Using a string containing a javascript object literal with functions, the following would work. PS I haven't used your whole string :)

var remoteUrl = "http://something.com";
var evalString =
  [
    '{url:"' + remoteUrl + '",',
    'type:"post",',
    'async:false}'
  ].join('')
evalString #// => "{url:"http://something.com",type:"post",async:false}"
var x= new Function("return " + evalString + ";")()
#// => Object
  async: false
  type: "post"
  url: "http://something.com"
Sign up to request clarification or add additional context in comments.

1 Comment

What would be a valid way to construct the object then?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.