Sunday, October 20, 2019

phone field in odoo


 
we can make phone field just as char field but we have to control (validate) the user’s 
input for the phone number just as below


Create two fields for the phone (the key field and the number field) just like

("phone_num = fields.Char(string="Phon
("phone_key = fields.Char(string="Key")

then create their view to show them just like in the image

<"group colspan="4" col="4>
    <";table style="width:40%>
         <";tr style="width:100%>
             < td style="fonte-size:13px;width:25;>
                 <b>
                      Phone
                 </b>
             </td>
             <td style="fonte-size:13px;width:15%;">
                 <field name="phone_key" nolabel="1" placeholder="+249"/>
             </td>
             <td style="fonte-size:13px;width:60%;">
                  <field name="phone_num" nolabel="1" placeholder="912312312"/>
             </td>
         </tr>
     </table>
</group>

and finally we have to create the validation function for the phone fields , just like

@api.one
    @api.constrains('phone_num','phone_key')
    def validate_phone(self):
        """
        fuction to validate the input phone number for the user
        """
        if len(str(self.phone_key)) != 4:
            raise ValidationError(_("Wrong Phone Key"))
        if len(str(self.phone_num)) != 9:
            raise ValidationError(_("Wrong Phone Number"))

        for index in range(0,len(str(self.phone_key))):
            if index == 0:
                if str(self.phone_key)[index] != '+':
                    raise ValidationError(_("Wrong Phone Key (It Must Begin With + )"))
            if index != 0:
                if str(self.phone_key)[index] not in ['0','1','2','3','4','5','6','7','8','9']:
                    raise ValidationError(_("Wrong Phone Key (It's Conatin Char )"))
        for index in range(0,len(str(self.phone_num))):
            if str(self.phone_num)[index] not in ['0','1','2','3','4','5','6','7','8','9']:
                raise ValidationError(_("Wrong Phone Number (It's Conatin Char )"))
# before creating this function you have to import ValidationError in you model just as

from odoo.exceptions import ValidationError

No comments:

Post a Comment

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...