跳至内容
菜单
此问题已终结
1 回复
4734 查看

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?

形象
丢弃
最佳答案

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

形象
丢弃
编写者

thanks!

编写者

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

What Odoo version did you use ?

编写者

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

编写者

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

编写者

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

Glad it works!

相关帖文 回复 查看 活动
5
10月 23
15913
0
5月 23
1614
0
2月 22
2207
0
2月 21
2666
0
6月 20
2445