Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to write a function to validate date of birth?

By
Jothimani
on 5/6/14, 9:23 AM 2,578 views

Hello guys, please help me to do validation in my custom module.

i want to develop a HR module with Employee name, date of birth and age. in my module i want validate age should above 18 years and do not exceed 120 years.

please let me know, how to improve my code here is my code.

in my .py file

from osv import fields, osv
from datetime import datetime
from time import strptime
from dateutil import parser

class employee_employee(osv.osv):

    _name='employee.employee'

#   Age calculation

    def onchange_age(self, cr, uid, ids, DOB, context=None):
        current_date=datetime.now()
        current_year=current_date.year
        birth_date = parser.parse(DOB)

        if birth_date.year > current_year-120 and birth_date.year<current_year-18:
            current_age=current_year-birth_date.year
        else:
            raise osv.except_osv(_('Invalid DOB'), _('Please enter a valid DATE OF BIRTH'))

        val = {
        'age':current_age
        }
        return {'value': val}

    _columns ={ 
          'name':fields.char('Emp Name',size=64, required=True),
          'DOB':fields.date('DOB'),
                  'age':fields.char('Age'),
        }

employee_employee()

in my .xml

   <record id="employee_form_view" model="ir.ui.view">
        <field name="name">employee.employee.form</field>
        <field name="model">employee.employee</field>
        <field name="type">form</field>
        <field name="arch" type="xml">

           <form string="employee">
            <field name="name"/>
                    <field name="DOB" />
                    <field name="age"/>
            </form>
        </field>
    </record>

Thanks in advance....

1

Nehal

--Nehal--
2090
| 5 2 8
Ahmadabad, India
--Nehal--
Nehal
On 5/6/14, 9:44 AM

Hi,

Replace <field name="DOB"/> with <field name="DOB" on_change="onchange_age(DOB)"/> to call onchange method.

You can apply constraint for the specific field which will check the age based on birth date. And it will not allow you to save record. For an example, you may have a look @ here.

i know, on_change event is good, but it doesn't make professional project. bcz when we enter a dob before 120 years like (01/01/1891), it shows validation error, after i click ok the dob field still same, and it could be saved without a restriction, that is my problem.

Jothimani
on 5/6/14, 11:01 AM

i know, on_change event is good, but it doesn't make professional project. bcz when we enter a dob before 120 years like (01/01/1891), it shows validation error, after i click ok the dob field still same, and it could be saved without a restriction, that is my problem.

Jothimani
on 5/6/14, 11:01 AM

I have edited my answer, just have a look.

Nehal
on 5/7/14, 2:08 AM

Very thanks Nehal

Jothimani
on 5/7/14, 4:43 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/6/14, 9:23 AM
Seen: 2578 times
Last updated: 8/6/15, 1:06 AM