Giter VIP home page Giter VIP logo

grails-constraints's People

Contributors

geofflane avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

grails-constraints's Issues

Constraints plug-in causes tests on Controllers that use Commands fail once installed

I have created a simple grails-app that demonstrates that when the constraints plug-in is installed that my integration test fails and without it, it succeeds. The failure I get is specifically

No signature of method: net.rrullo.tester.UserController$_closure9.doCall() is applicable for
argument types: (net.rrullo.tester.UserCommand, net.rrullo.tester.UserCommand) values:
[net.rrullo.tester.UserCommand@1c1902d, net.rrullo.tester.UserCommand@6c4fe] Possible
solutions: doCall(net.rrullo.tester.UserCommand), call(), call(net.rrullo.tester.UserCommand),
call([Ljava.lang.Object;), call(java.lang.Object)

I'm new to publishing to github so I'm not sure if this is the appropriate way to do this or not, but my demo testapp is available at http://dl.dropbox.com/u/6783272/constraints-test.zip.

Thanks!
-Bob

support multiple error codes

When writing a custom grails constraint, the validation might fail for multiple reasons and you can instruct grails to use a different error message code in each case. For example, a ZIP code validator might fail because the ZIP is the wrong length, or the ZIP doesn't exist.

Grails also allows you to specify custom arguments that will be used when those messages are resolved. Here's an example of how to use both these features with a Grails custom validator:

    zipCode(validator: {zipCode, target ->

        def zipCodeLength = 5

        if (zipCode?.size() != zipCodeLength) {

            // In this case the message code "className.propertyName.size" will be used and zipCodeLength will be passed 
            // as the message argument with index 3
            return ['size', zipCodeLength]
        }

        if (!target.locationManager.findZipCode(zipCode)) {

            // In this case the message code "className.propertyName.invalid" will be used
            return 'invalid'
        }
    })

It seems that the failureCode property allows you to use a different message for each class that uses a constraint. However, for a given class it seems you're limited to one error message and there's no way to pass custom arguments when the message is resolved.

BTW, thanks a lot for the plugin, seems to have been very well thought out.

Constraints not being found errors - Grails 1.3.7

Just did a simple install into grails 1.3.7. Ran app and got the following errors

2011-05-19 11:15:29,979 [main] ERROR commons.DefaultGrailsApplication - The class [net.zorched.constraints.ComparisonConstraint] was not found when attempting to load Grails application. Skipping.
2011-05-19 11:15:29,984 [main] ERROR commons.DefaultGrailsApplication - The class [net.zorched.constraints.SsnConstraint] was not found when attempting to load Grails application. Skipping.
2011-05-19 11:15:29,988 [main] ERROR commons.DefaultGrailsApplication - The class [net.zorched.constraints.UsPhoneConstraint] was not found when attempting to load Grails application. Skipping.
2011-05-19 11:15:29,992 [main] ERROR commons.DefaultGrailsApplication - The class [net.zorched.constraints.UsZipConstraint] was not found when attempting to load Grails application. Skipping.

Causes console to crash whenever a domain with a constraint is modified.

Tried uninstalling and reinstalling, same issue.

Different constraint sets for the same Domain.

Is it possible to have different constraint sets for the same Domain object, that can be applied
in different situations? I.e. to have not only one but several?
I see many examples how to "share" constrains, but I need exactly the opposite:
e.g. on the same Domain, under different circumstances to change the behavior.

Error running on IBM J9 VM(Java 1.6)

I am using grails 2.3.7 and trying to use IBM JVM .The application fails on start up because of the constraint plugin.My logs on startup is

Fatal error during compilation java.lang.ClassFormatError: JVMCFRE009 interface field must be public static and final; class=net/zorched/grails/plugins/validation/GrailsConstraintClass, offset=1396 (NOTE: Stack trace has been filtered. Use --verbose to see entire trace.)

Has anyone faced this?

Potential support for @TestFor

Hi Geoff,

Pull request #15 offers support for @TestMixin annotations in Grails 2 unit tests for Constraints.

There is another feature that can expand this even more, and that's supporting the @testfor annotation outright. This would mean instead of this:

@TestMixin(ConstraintUnitTestMixin)
class MyConstraintTests {
    void testStuff() {
        def constraint = testFor(MyConstraint)

        // Params are automatically mixed in to the test class and exposed
        // to the constraint with the call above.
        params.foo = "bar"

        assert constraint.validate("foo", null, null) == true
    }
}

You can do this:

@TestFor(MyConstraint)
class MyConstraintTests {
    void testStuff() {
        // No need to setup constraint, it's automatically setup in each test.

        // Params are automatically mixed in to the test class and exposed
        // to the constraint with the call above.
        params.foo = "bar"

        assert constraint.validate("foo", null, null) == true
    }
}

@testfor handles weaving in the relevant mixin class, so in the latter example ConstraintUnitTestMixin would still get mixed into the class at compile time. TestFor has the added avantage of automatically calling testForโ€  to setup the class under test in each test method. Sicne this is now the standard in Grails for testing core artefacts like Services/Controllers/Taglibs, it is nice to offer this for custom artefacts like Constraints too.

The caveat is to offer this support a small amount of monkey-patching is required. A hook needs to be added to _Events.groovy to let TestForTransformation know about your custom artefact. Take a look at how I did it in my of my plugins and you'll see what I'm talking about.

I posted about this on the Grails user mailing list and Graeme Rocher responded without any complaints about this approach. He recommended a feature request be raised to have an official API for this in a future version of Grails 2.

So it's up to you really, if you want to add in the small amount of monkey-patching code to _Events.groovy then you can have @testfor support now, or you can wait until there's an official mechanism to register custom artefacts.

Let me know what you think!

โ€  well actually, TestForTransformation automatically calls mock, testFor is just a convention and in the ConstraintUnitTestMixin it just calls mockConstraint

constraintPropertyName and constraintOwningClass are always null

I wanted a constraint like the one below. But i always get errors like this:

Property [null] with value [12/10/10 12:00 AM] is not [>] [releaseDate]

Given this message:

default.invalid.compare.message=Property [{0}] with value [{1}] is not [{2}] [{3}]

/**

  • Use it by doing something like:
  • endDate(compare: [op: '>', field:'startDate'])
  • lowerRange(compare: [op: '<=', field:'upperRange'])
    */
    class CompareConstraint {
    static expectsParams = ['op', 'field']

def validate = { val, target, errors ->
def compareVal = target."$params.field"
def result = false
if (val && compareVal) {
switch (params.op) {
case ">": result = val.compareTo(compareVal) > 0; break
case ">=": result = val.compareTo(compareVal) >= 0; break
case "==": result = val.compareTo(compareVal) == 0; break
case "<=": result = val.compareTo(compareVal) <= 0; break
case "<": result = val.compareTo(compareVal) < 0; break
default: throw new IllegalArgumentException("Invalid operation: " + params.op)
}
}

if (!result) {
    //TODO: constraintPropertyName doesn't seem to get set? not sure why not.
    def args = ArrayUtil.createArray(constraintPropertyName, val, params.op, params.field)
    errors.rejectValue(constraintPropertyName, 'default.invalid.compare.message', args, 'Invalid value')
}

return true //always return true, since we are manually inserting the error into the errors obj

}
}

Null value vetoing custom constraint

Hi, I have a domain class with nullable: true in a property and then a domain custom constraint in the same property, the problem is that the custom constraint is not executing when nullable is true, if I set nullable to false, the custom constraint executes normally.
I've been reading and this could be happening because nullable is vetoing other constraints, this issue has been solved in grails, but seems that it wasn't in this plugin.
thanks!

A compilation error occurs without hibernate plugin

Thank you for creating this great plugin.

I have uninstalled hibernate plugin because I wanted to use only MongoDB for the data source.
When I installed the custom constraints plugin, a compilation error occurred.

The error message said that "DefaultGrailsConstraintClass.java" has a dependency to hibernate plugin(org.hibernate.SessionFactory).

Are there any ways to avoid this error?

Allow changing the default location for constraints

I would prefer to have my constraints more of a higher-level artifact, putting them in grails-app/Constraints but there isn't an easy way to do this right now. This ability is also very much needed if other plugins are looking to bundle in custom constraints that use this plugin. (for example, shared domain classes between projects)

Docs required for dealing with null or blank values

I have been writing a custom constraint, but it was obvious that the validate method was not getting called when null or blank values were passed to it.
I had to resort to digging through the code to see if I could work out why this was. I found out that I could get the validate method to fire by having these two lines in my constraint:

def skipBlankValues = false
def skipNullValues = false

If this could be mentioned in the docs it would have saved me a lot of time!

Dependency injection not being auto-wired

In the plugin documentation, it indicates that dependency injection of services is supported. The following code doesn't seem to inject the armService into the constraint.

package com.vitalsmarts.ema.constraint

class OrderFoundConstraint {
def armService

static defaultMessageCode = "default.order.not.found.error"
static failureCode = "order.not.found.error"

def validate = {identifier ->
armService.basicOrderInfo(identifier) ? true : false
}
}

The exception is a null pointer because armService is null. Any suggestions oralternatives?

Grails 2.0 Unit tests

Just trying up upgrade my project to Grails 2.0 and run into a problem with this plugin.

Some of my domain classes share a need for complicated validation, so I use your plugin to extract it to a common constraint. However when running my unit tests in Grails 2.0 I get:

java.lang.IllegalArgumentException: ServletContext must not be null

When I attempt to construct a Domain class with this custom constraint. If I comment out the constraint, everything works fine.

create-constraint script does not default in package name.

Steps:
grails create-constraint net.rrullo.tester.FooBar

Creates FooBar in package structure net.rrullo.tester correctly, but does not place a package name in the groovy class.

Fix is simple, update the /src/templates/artifacts/Constraint.groovy from

class @artifact.name@ {

def validate = { propertyValue ->
    // execute validation
}

}

  • to -

@artifact.package@class @artifact.name@ {

def validate = { propertyValue ->
    // execute validation
}

}

No Such Property Error

I installed the plugin. I ran the create-constraint script and created my constraint. I set the constraint on each property in my domain class. Then I tried to use the constraint in my 'create' template with the following line:

<% if(!cp?.secondary) { %>

And I get the following error when trying to generate views:

Error executing script GenerateViews: No such property: secondary for class: org.codehaus.groovy.grails.validation.ConstrainedProperty
No such property: secondary for class: org.codehaus.groovy.grails.validation.ConstrainedProperty

I'm really not sure why it doesn't recognize the constraint. I'm not trying to do anything overly complicated.

Docs for defaultMessageCode give incorrect message code name

The docs here:

https://github.com/geofflane/grails-constraints/blob/master/README.markdown#defaultmessagecode-property-optional

state that "The default value is default.$name.invalid.message". However, it appears that the actual default value of the defaultMessageCode is default.invalid.$name.message ($name and invalid are in different orders). However, I don't think the fix should be to change the docs. To be consistent with the rest of grails (look at the default messages for the built-in grails constraints as well as the message codes defined for custom constraints by this plugin), I believe the code should be fixed to reflect what the docs say. It looks like the DefaultGrailsConstraintClass is the problem:

public String getDefaultMessageCode() {
    String obj = (String)getPropertyValue(DEFAULT_MESSAGE_CODE_PROPERTY);
    if (obj == null) return "default.invalid." + getConstraintName() + ".message";
    return obj;
}

This line:

if (obj == null) return "default.invalid." + getConstraintName() + ".message";

should change to read:

if (obj == null) return "default."  + getConstraintName() + ".invalid.message";

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.