susanahernandezd
11/9/2016 - 11:05 AM

Validations with bootstrap with submit form on "submitSuccess" and change validations depending on value

Validations with bootstrap with submit form on "submitSuccess" and change validations depending on value

//set vars
var formulary = $('#personalDataForm');
var dni = $('#nationality-id').val();

//do validation
var formularyValidations = $(formulary).find("input,select,textarea").not("[type=submit]"  ).jqBootstrapValidation({
	preventSubmit: true,
	autoAdd: {helpBlocks: false},
	bindEvents: ['blur','change'],

	submitSuccess: function($form, event) {
		event.preventDefault();
		console.log("OK validations");

		//do stuff
		var button = $($form).find('[type=submit]');

		//disable button
		button.prop('disabled', true);

		
		var resultDni = isSpanishDni(dni);

		if(resultDni){
			console.log('there is no errors');
			//destroy before do the submit
			formularyValidations.jqBootstrapValidation("destroy");
			//do submit
			$form.submit();
		
		}else{
			console.log('there is DNI errors');

			var $inputs = that.dni_input;
			$inputs.jqBootstrapValidation(); // set up; ideally only called on each field once

			// triggers the field(s) to show the error-state if something is wrong
			$inputs.trigger("change.validation", {submitting: true});

			//enable button
			that.form_btn.prop('disabled', false);
		}

	},

        filter: function () {
          return ($(this).is(":visible") || $(this).hasClass("force-validation"));
      }
});