Skip to content

Commit

Permalink
Merge pull request #117 from icebob/moment-fecha
Browse files Browse the repository at this point in the history
Moment -> fecha
  • Loading branch information
icebob authored Feb 14, 2017
2 parents a69e244 + 9f707dc commit 3e05e14
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": ["es2015", "stage-2"],
"plugins": ["transform-runtime"],
"plugins": ["transform-runtime", "lodash"],
"comments": false
}
13 changes: 3 additions & 10 deletions dist/vue-form-generator.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"devDependencies": {
"babel-core": "6.22.1",
"babel-loader": "6.2.10",
"babel-plugin-lodash": "3.2.11",
"babel-plugin-transform-runtime": "6.22.0",
"babel-preset-es2015": "6.22.0",
"babel-preset-stage-2": "6.22.0",
Expand All @@ -56,6 +57,7 @@
"eslint-plugin-vue": "1.0.0",
"extract-text-webpack-plugin": "1.0.1",
"fakerator": "0.3.0",
"fecha": "2.3.0",
"gitbook-cli": "2.3.0",
"inject-loader": "2.0.1",
"isparta-loader": "2.0.0",
Expand Down
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
22 changes: 12 additions & 10 deletions src/fields/fieldInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,23 @@

<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");
case "datetime":
return moment(value).format();
case "datetime-local":
return moment(value).format("YYYY-MM-DDTHH:mm:ss");
default:
return value;
if (value != null) {
switch(this.schema.inputType){
case "date":
return fecha.format(value, "YYYY-MM-DD");
case "datetime":
return fecha.format(value);
case "datetime-local":
return fecha.format(value, "YYYY-MM-DDTHH:mm:ss");
default:
return value;
}
}
},
formatValueToModel(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
Loading

0 comments on commit 3e05e14

Please sign in to comment.