Skip to content

Commit

Permalink
add fecha instead of moment
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Feb 14, 2017
1 parent 4751917 commit dcaa273
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
14 changes: 8 additions & 6 deletions src/fields/fieldDateTimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<script>
/* global $ */
import abstractField from "./abstractField";
import moment from "moment/min/moment.min";
import fecha from "fecha";
import { defaults } from "lodash";
let inputFormat = "YYYY-MM-DD HH:mm:ss";
Expand All @@ -26,19 +26,21 @@
},
formatValueToField(value) {
if (value != null)
return moment(value, this.schema.format).format(this.getDateFormat());
if (value != null) {
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
return fecha.format(dt, this.getDateFormat());
}
return value;
},
formatValueToModel(value) {
if (value != null) {
let m = moment(value, this.getDateFormat());
let m = fecha.parse(value, this.getDateFormat());
if (this.schema.format)
value = m.format(this.schema.format);
value = fecha.format(m, this.schema.format);
else
value = m.toDate().valueOf();
value = m.valueOf();
}
return value;
Expand Down
8 changes: 4 additions & 4 deletions src/fields/fieldInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@

<script>
import abstractField from "./abstractField";
import moment from "moment/min/moment.min";
import fecha from "fecha";
export default {
mixins: [ abstractField ],
methods: {
formatValueToField(value) {
switch(this.schema.inputType){
case "date":
return moment(value).format("YYYY-MM-DD");
return fecha.format(value, "YYYY-MM-DD");
case "datetime":
return moment(value).format();
return fecha.format(value);
case "datetime-local":
return moment(value).format("YYYY-MM-DDTHH:mm:ss");
return fecha.format(value, "YYYY-MM-DDTHH:mm:ss");
default:
return value;
}
Expand Down
15 changes: 9 additions & 6 deletions src/fields/fieldPikaday.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<script>
import abstractField from "./abstractField";
import moment from "moment/min/moment.min";
import fecha from "fecha";
import { defaults } from "lodash";
let inputFormat = "YYYY-MM-DD";
Expand All @@ -25,20 +25,23 @@
},
formatValueToField(value) {
if (value != null)
return moment(value, this.schema.format).format(this.getDateFormat());
if (value != null) {
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
return fecha.format(dt, this.getDateFormat());
}
return value;
},
formatValueToModel(value) {
if (value != null) {
let m = moment(value, this.getDateFormat());
let m = fecha.parse(value, this.getDateFormat());
if (this.schema.format)
value = m.format(this.schema.format);
value = fecha.format(m, this.schema.format);
else
value = m.toDate().valueOf();
value = m.valueOf();
}
return value;
}
Expand Down
18 changes: 9 additions & 9 deletions src/utils/validators.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNil, isNumber, isString, isArray } from "lodash";
import moment from "moment/min/moment.min";
import fecha from "fecha";

function checkEmpty(value, required) {
if (isNil(value) || value === "") {
Expand Down Expand Up @@ -130,22 +130,22 @@ module.exports = {
date(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;

let m = moment(value);
if (!m.isValid())
let m = new Date(value);
if (!m)
return [msg(resources.invalidDate)];

let err = [];

if (!isNil(field.min)) {
let min = moment(field.min);
if (m.isBefore(min))
err.push(msg(resources.dateIsEarly, m.format("L"), min.format("L")));
let min = new Date(field.min);
if (m.valueOf() < min.valueOf())
err.push(msg(resources.dateIsEarly, fecha.format(m), fecha.format(min)));
}

if (!isNil(field.max)) {
let max = moment(field.max);
if (m.isAfter(max))
err.push(msg(resources.dateIsLate, m.format("L"), max.format("L")));
let max = new Date(field.max);
if (m.valueOf() > max.valueOf())
err.push(msg(resources.dateIsLate, fecha.format(m), fecha.format(max)));
}

return err;
Expand Down

0 comments on commit dcaa273

Please sign in to comment.