简体   繁体   中英

Vue input required if others are non-empty

I have a form collecting an address. A fully specified address is okay, a completely empty one is okay, too, but a partial address is not okay.

I'm trying to express that this way:

<v-text-field :required="reqIn(address)" v-model="address.street" label="Street"></v-text-field>
<v-text-field :required="reqIn(address)" v-model="address.city" label="City"></v-text-field>
<v-text-field :required="reqIn(address)" v-model="address.state" label="State"></v-text-field>

// methods
reqIn (address) {
  // addresses may be all blank, or fully specified
  let totalLength = address.street.length + address.city.length + address.state.length + address.zip.length
  console.log(totalLength)
  return totalLength === 0
}

I can see the method getting called, and I can see the total length changing as I add and delete chars to the inputs, but when totalLength reaches 0, I expect the fields to become styled as required (if they are empty). But I don't see that happening. Any idea what I'm doing wrong?

这是因为如果您希望在更新某个变量时动态更改值,则方法reqIn在开始时调用一次,您可以使用computed propertieshttps ://v2.vuejs.org/v2/guide/ computed.html#Basic-示例

The styling is done through vuetify rules , and not the required directive. For each v-text-field , you can use the same rule if you want. Example code pen .

required directive documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM