odooodoo-8odoo-website

Validations not working in ODOO website


I have created a template and a controller for it in ODOO v8. Following is the template:

<template id="myTemplate">
    <t t-call="myTemplateHeader"/>
    <div class="myClass">
        <form action="/myControllerAction" name="myTemplateForm">
            <input type="text" id="name" name="name"/>
            <input type="text" id="lname" name="lname"/>
            <input type="text" id="email" name="email"/>
            <input type="submit" value="Submit"/>
        </form>
    </div>
</template>

And I have written a controller for the action /myControllerAction.

@http.route(['/myControllerAction'], type='http', auth="public", website=True)
def index(self, **post):
    data = {}
    # some action here
    # to submit and fetch values
    request.website.render("my_module.mySecondTemplate", data)

I have added validations on the fields in the form so that one cannot submit the form without entering the values in all the text fields given. The validations in JS works, it shows an alert message when the text fields are blank(one alert for each text field). But, after clicking the OK for the alert message of email field, it submits the form even when the field is empty. I have checked the issue and found that the problem exists only if I provide

<input type="submit" value="Submit"/>

and it will be solved if I am using

<input type="button" value="Submit"/>

But I have to make some calculations in the controller and need to retrieve some data from the database to show on the next page. For this, type="button" cannot be used as it just submit the form and redirect to next page without making a call to the controller function. type="submit" will make the call to the controller, but validations are not working as described earlier. Also submitting the form using onclick event of the button in javascript won't call the controller. I want validations on the form and then call the controller(on submit). Is there any way I could implement this in ODOO v8?


Solution

  • For making fields mandatory in ODOO templates, the attribute required="required" can be used on input fields.

    <input type="text" id="name" name="name" required="required"/>