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 validate float field

By
More9ike
on 7/16/13, 1:24 PM 3,383 views

Please I have two fields with float datatype, how do I validate these two fields to prompt a user to enter a value different from default value 0.00?

Thanks

0

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 11/5/15, 2:10 AM

Hi,

if you are working with new api, you may try this:

@api.one
@api.constrains('field1', 'field2')
def _check_values(self):
if self.field1 == 0.0 or self.field2 == 0.0:
raise Warning(_('Values should not be zero.'))
0
Ajeng Shilvie
On 11/5/15, 12:50 AM

If you are using class models.Model not osv.osv, try this :

@api.onchange('your_float_field1','your_float_field2')
def change_value(self):
if self.your_float_field1 == 0 or self.your_float_field2 == 0 :
return {'warning':{'title':'Error!','message':'your message'}}


0

Grover Menacho

--Grover Menacho--
2523
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Systems Engineer + MBA

Grover Menacho
On 7/16/13, 1:46 PM

You have to write a create function on that object. There are loads of examples on the web. You just have to write a create function then check if those values are different from zero. If they are you call the create of the super. If they are zero you can create a osv error.

Please can you give me an example?

More9ike
on 7/17/13, 7:08 AM

def create(self, cr, uid, vals, context=None):

    if(vals.get('field1'))==0:
    raise osv.except_osv(_('Warning'), _('Value must be different from zero'))
else:
    return super(module_name, self).create(cr, uid, vals, context)
GH Mediacloud, Grover Menacho
on 7/17/13, 11:29 AM

It's a generic way to do it. But basically it's what you need.

GH Mediacloud, Grover Menacho
on 7/17/13, 11:30 AM

Thank you very much!

More9ike
on 7/17/13, 12:04 PM

If it helped you please mark the answer as correct to help others.

GH Mediacloud, Grover Menacho
on 7/17/13, 12:11 PM

Please where do I mark it as correct answer?

More9ike
on 7/17/13, 12:20 PM

Below your question there is a section that now says "1 answer". There is a tick circle below a number and that number is to vote for the answer. You have to do it on all your questions answered because this helps to many people to find quickly answers. If the answer is correct you have to check it, if other answer helped you, you have to vote for it.

GH Mediacloud, Grover Menacho
on 7/17/13, 12:56 PM

Hi Grover Menacho, I also want to know how to validate float data type time field. For example in the validation code float(time) it accepts the format only xx:yy and not accept other format time for example 100:00 pl given me sample code based on this. Thanks

Prakash
on 1/13/14, 4:15 AM

I don't want to do it in create function. That validates when we save the entire form and that is a custom validation done by us. We need a way to have automatic validation just like the validation for the mandatory fields (Char, date) when they are empty. But integer and float have default value of 0. Because of that, the validation of mandatory field is not applied. Its not fair

Vivekrajan
on 11/15/14, 4:50 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

0 follower(s)

Stats

Asked: 7/16/13, 1:24 PM
Seen: 3383 times
Last updated: 11/5/15, 2:10 AM