Meteor-Community-Packages / meteor-autoform

AutoForm is a Meteor package that adds UI components and helpers to easily create basic forms with automatic insert and update events, and automatic reactive validation.
MIT License
1.44k stars 328 forks source link

Reactive validation for nested schema #1683

Closed gilizeevi closed 4 years ago

gilizeevi commented 5 years ago

Hello.

I'm currently facing an issue with reactive validation errors not appearing on nested subschema fields. When adding AutoForm.debug, the errors are logged but does not show on the form.

Example: JS

const Tasks = new Mongo.Collection('tasks');
const SubTaskSchema = new SimpleSchema({
    subtitle: {
        type: String,
        label: "Subtitle",
        required: true
    },
    price: {
        type: String,
        label: "Price",
        required: true
    }
}, { tracker: Tracker });

const TaskSchema = new SimpleSchema({
    title: {
        type: String,
        label: "Title",
    },
    subTask: {
        type: Object
    },
    'subTask.$': {
        type: SubTaskSchema
    }
}, { tracker: Tracker });
Tasks.attachSchema(TaskSchema);

HTML {{> quickForm collection=getCollection id="insertTaskForm" type="insert"}} OR

 {{#autoForm collection=getCollection id="insertTaskForm" type="insert"}}
        <fieldset>
            <legend>Add a task</legend>
            {{> afQuickField name='title'}}
            {{> afQuickFields name='subTask.$'}}
        </fieldset>
        <button type="submit" class="btn btn-primary">Insert</button>
{{/autoForm}}

In the quickform example, when subtask fields are empty, a validation error of 'subtasks is required' does appear, but not for subtasks fields.

Reactive validation errors of nested subscehma did work with previous versions, before Tracker was added. Any idea what i'm doing wrong and how to solve it?

jamgold commented 5 years ago

I notice the same behavior. Has anybody figured out how to solve this? The onError hook of the form has all validation errors in validationContext._validationErrors but just doesn't display it for a nested schema

evolross commented 4 years ago

I get this too. Occurs when using either quickForm and autoForm.

Linking to your work-around that solves this problem for now.

aldeed commented 4 years ago

Duplicate of https://github.com/aldeed/simple-schema-js/issues/359, which has a PR in review / discussion