Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow blank example #49

Open
nonotest opened this issue Feb 15, 2017 · 3 comments
Open

Allow blank example #49

nonotest opened this issue Feb 15, 2017 · 3 comments

Comments

@nonotest
Copy link

Hello I can't get the allow_blank validator to work.
Would it be possible to add an example in the doc?

For example:

params do
   requires :foo, type: :string, allow_blank: :true
end

but sending an empty string still fails the requires validation

thanks!

@falood
Copy link
Member

falood commented Feb 15, 2017

I'll explain it later, for now keep_blank is useful for you 🙂

params do
   requires :foo, type: :string, keep_blank: true
end

@nonotest
Copy link
Author

Hello!

thanks for the reply.

It looks like keep_blank will always put a blank value, no matter what.
What I am after is requiring the field but allowing a blank value. is that possible?

Thanks again!

@falood
Copy link
Member

falood commented Feb 15, 2017

Sorry, didn't get what do you want, can you show me your use case?

here are some examples:

  • for this line requires :foo, type: :string, foo="123" get %{foo: "123"} and foo="" get Parsing Param Error.

  • for this line requires :foo, type: :string, keep_blank: true, foo="123" get %{foo: "123"} and foo="" get %{foo: ""}

  • for this line optional :foo, type: :string, foo="123" get %{foo: "123"} and foo="" get %{}

By default, it's equal to pass blank parameters([], '', "", nil, %{}) and don't pass this parameter, {"a": 1, "b": null} and {"a": 1} are the same. keep_blank is useful to keep blank parameter for this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants