Skip to Content
Menu
This question has been flagged
4 Replies
30890 Views

from openerp import models, fields, apifrom openerp.exceptions import ValidationError class

HelloWorld(models.Model):

_name = 'helloworld.test' name = fields.Char("Name", required=True, size=20)

value = fields.Integer("Value", required=True)

@api.onchange('value')

def _onchange_value(self): for record in self:

if record.value < 20:

raise ValidationError("Your record is too small: %s" % record.value)


in field Value i need that minimum integer would be 20..  but i still can save with any value. what is wrong with this?

Avatar
Discard
Best Answer

hello Andrew,

try this

@api.constrains('value')   

def _onchange_value(self):
        if (len(self.value) < 20):
            raise exceptions.ValidationError("Your record is too small: %s" % self.value)

I hope this is  help you

Thank you


Avatar
Discard
Best Answer

@api.constrains('value')

.....

....
raise ValidationError(_("Your record is too small: %s") % record.value)

try this one. It's correct form

Avatar
Discard
Best Answer

Try this,

from openerp import models, fields, api

from openerp.exceptions import ValidationError

class HelloWorld(models.Model):

     _name = 'helloworld.test'

    name = fields.Char("Name", required=True, size=20)

    value = fields.Integer("Value", required=True)


    @api.onchange('value')

    def _onchange_value(self)

        if self.value < 20:

            raise ValidationError("Your record is too small: %s" % self.value)

@api.constrains('value')

def _check_value(self)

        if self.value < 20:

            raise ValidationError("Your record is too small: %s" % self.value)

Avatar
Discard
Author Best Answer

There was a typo... problem solved

Avatar
Discard
Related Posts Replies Views Activity
5
Mar 20
11627
3
Jan 18
2506
1
Aug 19
4499
1
Nov 24
94
1
Jun 24
171