Skip to content

Validation should allow empty field value if use allow_nil: true #737

@soulfly

Description

@soulfly

I use the following validation in my Rails model:

  validates :website, length: {maximum: 200},
                                 format: {with: URI::regexp},
                              allow_nil: true

Unfortunately it does not allow me to save empty field value, I'm getting the following error when submit a form:
image

Looks like it comes from here
https://github.com/DavyJonesLocker/client_side_validations/blob/master/coffeescript/rails.validations.coffee#L287

From my understanding we may need to add return if options.allow_nil == true as well there

And also replace the line
hash[:allow_blank] = true if options[:allow_blank]
with
hash[:allow_blank] = true if options[:allow_nil] || options[:allow_blank]

here https://github.com/DavyJonesLocker/client_side_validations/blob/master/lib/client_side_validations/active_model/length.rb#L30

So you already have it for 'numeric' validation here https://github.com/DavyJonesLocker/client_side_validations/blob/master/lib/client_side_validations/active_model/numericality.rb#L44
so I propose to do the same for 'length' validation

Do you have any issues with the suggestion?

I use Rails 4.2 and 4.2.12 version of this gem but it looks like the issue is persist on master as well

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions