Real-time form validation with php?

Asked

Viewed 490 times

0

Assuming I have a form with some fields, and one of those fields is a CPF field.

<input name="cpf" 
       data-input-mask="cpf" 
       data-input-field="cpf"  
       data-input-validator="cpf" 
       value="" 
       class="form-control input-field" 
       type="text">

Currently, through my framework, I identify the 'input-mask' at the jQuery event keyup and focus, add the mask, and also, to each modification in the input data I make a request to a Caller in php, which makes the validation according to 'input-validator', the response to that validation is a JSON containing 'true' or 'false' and an error message code, something like '0546', message that is stored in a JSON file.

This method worked well for a while, however, things started to get longer and forms more complex, causing the performance to fall very much due to the amount of requests.

With some research I found things like:

Websocket.

jQuery Validator plugin (personally found horrible).

Validate with JAVA (?) (It didn’t make much sense to me JAVA + PHP, but was nominated by a programmer friend)

Validate with pure javascript (It’s the easiest way, but it turns out I can’t reuse my PHP methods).

In the mode I use, the PHP validation methods of the ajax requests of each input are the same used after the form submission.

Which is the best option (without leaving jQuery + PHP) to validate fields, among other features such as updating graphics in real time ?

If, timed (or fired) ajax requests, how to improve their performance ?

If websocket, I know absolutely nothing about, which PHP framework/package/plugin provides me with good documentation ? (I tried to use cboden/Ratchet but without success).

If otherwise, which one would be recommended for an application that will rarely have more than 20 simultaneous accesses and has a dedicated server?

PS: The problem with the many current ajax requests is more client-side than server-side.

  • 4

    Probably your friend said JAVA(script), there is a difference between java and javascript :)

  • 1

    I don’t see pq validate by ajax or websocket. Validate in javascript and check in PHP again for security.

  • @Marceloboni I know it’s not the same thing, he said JAVA even, but probably this was a misunderstanding of information.

  • @Karlzillner This is what I’m trying to understand which one looks best, I consider the ajax requests for easier, because in PHP I have everything ready and I didn’t want to have to redo all the validators in another language.

  • 1

    The ideal in your case, since I believe your server runs php, it is necessary to do a superficial validation in javascript, and after the data pass pro backend continue doing the validation with php

  • 2

    Websocket for validation is a bad idea. It’s best to do it yourself. Since you will have to do something new, you could not abstract the rules so that they can be used by both a php code and a js?

  • Generally, everything that is said "in real time" leads to understand the use of ajax Asynchronous Javascript and XML, that is, the validation is done, without it being necessary to give a refresh on the page, maybe there started the confusion

  • @bfavaretto Until it would have like, more there are some complex validations, as validation of bar code, valid Cpf, cnpj, some with respect to the comparison of dates and compensation of values, I do not know if with my knowledge in JS I could have the same result that I have with PHP, But from the looks of it, I think switching to JS will be the way.

  • @Marceloboni will probably do just that, light validation with js, and with php the heaviest and security validations.

  • You probably already have this written on the internet in javascript. You don’t need to rewrite everything.

Show 5 more comments
No answers

Browser other questions tagged

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