Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Nested value of null marks form dirty on init #585

Closed
benjamingeorge opened this issue Aug 8, 2021 · 4 comments
Closed

Bug: Nested value of null marks form dirty on init #585

benjamingeorge opened this issue Aug 8, 2021 · 4 comments

Comments

@benjamingeorge
Copy link

I recently updated to the latest version of this library and noticed a new bug where my form isDirty = true on initialization.
I set values on my form that come back from an API response. Sometimes that value object has a nested object where some properties are null. When they are, MobxReactForm marks the form dirty on form initialization. But if those nested values are any other type (string, boolean, number) the form is not marked as dirty. It seems to be only a problem with nested values of type null. Top level values of null do not seem to be a problem, only nested ones.

Below is an example of the bug. See in the values object, the account: { id: null } causes the form to be isDirty = true on initialization which is a bug. If you change it to account: { id: "123" } or even account: { id: "" } , we get isDirty = false on initialization which is correct .

import React, { useState } from "react";
import { observer } from "mobx-react";
import dvr from "mobx-react-form/lib/validators/DVR";
import validatorjs from "validatorjs";
import MobxReactForm from "mobx-react-form";

const plugins = {
    dvr: dvr(validatorjs)
};

const hooks = {
    onSuccess(form) {
        alert("Form is valid! Send the request here.");
        // get field values
        console.log("Form Values!", form.values());
    },
    onError(form) {
        alert("Form has errors!");
        // get all form errors
        console.log("All form errors", form.errors());
    }
};

const schema = {
    fields: ["email"],
    labels: { email: "Email" },
    rules: { email: "required|email|string|between:5,25" },
    values: { email: "[email protected]", account: { id: null, name: "John" } }
};

const Form = observer(({ form }) => {
    console.log(form.isDirty); // true
    return (
        <form>
            <label htmlFor={form.$("email").id}>{form.$("email").label}</label>
            <input {...form.$("email").bind()} />

            <button type="submit" onClick={form.onSubmit}>
                Submit
            </button>
            <button type="button" onClick={form.onClear}>
                Clear
            </button>
            <button type="button" onClick={form.onReset}>
                Reset
            </button>

            <p>{form.error}</p>
        </form>
    );
});

export default () => {
    const [form] = useState(() => new MobxReactForm(schema, { plugins, hooks }));
    return <Form form={form} />;
};
@GorynychDragon
Copy link

Have the same problem. Does anyone have any thoughts on it?

@foxhound87 foxhound87 added the bug label Jun 19, 2022
@foxhound87
Copy link
Owner

foxhound87 commented Mar 13, 2023

I noticed you forgot to define the account field in then fields array.
I tested this with the correct field definition and worked (without null values).

@foxhound87 foxhound87 added fix and removed bug labels Mar 13, 2023
@foxhound87
Copy link
Owner

Related #531

foxhound87 added a commit that referenced this issue Mar 13, 2023
foxhound87 added a commit that referenced this issue Mar 18, 2023
foxhound87 added a commit that referenced this issue Mar 18, 2023
foxhound87 added a commit that referenced this issue Mar 26, 2023
fix: #585 #531

fix: #585, fix #531
@github-actions
Copy link

🎉 This issue has been resolved in version 5.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants