How to check validation return status when form is submitted

trb

Member
Hi,

I have a large form with several repeat groups.

I collect product information on the form, and add the products to a virtuemart shopping cart upon submitting the form. I use form_xx.js to collect the products which were submitted on the form and add them to the Virtuemart cart.

My problem is that if the form fails a validation upon submit, for example if a user forgets to enter their name in the name field element on the form, the form_xx.js is still called and the products are added to the shopping cart and the user is prompted to correct the missing info on the form. When they correct the missing info and once again submit the form, there are now twice as many products added to the shopping cart.

So my questions is how do I detect, after the form is submitted, if the validation has passed or failed, so that I can check the validation status in form_xx,js and not add products to the cart if the validation has failed.

I cannot us ajax validation on the form, so need the php validation checks.

Thanks...
 
You can do this on several ways:
1. Set ajax validation in Form->Form processsing
2. In your form_xx.js you can check fields using js code similar your validation plugin befor you send data to shopping cart
3. Send your data to shopping cart using php plugin onAfterProsess
 
I was a little to fast In my previus post.

So you can set when js event fires or with other words when fires functuon in your form_xx.js file. In your case just fire your event on fabrik.form.submit.end (form)

Read HERE
 
I'm still struggling with being able to detect a validation error when the form is submitted so that I can only run my code, which adds products from the form to the shopping cart, if there are no validation errors.

I've tried Deant's approach above but it doesn't seem to work for me???

Here is my workflow:
- Form is submitted with a validation error such as blank text element which has "notempty" set in the element validation setting. Must validate is set to "no"
- Form is using the forms Save button to submit
- In form_16.js, I have the below code, just as a test to determine if I can detect the validation error:
function($){
requirejs(['fab/fabrik'], function () {
Fabrik.addEvent('fabrik.form.submit.end', function(form) {
alert('form submission done..');
})
});

})(window.jQuery);

When I submit the form (which has a validation error) I see the alert window with my message "form submission done..." and upon closing the alert window, I then see the validation run on the form and it detects the validation failure.

So how can I detect the validation and prevent my custom code from running until all validations errors are cleared on the form?

I was expecting that the fabrik.form.submit.end would not fire until after the validation error was resolved but obviously I do not understand the order in which the form is processing on submit.

Any/all suggestions are welcome..... thanks.
 
Set element validation Must validate to "Yes". The form not be submited until the error is corrected.

In .js file you can check if input field have value. If is empty then don't run a code which adds products to the shopping cart.
 
Hi Deant,

Thanks.

I am using the validation feature within each element and have them set to "notempty". I did not realize that your suggested solution required to create validation checks for each field in the form_xx.js. Also ajax validation is not a good solution in my situation as I mentioned in the original post.

I have solved my issue another way, by using a php form plugin to detect any errors on the form and clearing the cart if any error is present. It's not the best solution but it works.
 
How do you 'add to cart', why do you add to cart at all if there are validation errors.
A form doesn't submit with errors. So add to cart after form submit via a php plugin.

Gesendet von meinem SM-G930F mit Tapatalk
 
Hi Troester,

Thanks...

I'm adding to the cart from form_xx.js so when the form is submitted it is running that code even if the form fails validation so I was looking for a way to listen for some kind of "form validation passed" event (???) and run my add to cart code from within that function. I see fabrik has 'fabrik.form.element.validation.complete' but not sure if there is anything that indicates if the validation passed/failed (???)
 
Is there anything that PHP-form plugin can't handle so you have to use form_xx.js? I'm not aware of "validation passed" or "validation failed" js events.
 
Hi juuser,

I'm doing a lot in the form_xx,js... it probably could be done using php form plugins also but probably more maintainable in a single file. I am detecting form errors on submit using a php form plugin and clearing the cart if there are any errors which works well. Thanks...
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top