Form validation Javascript library to Cakephp 2.5

Hi

I need to create form validations into cakephp 2.5, would like to research a little about wich would be the javascript library of choice.

To validate forms in cakephp 2.5 ?

VueJs, Jquery, jqueryvalidation.org.

I prefer not to use a javascript library for validation, but instead simply use Cake’s validation to populate my form with the errors after submission (via ajax). It’s safer and requires much less code, plus there’s no danger of having server/client inconsistencies.

The only problem is that you can’t easily validate single fields, since the validation happens when the whole form is submitted.

But whether that is necessary depends on the individual project. In any case, I just want you to be aware that this possibility exists; Other than that, you should be able to use pretty much any validation library with CakePHP. Your choice should depend more on the specific project requirements rather than compatibility with cake (which shouldn’t be an issue).

Hi,

I already have done all Cakephp Validation, for safety. I need the Javascript validation to bring some processing and availability relief, to the webserver, as the validation processing can be done into the client side.

The requirements are simple vehicle registration car plate numbers, and document numbers that will follow some specific format.

I do not know much CakePHP programers that use javascript to validate forms, would like to know more about javascript validation good practices into CakePHP 2.5, as i trying to implement into Jquery or Vuejs.

I would like to know if there are any client-side best practices for cakephp validation, too :smiley:

But there probably aren’t… You can just use something like https://validatejs.org/ and it should be fine.

I’m not a big fan of it because you need to setup the validation rules twice that way, and it can be a lot of work.
Plus, usually one only needs a couple of validation rules, so a huge library is often overkill.

I suspect that there are no ideal solutions, but many options: For one, you could always use regular html5 validation with “required”. You can even validate against patterns, so it’s relatively powerful, but obviously isn’t 100% consistent across all browsers and not supported in old browsers, if that’s relevant.

You could also always write your own js validation rules or select a few small, specialized scripts to do the job.

In the end, it’s simply a matter of choice, I think.

1 Like

Here goes a simple Jquery code

`$( document ).ready(function() {
$(’#Form’).submit(function(e){
var Field1 = $(’#Field1’).val();
var Field2 = $(’#Field2’).val();
var Field3 = $(’#Field3’).val();

  if (	Field1 == '' &&
  		Field2 == '' &&
  		Field3 == ''
  	)
  {
	console.log( 'if' );
    confirm('Provide at leat one field');
        e.preventDefault(e);
  } else {
	console.log( 'else' );
  }
});

});`

As a minimalist starting point.

If you’re using js validation, it probably makes more sense to validate on blur or even keyup events, so that users can see when their input values are faulty right away.

For instance:

$(document).on('blur keyup', 'input, textarea, select', function() {
    validateField( $(this) );
});

The validateField() function could then validate the particular field by its name and/or type.
It makes sense to use a function since you can reuse it for other events, for example, you can add a foreach which walks through every form element when submitting the form.

How can i call a jquery modal function inside the If ?

Today i do use an alert, this fiddle has a simple modal : http://jsfiddle.net/d7r8R/98/

I know i must call leanModal() but just do noto know how , must i append to an existing div ?

Well, the div with the modal content has to exist somewhere; Whether you append it to the body with jQuery or just write it into the html is up to you. Then you have to call it something like $(’#myDiv’).leanModal().

However, you would probably find more help in a JS/jQuery forum; CakePHP has very little to do with JS…

Thank you for all the support!

Glad if I could help!

Sure ! i do apreciate your help !! I end with Jquery, in the future will try VueJS wich promisses to be more concise and simple.