How to retrieve remote text from PHP in jquery.validate

2

Friends, I would like to know how to remotely retrieve the text of messages from jquery.validate in PHP.

$('#ajax_form_inclusao').validate({
    rules : {
        numeroContrato : {
            required : true
        },
        dataInicial : {
            required : true,
            dateTimeBR : true
        },
        dataFinal : {
            required : true
        },
        valorPF : {
            required : true
        },
        qtdPFContratado : {
            required : true
        },
        status : {
            required : true
        }
    },
    messages : {
        numeroContrato : {
            required : errorMessages.E2
        },
        dataInicial : {
            required : 'Preencha o campo Data Inicial'
        },
        dataFinal : {
            required : 'Preencha o campo Data Final'
        },
        valorPF : {
            required : 'Preencha o campo ValorPF'
        },
        qtdPFContratado : {
            required : 'Preencha o campo Quantidade de PF Contratado'
        },
        status : {
            required : 'Selecione uma opção para o campo Status'
        }
    },
    submitHandler : function(form) {

I would just like to put all the system messages in a single central file and retrieve the message text in the scripts.

    
asked by anonymous 06.05.2014 / 16:56

1 answer

1

You do not need to perform server-side validations. For simplicity, there is no need to have this on the server side because it does not work with the presentation layer . If you do, you will enter into a conceptual hiatus.

The natural mechanism of jQuery Validation, in the first place, does not - natively - support this type of operation. In short, you will not be able to pass from the client to the server such an error message - thank goodness, because that does not make any sense.

What you could do is: through submitHandler of jQuery Validation, send - with JSON - an error number to there on the server, fetch a message in the database corresponding to the requested number and then return to the view . If you do, it will have the desired effect: a centralized repository of error messages.

Is it good to centralize these messages? Theoretically yes . It is necessary? No .

Double-handed validation is required yes, but invoking an error in the format of messages in the direction of the server is not convenient. Ah, but what if the client disables JavaScript? It's like I said, you just should not invoke messages, but continue validating so that requested action is not processed.

But Guilherme, I still do not understand why I can not pass the client message to the server.

The message is to the client. In this specific case of validation, it is born in the client and stays there. The server-side does not have to deal with the presentation part - only and only it should be a catalyst that propagates route information, but it is a mathematician - it does not know how to paint a painting or writing a book; only deals with numbers, the bureaucratic part. Like I said, this is conceptual murder.

I think I understood. But what if I still want to do it?

jQuery Validation will not have a tangible solution natively.

Previously, when I wanted to do the same thing as you, I would create a database to store multiple messages to their ids (ids) and when I wanted to retrieve them [error messages] , I would make a request - asynchronous, with AJAX - to look in my "repository" and as soon as it was found, indicated that my action would pass on the information obtained to view again.

Anyway, the above process I no longer find much more valid today. Yours is to go from customer to serivdor is, in my opinion, even worse. So, avoid.

    
06.05.2014 / 18:46