未定义的(在承诺中)typeerror:无法读取未定义的属性(阅读' $消息')

发布于 2025-02-03 04:34:17 字数 2825 浏览 4 评论 0原文

我正在尝试在Vuejs网站上使用Vuelidate库进行我的寄存器表单验证。显示错误消息的方式是使用$ errors属性来显示它,并且可以很好地显示它我在现场输入。

这是模板代码:

  <form @submit.prevent="register()" class="mb-3">
            <div class="form-floating mb-3">
              <input
                type="email"
                class="form-control rounded-0"
                id="floatingInput"
                placeholder="[email protected]"
                v-model="state.form.email"
                @blur="v$.form.email.$touch"
              />
              <label for="floatingInput">Email</label>
              <span v-if="v$.form.email.$error" style="color: red">
                {{ v$.form.$errors[0].$message }}
              </span>
            </div>
import store from "../store";
import router from "../router";
import { required, email,  minLength, minValue  } from "vuelidate/lib/validators";
import useVuelidate from "@vuelidate/core";
import { helpers} from "@vuelidate/validators";
import { reactive, computed } from "vue";
export default {
  name: "Register",
  setup() {
    const state = reactive({
      form: {
        email: "",
        password: "",
        confirm: "",
        username: "",
      },
    });
    const myRequired = helpers.withMessage(
    'This field is required',
    required,
    );
    const minLengthvalidate = helpers.withMessage(
      'Password required at leaset 8 characters',
      minLength,
    );

    const minVlaueValidate = helpers.withMessage(
      'Password must include at least one special symbols',
      minValue,
    );

    const emailErrorMessage = helpers.withMessage(
      'This is not a valide email address',
      email
    );
    
    const rules = computed(() => {
      return { 
        form: {
          email: {
            required:myRequired,
            email:emailErrorMessage,
          },
          password: {
            required:myRequired,
            minLength:minLengthvalidate,
            minValue:minVlaueValidate,
          },
        },
      };
    });

    const v$ = useVuelidate(rules, state);

    return {
      state,
      v$,
    };
  },
  methods: {
    register() {
      this.v$.$validate();
      console.log(this.v$);
      if (!this.v$.$error) {
        store
          .dispatch("register", this.form)
          .then((res) => {
            //router.push('/verifying')
            console.log("still here");
          })
          .catch((error) => {
            console.log(error);
          });
      } else {
        console.log("Field required");
        console.log(this.v$.form.$errors[0].$message)
       
      }
    },
  },
};
</script>

I'm trying to use the vuelidate library for my register form validation in my vuejs website. The way of displaying the error message is using the $errors properties to display it and it work fine but I encounter a problem in the console showing that Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '$message') when I input in the field.

Here is the template code:

  <form @submit.prevent="register()" class="mb-3">
            <div class="form-floating mb-3">
              <input
                type="email"
                class="form-control rounded-0"
                id="floatingInput"
                placeholder="[email protected]"
                v-model="state.form.email"
                @blur="v$.form.email.$touch"
              />
              <label for="floatingInput">Email</label>
              <span v-if="v$.form.email.$error" style="color: red">
                {{ v$.form.$errors[0].$message }}
              </span>
            </div>
import store from "../store";
import router from "../router";
import { required, email,  minLength, minValue  } from "vuelidate/lib/validators";
import useVuelidate from "@vuelidate/core";
import { helpers} from "@vuelidate/validators";
import { reactive, computed } from "vue";
export default {
  name: "Register",
  setup() {
    const state = reactive({
      form: {
        email: "",
        password: "",
        confirm: "",
        username: "",
      },
    });
    const myRequired = helpers.withMessage(
    'This field is required',
    required,
    );
    const minLengthvalidate = helpers.withMessage(
      'Password required at leaset 8 characters',
      minLength,
    );

    const minVlaueValidate = helpers.withMessage(
      'Password must include at least one special symbols',
      minValue,
    );

    const emailErrorMessage = helpers.withMessage(
      'This is not a valide email address',
      email
    );
    
    const rules = computed(() => {
      return { 
        form: {
          email: {
            required:myRequired,
            email:emailErrorMessage,
          },
          password: {
            required:myRequired,
            minLength:minLengthvalidate,
            minValue:minVlaueValidate,
          },
        },
      };
    });

    const v$ = useVuelidate(rules, state);

    return {
      state,
      v$,
    };
  },
  methods: {
    register() {
      this.v$.$validate();
      console.log(this.v$);
      if (!this.v$.$error) {
        store
          .dispatch("register", this.form)
          .then((res) => {
            //router.push('/verifying')
            console.log("still here");
          })
          .catch((error) => {
            console.log(error);
          });
      } else {
        console.log("Field required");
        console.log(this.v$.form.$errors[0].$message)
       
      }
    },
  },
};
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2025-02-10 04:34:17

改变条件:

 <span v-if="v$.form.email.$error" style="color: red">
      <template v-if="v$.form.$errors && v$.form.$errors.length">
         {{ v$.form.$errors[0].$message }}
      </template>
  </span>

Change the condition to this:

 <span v-if="v$.form.email.$error" style="color: red">
      <template v-if="v$.form.$errors && v$.form.$errors.length">
         {{ v$.form.$errors[0].$message }}
      </template>
  </span>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文