gitana / alpaca

Alpaca provides the easiest way to generate interactive HTML5 forms for web and mobile applications. It uses JSON Schema and simple Handlebars templates to generate great looking, dynamic user interfaces on top of Twitter Bootstrap, jQuery UI, jQuery Mobile and HTML5.
http://www.alpacajs.org
Other
1.29k stars 371 forks source link

Manually get validation state of Alpaca form #672

Closed aenany-atmosphereiot closed 5 years ago

aenany-atmosphereiot commented 6 years ago

Hello Alpaca Team,

Any idea how to get the current validation state of an alpaca form? I have a webpage with multiple alpaca forms and a single submit button. And I want to check that all forms are valid before the data is collected from all the forms and submitted.

$('#alpacaContainer1').alpaca('get').isValid() $('#alpacaContainer2').alpaca('get').isValid() $('#alpacaContainer3').alpaca('get').isValid()

The aforementioned calls seem to return true even if the inputs are invalid. Is there another function I should be calling to get the actual validation state?

Thanks!

BrianH85 commented 6 years ago

Have a look at the 'Form with Auto Focus on Invalid Field' example at: http://alpacajs.org/docs/api/forms.html

I think the problem is you need to first call 'refreshValidationState()' before checking 'isValid()'. I solved this by implementing a jQuery promis. Making sure to wait until the form validation was refreshed before checking the validation state:

$.when(this.refreshValidationState(true)).then(function () { if (form.isValid()) {

nickjgentile commented 5 years ago

Hey. If you add the "form" property to the "options" property in the JSON you use to initialize an instance of alpaca, you can interrogate that form property once initialized in order to run [yourAlpacaInstance].form.isFormValid(). It's odd that you have to manually add the "form" property in order to use this API method but I'll leave an example below.

formData.postRender = function(control) { alpacaInstance = control;

formValid = alpacaInstance.form ? alpacaInstance.form.isFormValid() : true;