logo

Basic Form Handling with Owl.js + Odoo 17

VH CHAUDHARY

Explore Odoo servicesBook a discovery call

Basic Form Handling with Owl.js + Odoo 17

Owl.js is a modern JavaScript framework that shines in creating dynamic web interfaces within Odoo applications. If you are working within the Odoo ecosystem, Owl.js is a powerful tool for handling user input through forms. Let's dive into the basics of how it works.

Key Concepts and Setup

  1. Owl Components: In Owl.js, you organize your web interfaces into reusable components. Each component is a self-contained unit with its own logic and rendering behavior. A form is a perfect example of a component.
  2. State: The concept of 'state' is central to Owl.js. The state represents the data within your component at a given moment in time. User input into your form fields directly modifies this state.
  3. Reactivity: Owl.js components are reactive. When the state changes, Owl.js seamlessly re-renders the relevant parts of your form to reflect the updates without manual DOM manipulation.

Building a Simple Form

Let's illustrate with a basic contact form example:

class ContactForm extends owl.Component {
    state = {
        name: '',
        email: '',
        message: ''
    };

    onNameChange(event) {
        this.state.name = event.target.value;
    }

    onEmailChange(event) {
        this.state.email = event.target.value;
    }

    onMessageChange(event) {
        this.state.message = event.target.value;
    }

    async submitForm() {
        // Handle Form Submission (e.g., send data to server)
        console.log("Form Data:", this.state);
    }
}

Explanation

  • Component Structure: We define a class ContactForm that extends the base owl.Component.
  • Initial State: Within the component, we initialize state with fields for name, email, and message.
  • Event Handlers: Functions like onNameChange, onEmailChange and onMessageChange are event handlers. Owl.js automatically binds them to input elements in your template. Whenever the user types in an input field, these functions update the corresponding state values.
  • Form Submission: The submitForm function would handle the logic involved in submitting the form data, potentially sending it to your Odoo backend.

Template Structure

Now, let's create a basic XML template to pair with this component:

<?xml version="1.0" encoding="UTF-8"?>
<templates>
    <t t-name="ContactForm">
        <form t-on-submit.prevent="submitForm">
            <input type="text" class="form-control" placeholder="Name" t-on-input="onNameChange"/>
            <input type="email" class="form-control" placeholder="Email" t-on-input="onEmailChange"/>
            <textarea class="form-control" placeholder="Message" t-on-input="onMessageChange"/>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </t>
</templates>
  • t-on Directives: Notice the t-on-submit.prevent and t-on-input directives. These bind your form elements to the event handlers we defined in the JavaScript component.

Reference:

https://github.com/odoo/owl/blob/master/doc/reference/input_bindings.md

About our Odoo practice

We implement, customize, and support Odoo for operations-heavy teams across retail, manufacturing, logistics, and services. Phased delivery, clear ownership, and support that continues after go-live.
View Odoo services