-
Notifications
You must be signed in to change notification settings - Fork 67
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
validate_presence should work for date fields #91
Comments
In what case are you expecting an invalid date? Aren't you using a date picker? |
In this case I am creating models from a JSON API. Validations are to ensure I only have valid models even in the case something is not correct server side (ie. I get sent a nil date) |
IIRC, you should be able to use
or something like that. Your validation is equally valid and if you would like to submit a pull request with a spec, that would be great. |
Thanks, As a simple change to validate_presence, would this suffice? def validate_presence(field, value, setting) (or just including Time, as I presume all date casts are to Time?) |
Let's take a step back. Say there are two ways of populating your model. Way 1 is that the user enters info on their iPhone. Further, say that your model contains start time and end time, so it's ok to start something without ending it. That implies that for Way 1, a nil date is ok. Way 2 is to slurp it off the server, in which case, nil dates are not ok. So the default "validate this way all the time" ala Rails is not actually perfect for this kind of thing. I don't know if you have this requirement, but it's something worth considering. Comments? |
I'd say in the case a nil date is OK, then it's counterintuitive to use a presence validator at all?
|
@robj In Rails that would be |
currently validates_presence fails validation if the field is not of a numeric or string type. This seems to violate the principle of least surprise if it is not usable on all available field types.
|
@robj You might want to use a format validator. From the spec:
This would fail if I know you've already read this, but the relevant portion of the documentation regarding custom validators is: |
This has just had me going round in circles too until I decided to remove my validations. I tried validating the presence of a date and an array, both of which failed even though they were present. I was going to call the following line on each field change in my ViewController to disable the save button until all the required fields were filled in. |
I'm not sure this will work. The object does not have any data until you put it there. If you aren't doing that as the user enters it, you can't validate against it. Further, once you assign to a model field of type |
Ok so in which case, if I'm migrating some code from CDQ, I'd implement |
I'm not sure about the For the array, Remember, validations are not necessarily for doing on-the-fly form validations -- more so to ensure data integrity. There's no reason not to use them for input validations, but don't forget to roll back your transaction before moving on if the user cancels. The main place I'm looking is motion/validatable.rb. It's pretty straightforward code. |
Yep I've not an NSDate, so everything is easy there. Thanks for the help, this makes more sense now :) |
It is not immediately apparent from docs it only supports Numeric/String
my custom validator for the interim:
The text was updated successfully, but these errors were encountered: