基于API响应,突出显示了特定的输入红/无效

发布于 2025-01-30 23:33:12 字数 1365 浏览 0 评论 0原文

基于API依据

“在此处输入图像描述”

我抓住了它并显示了一个红色警报,但最重要的是,我也想触发红色亮点 name Input

我的输入

<v-text-field
    :class="{ 'invalid' : invalidName }"
    dense
    outlined
    v-model="name"
    :rules="form.rules.name"
    label="Name"
    required
></v-text-field>

CSS

>>> .invalid .v-label {
    color: #b13737 !important;
    caret-color: #ff5252 !important;
}

>>> .invalid input {
    color: #b13737 !important;
    caret-color: #ff5252 !important;
}

>>> .invalid .v-text-field--outlined fieldset {
    border: 2px solid #b13737 !important;
}

捕获

this.invalidName = true //turned it to true when error detected 
this.alert = true
this.alertColor = 'red'
this.alertMessage = response.data.error.detail

结果

我只能将标签和文本的颜色和颜色变

”在此处输入图像说明”

Based on the API reponse

enter image description here

I caught it and display a red alert, but on top of that, I would like to also trigger red highlight to the name input.

enter image description here

my input

<v-text-field
    :class="{ 'invalid' : invalidName }"
    dense
    outlined
    v-model="name"
    :rules="form.rules.name"
    label="Name"
    required
></v-text-field>

css

>>> .invalid .v-label {
    color: #b13737 !important;
    caret-color: #ff5252 !important;
}

>>> .invalid input {
    color: #b13737 !important;
    caret-color: #ff5252 !important;
}

>>> .invalid .v-text-field--outlined fieldset {
    border: 2px solid #b13737 !important;
}

catch it

this.invalidName = true //turned it to true when error detected 
this.alert = true
this.alertColor = 'red'
this.alertMessage = response.data.error.detail

result

I can only turn label and the color of the text red

enter image description here

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

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

发布评论

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

评论(2

や三分注定 2025-02-06 23:33:12

有时候,Vuetify的CSS有些棘手。尝试这样做:

.theme--light.v-text-field--outlined:not(.v-input--is-focused):not(.v-input--has-state)
  > .v-input__control
  > .v-input__slot
  fieldset {
  border: 2px solid #b13737 !important
}

“

Override vuetify's css can be a little tricky sometimes. Try doing it like this:

.theme--light.v-text-field--outlined:not(.v-input--is-focused):not(.v-input--has-state)
  > .v-input__control
  > .v-input__slot
  fieldset {
  border: 2px solid #b13737 !important
}

Preview

墟烟 2025-02-06 23:33:12

不要与Vuetify输入错误状态CSS手动混乱。而是尝试使用错误(boolean)和error-messages(字符串或数组)((如果您不隐藏详细信息)) 。

如果您可以在响应中解析错误,我建议构建一个inputErrors对象,其中键是字段的名称和值是(可能)错误消息。特别是如果您在一个提交中显示多个错误字段。

您可以用.split(':':')data.err.Error.Error.Errer.Erferion进行解析,然后使用索引[0]作为上述inputErrors对象的键和index [1]值。

然后,您可以通过编程使用文本字段的错误道具。

<v-text-field
  :error="inputErrors['name']"
  :error-messages="inputErrors['name']"
>

您可能不需要两者。如果错误消息不是null,则可能是错误状态为true。

Don't mess with the Vuetify input error state CSS manually. Instead try to make use of the error (boolean) and error-messages (string or array) ((if you are not hiding details)) props of the text-field.

If you can parse the error in the response, I would recommend constructing an inputErrors object, where the keys are the names of the fields and values are (possibly) the error message(s). Especially if you would show multiple error fields in one submission.

You could parse data.error.reference with .split(':') and use index [0] as the key of the above inputErrors object and index [1] as the value.

Then you can use the error props of the text-field programatically.

<v-text-field
  :error="inputErrors['name']"
  :error-messages="inputErrors['name']"
>

It's possible you don't need both. It might be the case if error-messages is not null the error state is true.

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