我想验证表单的输入是否存在另一个集合中的公共字段。有关如何实现这一目标的任何建议吗?
编辑:添加了代码示例和上下文
我简单地将其构建为原型基础架构。
架构:
Transactions.schema = new SimpleSchema({
key: { // When inserted this need to be an existing key
type: Number,
unique: true,
min: 0,
max: 1000000000,
},
employee: { // When inserted this need to be an existing employee
type: String,
regEx: /^[0-9]+[NCV]$/,
min: 4,
max: 128,
},
});
Keys.schema = new SimpleSchema({
code: {
type: Number,
unique: true,
min: 0,
max: 1000000000,
},
name: {
type: String,
min: 3,
max: 128,
},
});
Employees.schema = new SimpleSchema({
empId: {
type: String,
regEx: /^[0-9]+[NCV]$/,
min: 4,
max: 128,
},
});
目前,我只是使用验证和upsert方法来简化故障排除和实验。
export const upsertTransaction = new ValidatedMethod({
name: 'transactions.upsert',
validate: new SimpleSchema({
_id: { type: String, optional: true },
key: { type: Number, optional: true }, // I think this is where I need to validate existing key
employee: { type: String, optional: true }, // I think this is where I need to validate existing employee
}).validator(),
run(document) {
return Transactions.upsert({ _id: document._id }, { $set: document });
},
});
反应形式:
export default class TransactionEditor extends React.Component {
componentDidMount() {
transactionEditor({ component: this });
setTimeout(() => { document.querySelector('[name="key"]').focus(); }, 0);
}
render() {
const { trans } = this.props;
return (<form
ref={ form => (this.transactionEditorForm = form) }
onSubmit={ event => event.preventDefault() }
>
<FormGroup>
<ControlLabel>Key</ControlLabel>
<FormControl
type="number"
name="key"
defaultValue={ trans && trans.key }
placeholder="1234"
/>
</FormGroup>
<FormGroup>
<ControlLabel>Employee</ControlLabel>
<FormControl
type="text"
name="employee"
defaultValue={ trans && trans.employee }
placeholder="12345V"
/>
</FormGroup>
<Button type="submit" bsStyle="success">
{ trans && trans._id ? 'Save Changes' : 'Add Transaction' }
</Button>
</form>);
}
}
我使用https://github.com/themeteorchef/base作为框架,供参考。