Grails inList Constraint get values of inList map in error message - validation

I am using inList constraint to validate list of values. And I am not using directly command/domain class to show error message
ex:
name inList: ["Joe", "Fred", "Bob"]
if name is not from the list error message is shown as
Property [{0}] of class [{1}] with value [{2}] is not contained within
the list [{3}]
Instead i want to show a proper message having values as Property of name of class MyClass with value XYZ is not contained within the list Joe, Fred, Bob.
Whereas MyClass , XYZ and List values must be coming from cmd object that I use to validate.
Please help on how to show this message.

I got the solution for it.
I can get the rejected value from cmd.errors.getRejectedValue() and I can get list of values from cmd.constraints.name.inList

Related

How to pass the array in the postman

I have created the modal class in the DRF reference to ManytoMany. When I am trying to pass the array through the PostMan it's throwing an error as "Incorrect type. Expected pk value, received str." But when I am passing directly through Web in 'as_view()' It's successfully inserting in the database. My modal class is :
class PackageComponent(models.Model):
pkg_code = models.CharField(max_length=255)
comp_code = models.ManyToManyField(
'core.SensorDevice'
)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
So, When I am passing the array in comp code through PostMan in component code as [1,2]. I am getting the error. So my question is how can I pass the array? Any suggestions are most welcome. Thanks in advance
If you pass multiple values under the same key they will get interpreted as a list in Django. So in Postman you would use e.g. form-data as comp_code=1&comp_code=2&comp_code=3.
You can read the data then as request.data.getlist('comp_code') # [1, 2, 3].

Custom field called instead of the default field 'name' in m2o field in odoo

There is a field name partner_id in model mail.channel.partner. I want that the partner_id return "Joestar" instead of it's full name 'Joseph joestar'. This should be apply only on this model .
How can I modify the partner_id(m2o) so that it will return only the last string. The goal of this is to hide the full name of the users/partners that's why I need the last string only.
The goal is when we interact to our customer through our site, we don't want to reveal our full name instead, we want to display only the last name.
In the image below, The encircled data is the name I want to modify to last name instead of full name.
enter image description here
Hello Marychan,
Please Override name_get method that'll already define in res.partner model like this:
#api.multi
def name_get(self):
result = []
for rec in self:
result.append((rec.id, "%s" % (rec.name.strip().split()[-1])))
return result
That will give you only last word of name.
If you want to check the url for you model:-
from openerp import http
print http.request.env['ir.config_parameter'].get_param('web.base.url') # BASE URL
print http.request.httprequest
print http.request.httprequest.full_path
Check before stripping and spliting

Get multiple selected values from list in arrraylist on select in Spring MVC

I have a bean class named AnnouncementDetails having attributes
Now on submit i want to get the values of the selected users in selectedUsers Arraylist. Any idea how to do that? ..............................................
Change datatype to String array and then convert to arraylist by Arrays.asList(selectedUsers).
String [] selectedUsers

How can I add multiple instances to a Django reverse foreign key set in a minimal number of hits to the database?

For example, I have two models:
class Person(models.Model):
name = models.CharField(max_length=100)
class Job(models.Model):
title = models.CharField(max_length=100)
person = models.ForeignKey(Person)
I have a list of job ids--
job_ids = [1, 2, ....]
that are pks of Job model instances
I know I can do--
for id in job_ids:
person.jobs.add(job_id)
but this will be many more queries than if I could do--
person.jobs.add(job_ids)
where it would unpack the list and use bulk_create. How do I do this? Thanks.
Have you tried
person.jobs.add(*job_ids)
In my case I used a filter query and had a list of objects (as opposed to IDs). I was getting an error similar to
TypeError: 'MyModel' instance expected, got [<MyModel: MyModel Object>]
...before I included the asterisk.
credit (another SO question)
If you didn't create your jobs yet, you can create them by adding bulk=False
jobs_list=[
Job(title='job_1'),
Job(title='job_2')
[
person.jobs.add(*jobs_list, bulk=False) # your related_name should be 'jobs', otherwhise use 'job_sets'.

Grails parent child form validation

I have an Invoice
class Invoice{
static hasMany = [lineItems: InvoiceItem]
double total
}
class InvoiceItem{
String description
double price
double qty
}
My issue is with form validation. If a user enters a string or invalid number format in either price or qty I get a
Failed to convert property value of type java.lang.String
to required type double for property price
BUT the error is in the Invoice object NOT in the LineItems object therefore I cannot highlight in RED the form appropriately. (And field value remains at zero when displayed so message is somewhat meaningless to user)
I was thinking of using COMMAND object with String parameters and validating their numeric value but I can't figure how to bind the InvoiceItem List.
What is the appropriate Grails way?
I could do all validation on the client side in javascript but that is not my question
You can do it with command objects. You need to read:
http://blog.peterdelahunty.com/2009/01/cool-way-to-dynamically-add-entries-to.html
command object data binding
Grails command object data binding
http://grails.1312388.n4.nabble.com/validating-nested-command-objects-td1346994.html

Resources