GithubHelp home page GithubHelp logo

Comments (8)

adzap avatar adzap commented on July 17, 2024

If you have not set a value on the attribute then it wil be invalid and output the error message you are seeing. You need assign a value for it to be validated against the restrictions such as on_or_after.

Treat it like other attributes where you need to have a validates_presence_of. Use this in combination with :allow_nil => true option on validates_date, to avoid two errors for the same attribute.

from validates_timeliness.

InvisibleTech avatar InvisibleTech commented on July 17, 2024

Thanks for the follow-up, I have debugged into the controller and I see that a string from the UI like this:

May 21, 1999

Is being sent up. Initially, when I played with your gem I thought I had issues with the long US format like that. But when I tried to add a supported mask (per your git wiki) I was informed that the format: mmm dd, yyyy was already supported.

I will verify my claimed experience again.

from validates_timeliness.

adzap avatar adzap commented on July 17, 2024

The formats in the gem are the supported parsing formats. If you are entering the date in a text field these would applicable. What field type are you entering the date into?

Just a note, the wiki is valid for the latest gem version, v3.0.x. The formats should mostly be the same, however. You can add more that you need.

On 27/05/2011, at 8:31 AM, [email protected] wrote:

Thanks for the follow-up, I have debugged into the controller and I see that a string from the UI like this:

May 21, 1999

Is being sent up. Initially, when I played with your gem I thought I had issues with the long US format like that. But when I tried to add a supported mask (per your git wiki) I was informed that the format: mmm dd, yyyy was already supported.

I will verify my claimed experience again.

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

from validates_timeliness.

InvisibleTech avatar InvisibleTech commented on July 17, 2024

I am using a text field like this:

    <legend>Availability</legend>
    <div>
      <%= f.label :publish_on, "Publish on:" %>
      <%= f.text_field :publish_on, :class => "date_select_entry" %>
      <abbr class="required-indicator" title="Required field">*</abbr>
    </div>

I am also using a slightly customized FormBuilder for output:

class FormattedDateFormBuilder < ActionView::Helpers::FormBuilder
def text_field(field_name, *args)
set_value_to_formatted_date field_name, args
super
end

def set_value_to_formatted_date(field_name, args)
    opts = args.extract_options!
    value = object.send field_name

    (opts[:value] = value.strftime("%B %d, %Y")) if is_supposed_to_be_date_value?(opts, value)

    args << opts
end

def is_supposed_to_be_date_value? opts, value
    value.instance_of?(Date) && opts[:class] == "date_select_entry"
end

end

I adopted this approach before using your gem in order to dry up stuff I needed to drive a JQuery plugin Datepicker.

I will be able to re-check the flow later when I can sit down at the debugger. I will report what I find here so don't burn any cycles yet.

from validates_timeliness.

InvisibleTech avatar InvisibleTech commented on July 17, 2024

I updated the Unit Test to use the same string format as I use in my application:

  def setup
      @min_offer = Offer.new :title => "Test Offer", :valid_starting_on => Date.today, :publish_on => Date.today
      @min_offer.seller = sellers(:seller_two)
      
      @min_offer.save
  end

  test "update of offer fails when publish on is before today" do
    @min_offer.update_attributes :publish_on => (Date.today-1).strftime("%B %d, %Y")

    assert_equal ["Publish on must be on or after #{Date.today}"], @min_offer.errors.full_messages
  end

And it now produces the same shorter message as in the application because now my Unit Test fails like this:

<["Publish on must be on or after 2011-05-26"]> expected but was
<["Publish on is not a valid date"]>.

Do I need to do this?

ValidatesTimeliness::Formats.add_formats(:date, "mmm dd, yyyy")

If I do use this do I need this? (Because I am not using the plugin parser since my date format is parsed by Ruby Date)

config.use_plugin_parser = true

from validates_timeliness.

adzap avatar adzap commented on July 17, 2024

What is the value of publish_on just before the assert?

from validates_timeliness.

InvisibleTech avatar InvisibleTech commented on July 17, 2024

It is nil as demonstrated by this output:

    Publish_on is now [] and is class NilClass

Produced by using this in the test:

    puts "Publish_on is now [#{@min_offer.publish_on}] and is class #{@min_offer.publish_on.class.name}"

from validates_timeliness.

adzap avatar adzap commented on July 17, 2024

So the value is still nil and therefore an invalid date. The on_or_after value is not assessed if the value is nil.

It is invalid because the validation checks if you had attempted to assign a value by checking if publish_on_before_type_cast value is not nil and publish_on is nil. In that scenario the string has not been parsed properly.

I will leave it for you to investigate further.

On 28/05/2011, at 6:01 AM, [email protected] wrote:

It is nil as demonstrated by this output:

   Publish_on is now [] and is class NilClass

Produced by using this in the test:

   puts "Publish_on is now [#{@min_offer.publish_on}] and is class #{@min_offer.publish_on.class.name}"

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

from validates_timeliness.

Related Issues (20)

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.