Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

numericality greaterThan is always required #31

Closed
droganov opened this issue Feb 12, 2015 · 8 comments
Closed

numericality greaterThan is always required #31

droganov opened this issue Feb 12, 2015 · 8 comments

Comments

@droganov
Copy link

Hello,
I my case price field is optional, but if it's filled, then it should be a positive integer.

How do I configure the schema?

@ansman
Copy link
Owner

ansman commented Feb 12, 2015

var constraints = {
  price: {
    numericality: {
      onlyInteger: true,
      greaterThan: 0
    }
  }
};

validate({}, constraints);
// => undefined

validate({price: 0}, constraints);
// => {price: ["Price must be greater than 0"]}

validate({price: 1.2}, constraints);
// => {price: ["Price must be an integer"]}

@droganov
Copy link
Author

Nicklas, my config is:

price:
    numericality:
        greaterThan: 0
        onlyInteger: true
        notGreaterThan: "Положительное число"

I see 2 problems.

  1. I get must be greater than 0 exception for an empty field
  2. notGreaterThan option is replaced with default error message

When I enter positive integer, I see no error message.

Version is 0.4.0

@ansman
Copy link
Owner

ansman commented Feb 12, 2015

You cannot set a greater than only message like that. The validators only accepts "message", the other ones are when setting default messages.

I don't really understand problem #1, the validator will accept all integers above 0 and reject all values that aren't empty.

On Thu, Feb 12, 2015 at 12:29 PM, droganov [email protected]
wrote:

Nicklas, my config is:

price:
  numericality:
      greaterThan: 0
      onlyInteger: true
      notGreaterThan: "Положительное число"

I see 2 problems.

  1. I get must be greater than 0 exception for an empty field
  2. notGreaterThan option is replaced with default error message
    When I enter positive integer, I see no error message.

Version is 0.4.0

Reply to this email directly or view it on GitHub:
#31 (comment)

@droganov
Copy link
Author

Ok, I can reconfigure messages, but as for validation, I expect validator to accept empty value, since the field is not required.

@ansman
Copy link
Owner

ansman commented Feb 12, 2015

But it does accept empty values as in my example? Or do you have an example of where it doesn't work?

@droganov
Copy link
Author

@ansman
Copy link
Owner

ansman commented Feb 12, 2015

Ah, the problem is that it doesn't accept empty strings. I'll make a fix for this.

@droganov
Copy link
Author

Great, thank you!

@ansman ansman closed this as completed in c6e8fe3 Mar 4, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants