Skip to Content
Menu
This question has been flagged
1 Reply
3867 Views

Can anyone explain how are inputs on the template website_sale.address set as required? I need to make zip required but cannot figure out what method is used. I don't see anything in template controller or I haven't found anything in js files.


<div t-attf-class="form-group #{error.get('phone') and 'o_has_error' or ''} col-lg-6" id="div_phone">

    <label class="col-form-label" for="phone">Phone</label>

    <input type="tel" name="phone" t-attf-class="form-control #{error.get('phone') and 'is-invalid' or ''}" t-att-value="'phone' in checkout and checkout['phone']" />

</div>


<t t-set='zip_city' t-value='country and [x for x in country.get_address_fields() if x in ["zip", "city"]] or ["city", "zip"]'/>

<t t-if="'zip' in zip_city and zip_city.index('zip') &lt; zip_city.index('city')">

    <div t-attf-class="form-group #{error.get('zip') and 'o_has_error' or ''} col-md-4 div_zip">

        <label class="col-form-label label-optional" for="zip">Zip Code</label>

        <input type="text" name="zip" t-attf-class="form-control #{error.get('zip') and 'is-invalid' or ''}" t-att-value="'zip' in checkout and checkout['zip']" />

    </div>

</t>

Phone input is required, zip is not. How is it done?

Avatar
Discard
Best Answer

Hi Samo,

You can set required by inherit website_sale controller -> main.py

Here we got 2 function:
_get_mandatory_billing_fields
_get_mandatory_shipping_fields

Just add your field name that you wish to make it required


Regards,
Ivan

Avatar
Discard
Author

thanks!

Author

I've extended the controller but it doesn't work. It still isn't mandatory!

What Odoo version did you use ?

Author

odoo 12 CE. I've even extended the template input field name="field_required" with zip

You've restarted your server right ?

I tried & it works from my side, you may need to try & print the variable in checkout_form_validate function

Author

yeah, I've restarted the server. I've extended the controller:

from odoo.addons.website_sale.controllers.main import WebsiteSale

class TablaWebsiteSale(WebsiteSale):

def _get_mandatory_billing_fields(self):

return ["name", "email", "street", "city", "zip", "country_id"]

def _get_mandatory_shipping_fields(self):

return ["name", "street", "city", "zip", "country_id"]

But when I log the output of those methods

I get only: odoo.addons.website_sale.controllers.main: mandatory shipping: ['name', 'street', 'city', 'country_id'], mandatory billing: ['name', 'email', 'street', 'city', 'country_id']

I think you also need to inherit "checkout" and "checkout_form_validate" function, because these 2 functions call for it

Author

Ok now it works, thanks.... but weird I never needed to inherit other methods.

Glad it works!

Related Posts Replies Views Activity
5
Oct 23
14108
0
May 23
903
0
Feb 22
1027
0
Feb 21
1829
0
Jun 20
1623