Jquery Validate renaming complex (square brackets)

Asked

Viewed 37 times

0

I am validating some fields of an application form that was made in Ruby on Rails. In this application has some query endpoint, where it returns a json, if CNPJ exists it returns this data:

{
   "existe": true,
   "loja": {
   "id": 16,
   "nome": "Loja Sergio"
   }
}

And if it doesn’t exist it returns

{  
  "existe":false
}

The URL to query the CNPJ is as follows: http://localhost:3000/api/findLojaByCnpj? cnpj=42.364.513/0001-21 via get

Below the code snippet to try to validate the field using the library Jquery Validation, using the method remote from the library itself, which accesses the Url and returns true or false.

$(".validateCadastrarLoja").validate({
 rules: {
    'loja[cnpj]': {
        required: true,
        remote: {
            url: "/api/findLojaByCnpj",
            type: "GET",
            data: {
                cnpj: function() {
                    return $("#validationCNPJ").val()
                }
            },
            dataFilter: function(d) {
                response = JSON.parse(d);
                return !response.existe;
            }
        }
    },
 },

 messages: {
    'loja[cnpj]': {
        required: "Informe o CNPJ.",
        cnpj: 'CNPJ inválido',
        remote: 'CNPJ já cadastrado'
    },
 }
});

Only this way he is making a request using both the store[cnpj] and cnpj parameters.

I’d like you to give change the store[cnpj] to the cnpj, but without changing the call in Rules, I searched many sites in English and could not find anything

  • answering: if your field is loja[cnpj] and so the rules are enforced in that field, that is, this is the name of the field to validate, you can only do that if you rename the html field to cnpj ... I see no problem he pass two parameters.

  • You think it’s okay? I’ll run some tests here

  • No problem is not only a parameter more you can rescue the parameter ... is also an alternative.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.