I've read a lot about uniqueness and constraints in Grails (but maybe not enough)
I can't make the unique constraint to work on multiple fields as explained here:
http://grails.org/doc/1.3.7/ref/Constraints/unique.html
(I'm using grails 1.3.9)
I have 2 domain classes:
class Dog {
static constraints = {
humanSsn(unique: ['name', 'breed'])
//I also tried with just 2 fields, didn't work either.
}
Integer humanSsn
String name
String breed
}
class Human {
static constraints = {
ssn(unique: true)
}
Integer ssn
String name
}
It is a legacy DB, so I cant modify the tables.
When I save a Human, I (just to test) save two dogs with the same name, breed and humanSsn
def humanoInstance = new Humano(params)
if (humanoInstance.save(flush: true)) {
def newDog = new Dog()
def newDogTwo = new Dog()
newDog.name = "n1"
newDog.breed = "b1"
newDog.humanSsn = humanInstance.ssn
println newDog.validate()
println newDog.getErrors()
newDog.save(failOnError:true)
newDogTwo.name = "n1"
newDogTwo.breed = "b1"
newDogTwo.humanSsn = humanInstance.ssn
println newDogTwo.validate()
println newDogTwo.getErrors()
newDogTwo.save(failOnError:true)
}
But it saves anyway the 2 dogs without complaining nor throwing any errors.
true
org.springframework.validation.BeanPropertyBindingResult: 0 error
true
org.springframework.validation.BeanPropertyBindingResult: 0 error
What am I doing wrong?
Thanks in advance.
it may be due to validation works on database level
and newDog.save(failOnError:true) doesnot save dog object immediately
have you try
newDog.save(flush:true)
for first dog and then
newDogTwo.save(failOnError:true)
it should work
Related
I try to validate a nested domain class instance on a command object.
Having the following command object
package demo
import grails.databinding.BindingFormat
class SaveEventCommand {
#BindingFormat('yyyy-MM-dd')
Date date
Refreshment refreshment
static constraints = {
date validator: { date -> date > new Date() + 3}
refreshment nullable: true
}
}
And having the following domain class with its own constraints
package demo
class Refreshment {
String food
String drink
Integer quantity
static constraints = {
food inList: ['food1', 'food2', 'food3']
drink nullable: true, inList: ['drink1', 'drink2', 'drink3']
quantity: min: 1
}
}
I need when refreshment is not nullable the command object validates the date property and check the corresponding restrictions in refreshment instance
For now try with this code in the controller:
def save(SaveEventCommand command) {
if (command.hasErrors() || !command.refreshment.validate()) {
respond ([errors: command.errors], view: 'create')
return
}
// Store logic goes here
}
Here through !command.refreshment.validate() I try to validate the refresh instance but I get the result that there are no errors, even when passing data that is not correct.
Thank you any guide and thank you for your time
I typically just include some code that will use a custom validator to kick off validation for any property that is composed of another command object. For example:
thePropertyInQuestion(nullable: true, validator: {val, obj, err ->
if (val == null) return
if (!val.validate()) {
val.errors.allErrors.each { e ->
err.rejectValue(
"thePropertyInQuestion.${e.arguments[0]}",
"${e.objectName}.${e.arguments[0]}.${e.code}",
e.arguments,
"${e.objectName}.${e.arguments[0]}.${e.code}"
)
}
}
})
This way it's pretty clear that I want validation to occur. Plus it moves all the errors up into the root errors collection which makes things super easy for me.
Two things I could think of:
Implement grails.validation.Validateable on your command object
What happens when you provide an invalid date? Can you see errors while validating?
I have the following hierarchy of classes:
class Incident {// Id => Entity
#Id
String id
List<Participant> participants
List<RealEstateProperty> realEstateProperties
}
where
class Participant {// No id => by javers terms - ValueObject
EnclosedContact contact
}
class EnclosedContact {// No id => by javers terms - ValueObject
String name
}
class RealEstateProperty {// No id => by javers terms - ValueObject
List<CadastralSection> cadastralSections
}
class CadastralSection {// No id => by javers terms - ValueObject
String sectionId
}
I have written the following test (in groovy):
def "Querying Javers Repository for participants changes works correctly"() {
given:
(1..3).each {
javers.commit("author", new Incident(
id: it,
participants: [
new Participant(contact: new EnclosedContact(id: 20 + it))
]
))
}
when:
def snapshots = javers.findSnapshots(QueryBuilder.byValueObjectId(1, Incident.class, "contact").build())
then:
assert snapshots.size() == 1
}
The result of this test is:
JaversException: PROPERTY_NOT_FOUND property 'contact' not found in class 'Incident'
Trying to get the changes this way
def snapshots = javers.findSnapshots(QueryBuilder.byValueObjectId(1, Incident.class, "participants/0/contact").build())
returns empty list.
Does Javers support selecting for changes on nested ValueObjects?
in JaVers 1.6.2 there is a basic support for nested ValueObjects queries (undocumented yet). Your query should work for data persisted by this JaVers version. For example:
def "should query for changes on nested ValueObjects stored in a list"(){
given:
def user = new DummyUserDetails(
id:1,
addressList: [new DummyAddress(networkAddress: new DummyNetworkAddress(address: "a"))])
javers.commit("author", user)
user.addressList[0].networkAddress.address = "b"
javers.commit("author", user)
when:
def changes = javers.findChanges(QueryBuilder.byValueObjectId(1, DummyUserDetails,
"addressList/0/networkAddress").build())
then:
changes.size() == 1
changes[0].left == "a"
changes[0].right == "b"
}
I created a generic nested object comparator, hope it helps.
https://gist.github.com/hank-cp/3db40faed1dd9f02ababd86c2c9eaf8d
Registers it like this way:
NestedObjectComparator productRootComparator =
new NestedObjectComparator<POProductRoot>() {};
sharedJavers = JaversBuilder.javers()
.registerValue(POProductRoot.class)
.registerCustomComparator(productRootComparator, POProductRoot.class).build();
productRootComparator.setJsonConverter(sharedJavers.getJsonConverter());
Then your will get the changes result in MapChange format.
Since JaVers 2.1, there is a new filter for this type of queries - child-value-objects-filter
I have the following problem:
I have a domain class called customer, with a field discount embedded.
class Customer {
...
String username
Discount discount
static constraints = { ... }
}
class Discount {
Integer item1
Integer item2
static constraints = { item1 min:1, max:100, nullable:true }
}
I have a controller, where a customer's data can be modified. The code goes something like this:
def edit() {
Customer c = Customer.findByUsername(params.userName)
if(request.method != 'GET'){
bindData(c, params)
if(c.validate()) {
//save the result
}
}
println c.dump()//1
model:[customer:c]
}
Then in the edit.gsp I put the following code:
${customer.dump()}//2
${customer.discount.dump()}
Now my problem is, if I have a validation error, for example the user enters 123 for item1, I get the appropriate errors object which says that Customer bean has 1 field error on field discount.item1 when I call println c.dump()//1
In the edit.gsp on the other hand, the customer bean doesn't have any field errors but customer.discount has the mentioned error. This is a big inconvenience, because I want to render errors next to the fields like so:
<g:renderErrors bean="${customer}" field="discount.item1"/>
But the customer bean doesn't have any errors, just the discount bean (therefore I don't get any errors rendered).
Has this problem occured to any of you ?
It seems that #Validatable classes use functionality of spring framework's AbstractBindingResult, which doesn't support this usage (as far as I can tell).
However I was able to create a workaround, which could be used as a taglib to achieve the same effect:
Given two validateable classes
#Validateable
class TestA {
TestB b
static constraints = {
b validator: {
it.validate()
}
}
}
#Validateable
class TestB {
int c
static constraints = {
c min: 100
}
}
One can simply resolve the bean and the attribute and delegate the rendering to the existing taglib.
import test.*
import org.codehaus.groovy.grails.plugins.web.taglib.*
ValidationTagLib validationTagLib = ctx.getBean(ValidationTagLib)
renderErrorsWithNestedFields = { attrs, body ->
def modifiedAttrs = attrs
String[] path = attrs?.field?.split('\\.')
if(path?.size() > 1) {
Object resolvedBean
resolvedBean = attrs.bean
path[0..-2].each {
//Some error handling would not hurt, but this is just a proof of concept
resolvedBean = resolvedBean[it]
}
modifiedAttrs = new HashMap(attrs)
modifiedAttrs.bean = resolvedBean
modifiedAttrs.field = path.last()
}
validationTagLib.renderErrors (modifiedAttrs, null)
}
TestA a = new TestA(b:new TestB(c:3))
a.validate()
renderErrorsWithNestedFields([bean:a, field:'b.c'], null)
And this should render an error for violating the min constraint on c.
I have a Spring Security User class which has a unique constraint for username and email. In a Command class I imported all constraints from this class with "importFrom User". All constraints work as expected EXCEPT the unique ones.
However when saving the User the unique constraints get validated and errors are shown. But it would be nice if they get validated BEFORE saving like all other constraints.
UPDATE
I added this to the controller:
user.errors.fieldErrors.each {
command.errors.rejectValue(it.getField(), it.getCode())
}
Seems like a dirty workaround, but it works.
Good question #Chris, and your solution is best since the goal of sharing constraints between domain classes and command objects is to avoid duplicating validation logic.
I'll just add that to avoid duplicating field errors and to handle nested field paths in domain objects, something like the following might be necessary.
def save(EntityCreateCommand cmd) {
def entity = new Entity(cmd.properties)
def someAssociation = new Something(cmd.properties)
entity.someAssociation = someAssociation
entity.validate()
entity.errors.fieldErrors.each {
def fieldName = it.field.split("\\.").last()
def flattenedCodes = cmd.errors.getFieldErrors(fieldName).codes.flatten()
if(cmd.hasProperty(fieldName) && (!flattenedCodes.contains(it.code))) {
cmd.errors.rejectValue(fieldName,
"entityCreateCommand.${fieldName}.${it.code}")
}
}
if(cmd.errors.hasErrors()) {
error handling stuff...
} else {
business stuff...
}
}
I had problems with unique constraint before, so I made a custom validator in my command object to test and see if it's unique:
Command Object:
class wateverCommand{
....
String username
static constraints = {
username validator:{value, command ->
if(value){
if(User.findByUsername(value){
return 'wateverCommand.username.unique'
}
}
}
}
}
within your messages.properties add a custom error message:
wateverCommand.username.unique The username is taken, please pick a new username
I agree the unique constraint doesn't always seem to import properly. Since I like to avoid clutter in the constraint body I like the one liner approach:
validator: {value, command -> (User.findByUsername(value) ? false : true ) }
Then in your message.properties it would be:
accountCommand.username.validator.error=That username already exists
I'm just starting out with F# and I can't find the syntax to do object initialization like in C# 3.
I.e. given this:
public class Person {
public DateTime BirthDate { get; set; }
public string Name { get; set; }
}
how do I write the following in F#:
var p = new Person { Name = "John", BirthDate = DateTime.Now };
You can do it like this:
let p = new Person (Name = "John", BirthDate = DateTime.Now)
the answer from CMS is definitely correct. Here is just one addition that may be also helpful. In F#, you often want to write the type just using immutable properties. When using the "object initializer" syntax, the properties have to be mutable. An alternative in F# is to use named arguments, which gives you a similar syntax, but keeps things immutable:
type Person(name:string, ?birthDate) =
member x.Name = name
member x.BirthDate = defaultArg birthDate System.DateTime.MinValue
Now we can write:
let p1 = new Person(name="John", birthDate=DateTime.Now)
let p2 = new Person(name="John")
The code requires you to specify the name, but birthday is an optional argument with some default value.
You can also omit the new keyword and use less verbose syntax:
let p = Person(BirthDate = DateTime.Now, Name = "John")
https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/members/constructors