1

bear with me, you don't need to watch the video, nor read the github project, just for completeness:

from the laracasts about vue js and form submit

see github project here

 <form @submit.prevent="doSubmit()"  @keydown="form.errors.clear($event.target.name)">

this will clear the error of an element in case of "keydown" event. Now this won't work for <select> as users usually click or change for sure

so what I want to do is something like

<form @submit.prevent="doSubmit()"  @keydown@change="form.errors.clear($event.target.name)">

but vue js does not seem to have syntax to do the same thing for multiple events

so only solution is:

 <form @submit.prevent="doSubmit()" @change="form.errors.clear($event.target.name)"  @keydown="form.errors.clear($event.target.name)">

I repeat the question:

how to do the same thing for 2 different events?

@scroll@click any 2 events

the actual solution to my problem is secondary

5
  • Your final example is fine. What's wrong with it? Commented Apr 23, 2018 at 2:25
  • well DRY: en.wikipedia.org/wiki/Don%27t_repeat_yourself just imagine you have 5 events doing the same thing. To me its a smell. If I have to reread my code, I have to reread all event handlers to see if they are really exactly the same. Because if there is a small little difference there, good luck man. If it is the same, it should be called the same e.g. @click@change=".." that way you are sure at a glance it is the same, if it is not the same, it should be called like in the last example Commented Apr 23, 2018 at 2:30
  • 1
    this issue was discussed in github.com/vuejs/vue/issues/6457. looks like there isn't a solution for this yet. Commented Apr 23, 2018 at 2:35
  • This does not violate DRY. Commented Apr 23, 2018 at 2:35
  • 1
    @Ohgodwhy i cite: Violations of DRY are typically referred to as WET solutions, which is commonly taken to stand for either "write everything twice", "we enjoy typing" or "waste everyone's time" yes, that is exactly WET Commented Apr 23, 2018 at 2:43

1 Answer 1

1

Instead of

@keydown@change="form.errors.clear($event.target.name)"

use

@input="form.errors.clear($event.target.name)"

It will work for any input event occurs.

Sign up to request clarification or add additional context in comments.

1 Comment

this does not answer the question. OP does not want "any" input events to trigger the method, he/she wants two specific events to cause a trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.