Question

I'm creating onepage customer account and I need to validate dynamically created additional address forms, all on same page.

I understand that in Magento you should use new VarienForm to validate your forms. to do that you just call following line

var dataForm = new VarienForm('form-id', true);

But what if forms are created dynamically like additional customer addresses and all forms are on same page and you need to check whether the form validated or not

if(dataForm.validator.validate())

Isn't this var dataFrorm get overwriten with next generated form? So now if you try to check if first form got validated, it actually checks last? Yes I could generate form id dynamically, but then I would need to do same thing with JS variable name? That does not sound right, or is it? Is there better approach to achieve the same goal?

Was it helpful?

Solution

If you dynamically create forms, you sure do this via a javascript(ajax) request.

So you have an place where you can add javascript after the form was created.

For the case you have a limited count of steps, every step gets its own form ID, because every form has different content/fields.

But: if you always use the same form and only changes the content, you dont need to create the object new, as it is bound to the dom element, not the ID. In this case you even should not recreate it.

But2: if you always replace the whole form, you can use the same ID, as the old form is away anyways. So you can always create it new with the same form id

Very important rule: never have more then one element with the same ID on the page, it will make things broken.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top