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

Error on empty input when allow-empty is false #307

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ All helpers accept the following optional named arguments (even though they are
| `locale` | An optional `String` [locale](https://momentjs.com/docs/#/i18n/), to override the default global `moment.locale` |
| `timeZone` | An optional `String` [time zone](https://momentjs.com/timezone/docs/), defaults to `moment.timeZone` (the default time zone) |
| `interval` | An optional interval `Number` of milliseconds when the helper should be recomputed |
| `allow-empty` | An optional `Boolean` to ignore the `Invalid date` output when knowingly passing `null`, `undefined`, or `''`, defaults to `false` |
| `allow-empty` | An optional `Boolean` to suppress errors when knowingly passing `null`, `undefined`, or `''`, defaults to `false` |

Note that `interval` does not recompute the value of the helper parameters, unless it is
part of a helper that *is* a value in which case it is useful for "live" updating as time elapses.
Expand Down Expand Up @@ -414,7 +414,7 @@ export default Ember.Controller.extend({

### Global Allow Empty Dates

If `null`, `undefined`, or an empty string are passed as a date to any of the moment helpers then you will get `Invalid Date` in the output. To avoid this issue globally, you can set the option `allowEmpty` which all of the helpers respect and will result in nothing being rendered instead of `Invalid Date`.
If `null`, `undefined`, or an empty string are passed as a date to any of the moment helpers they will raise an error. To avoid this issue globally, you can set the option `allowEmpty` which all of the helpers respect and will result in no error and nothing being rendered.

```js
// config/environment.js
Expand Down Expand Up @@ -511,7 +511,7 @@ export default Ember.Route.extend({

An invalid date string is being passed into momentjs and/or the [input string format](https://momentjs.com/docs/#/parsing/string-format/) was omitted.

If you are knowingly passing null, undefined, or an empty string and want to ignore the output of `Invalid Date` then pass the option `allow-empty=true` to the helper (all helpers accept this property)
If you are knowingly passing null, undefined, or an empty string and want to avoid errors then pass the option `allow-empty=true` to the helper (all helpers accept this property)

```hbs
{{moment-format ''}} {{!-- Invalid date --}}
Expand Down
4 changes: 2 additions & 2 deletions addon/utils/helper-compute.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assert } from '@ember/debug';
import { isBlank } from '@ember/utils';
import { get } from '@ember/object';

Expand All @@ -20,8 +21,7 @@ export default function(cb) {
return;
}

/* eslint-disable no-console */
console.warn(`ember-moment: an empty value (null, undefined, or "") was passed to ember-moment helper`);
assert(`ember-moment: an empty value (null, undefined, or "") was passed to ember-moment helper`);
}

return cb.apply(this, arguments);
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/helpers/moment-to-now-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Ember from 'ember';
import EmberObject from '@ember/object';
import { run } from '@ember/runloop';
import hbs from 'htmlbars-inline-precompile';
Expand Down Expand Up @@ -96,7 +97,11 @@ test('can be called with null using global config option', function(assert) {
test('unable to called with null overriding global config option', function(assert) {
assert.expect(1);

const origOnerror = Ember.onerror;
Ember.onerror = () => assert.ok('empty value with allow-empty=false errors');

this.set('date', null);
this.render(hbs`{{moment-to-now date allow-empty=false}}`);
assert.equal(this.$().text(), 'Invalid date');

Ember.onerror = origOnerror;
});