Fix date components and new doc validation

This commit is contained in:
SachaG 2018-03-28 11:14:36 +09:00
parent ce7e0b0b25
commit 9aa06cee82
5 changed files with 45 additions and 43 deletions

View file

@ -651,6 +651,7 @@ class Form extends Component {
<Components.FormGroup <Components.FormGroup
key={group.name} key={group.name}
{...group} {...group}
errors={this.state.errors}
currentValues={this.state.currentValues} currentValues={this.state.currentValues}
updateCurrentValues={this.updateCurrentValues} updateCurrentValues={this.updateCurrentValues}
formType={this.getFormType()} formType={this.getFormType()}

View file

@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import DateTimePicker from 'react-datetime'; import DateTimePicker from 'react-datetime';
import { registerComponent } from 'meteor/vulcan:core'; import { registerComponent } from 'meteor/vulcan:core';
class Date extends PureComponent { class DateComponent extends PureComponent {
constructor(props) { constructor(props) {
super(props); super(props);
@ -11,14 +11,14 @@ class Date extends PureComponent {
} }
// when the datetime picker has mounted, SmartForm will catch the date value (no formsy mixin in this component) // when the datetime picker has mounted, SmartForm will catch the date value (no formsy mixin in this component)
componentDidMount() { // componentDidMount() {
if (this.props.value) { // if (this.props.value) {
this.updateDate(this.props.value); // this.updateDate(this.props.value);
} // }
} // }
updateDate(date) { updateDate(date) {
this.context.updateCurrentValues({[this.props.name]: date}); this.context.updateCurrentValues({[this.props.path]: date});
} }
render() { render() {
@ -42,7 +42,7 @@ class Date extends PureComponent {
} }
} }
Date.propTypes = { DateComponent.propTypes = {
control: PropTypes.any, control: PropTypes.any,
datatype: PropTypes.any, datatype: PropTypes.any,
group: PropTypes.any, group: PropTypes.any,
@ -51,10 +51,10 @@ Date.propTypes = {
value: PropTypes.any, value: PropTypes.any,
}; };
Date.contextTypes = { DateComponent.contextTypes = {
updateCurrentValues: PropTypes.func, updateCurrentValues: PropTypes.func,
}; };
export default Date; export default DateComponent;
registerComponent('FormComponentDate', Date); registerComponent('FormComponentDate', DateComponent);

View file

@ -11,14 +11,14 @@ class DateTime extends PureComponent {
} }
// when the datetime picker has mounted, SmartForm will catch the date value (no formsy mixin in this component) // when the datetime picker has mounted, SmartForm will catch the date value (no formsy mixin in this component)
componentDidMount() { // componentDidMount() {
if (this.props.value) { // if (this.props.value) {
this.updateDate(this.props.value); // this.updateDate(this.props.value);
} // }
} // }
updateDate(date) { updateDate(date) {
this.context.updateCurrentValues({[this.props.name]: date}); this.context.updateCurrentValues({[this.props.path]: date});
} }
render() { render() {

View file

@ -11,16 +11,16 @@ class Time extends PureComponent {
} }
// when the datetime picker has mounted, SmartForm will catch the date value (no formsy mixin in this component) // when the datetime picker has mounted, SmartForm will catch the date value (no formsy mixin in this component)
componentDidMount() { // componentDidMount() {
if (this.props.value) { // if (this.props.value) {
this.context.updateCurrentValues({[this.props.name]: this.props.value}); // this.context.updateCurrentValues({[this.props.path]: this.props.value});
} // }
} // }
updateDate(mDate) { updateDate(mDate) {
// if this is a properly formatted moment date, update time // if this is a properly formatted moment date, update time
if (typeof mDate === 'object') { if (typeof mDate === 'object') {
this.context.updateCurrentValues({[this.props.name]: mDate.format('HH:mm')}); this.context.updateCurrentValues({[this.props.path]: mDate.format('HH:mm')});
} }
} }

View file

@ -40,26 +40,30 @@ export const validateDocument = (document, collection, context) => {
}); });
// 4. check that required fields have a value // 4. check that required fields have a value
_.keys(schema).forEach(fieldName => { // _.keys(schema).forEach(fieldName => {
const fieldSchema = schema[fieldName]; // const fieldSchema = schema[fieldName];
if ((fieldSchema.required || !fieldSchema.optional) && typeof document[fieldName] === 'undefined') { // if ((fieldSchema.required || !fieldSchema.optional) && typeof document[fieldName] === 'undefined') {
validationErrors.push({ // validationErrors.push({
id: 'app.required_field_missing', // id: 'app.required_field_missing',
data: { fieldName }, // data: { fieldName },
}); // });
} // }
}); // });
// 5. still run SS validation for now for backwards compatibility // 5. still run SS validation for now for backwards compatibility
try { const validationContext = collection.simpleSchema().newContext();
collection.simpleSchema().validate(document); validationContext.validate(document);
} catch (error) {
// eslint-disable-next-line no-console if (!validationContext.isValid()) {
console.log(error); const errors = validationContext.validationErrors();
validationErrors.push({ errors.forEach(error => {
id: 'app.schema_validation_error', // eslint-disable-next-line no-console
data: { message: error.message }, // console.log(error);
validationErrors.push({
id: 'app.schema_validation_error',
data: error,
});
}); });
} }
@ -133,9 +137,6 @@ export const validateModifier = (modifier, document, collection, context) => {
if (!validationContext.isValid()) { if (!validationContext.isValid()) {
const errors = validationContext.validationErrors(); const errors = validationContext.validationErrors();
console.log('// validationContext');
console.log(validationContext.isValid());
console.log(errors);
errors.forEach(error => { errors.forEach(error => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
// console.log(error); // console.log(error);