Comments (7)
Done! I have implemented this code:
let tabValid = false
_tabs = document.querySelectorAll('a[data-bs-toggle="tab"]');
_tabs.forEach(function (tab, index, arr) {
tab.addEventListener('hide.bs.tab', function (event) {
//if validation is ok allow tab moving
if (tabValid) {
//do something on valid form case
tabValid = false;
return
}
//to lock switch beetwen tabs use the code below
event.preventDefault()
//validate
_validator.revalidate().then((isValid) => {
if (isValid) {
tabValid = true
event.relatedTarget.click() // related target contains the tab clicked
}
});
})
Thanks for your support
from just-validate.
Hey, why do you need to wait for the promise? I think you could use onSuccess
, onFail
callbacks, set your state variable and based on this enable and disable your tabs. If you create a small example, probably I could help
from just-validate.
Sorry but I don't want disable tabs...it could make interface less understandable by the user and close a way to interact with the form.
Like I said before, I can't work on the click event of the tabs because I have to redesign a too much things in the code and this function is not a requirement that have all of this priority.
The only event that remain is show.bs.tab
(or hide.bs.tab
) that is called before the destination tab is made visible. The thing that I try to do is this:
tab.addEventListener('show.bs.tab', function (event) {
//pause event execution
//call revalidate function
_validator.revalidate()
.then((isValid) => {
//if form is valid resume event execution
});
I don't have experience with javascript Promise
and it's difficult to me understand how to use them. For what I have read is not possible to do what I want to do in the example.
I thank you for your interest.
from just-validate.
from your example show.bs.tab
is a custom event. Usually you could prevent it, like event.preventDefault()
, but then you should trigger it manually, like document.querySelector('#yourtab').click()
, will it work?
from just-validate.
Thank you @horprogs,
I have tested this code:
tab.addEventListener('hide.bs.tab', function (event) {
//to lock switch beetwen tabs use the code below
event.preventDefault()
//validate
_validator.revalidate().then((isValid) => {
if (isValid)
event.relatedTarget.click() // related target contains the tab clicked
});
...
})
but if the form is valid it goes in loop (event, preventDefault(), form valid, call click, event, preventDefault(), form valid, call click ...).
from just-validate.
Fair enough. You could try these 2 solutions:
- Using
defaultPrevented
property, like:
tab.addEventListener('hide.bs.tab', function (event) {
if (event.defaultPrevented) {
// do nothing, allow the clicking
return
}
//to lock switch beetwen tabs use the code below
event.preventDefault()
//validate
_validator.revalidate().then((isValid) => {
if (isValid)
event.relatedTarget.click() // related target contains the tab clicked
});
...
})
- Using an additional variable:
let tabValid = false
tab.addEventListener('hide.bs.tab', function (event) {
if (tabValid) {
// do nothing, allow the clicking
return
}
//to lock switch beetwen tabs use the code below
event.preventDefault()
//validate
_validator.revalidate().then((isValid) => {
if (isValid) {
tabValid = true
event.relatedTarget.click() // related target contains the tab clicked
}
});
...
})
from just-validate.
Nothing 😔...if you switch fast between tabs seems tabValid
doesn't have the right value... I see the debugger execute tabValid = false;
, if form is valid, but when I click fast over the next tab the check if(tabValid)
report the true value instead of false.
from just-validate.
Related Issues (20)
- Method unlockForm HOT 2
- Recaptcha Validation HOT 1
- Additional Rules/Validators on Groups (Not just required) HOT 3
- Cannot use this library in core typescript project HOT 3
- Provide unminified js version with named export HOT 2
- Undefined error message HOT 4
- showErrors not working on groups HOT 1
- Reset form input value
- when input hidden deal with script HOT 1
- Number rule should support locale number format
- Trigger validation and error messages via javascript HOT 1
- Silent revalidate HOT 2
- docs: provide more detailed explanation with images or code
- Compatibility with Vue 3? HOT 4
- Radio input required and other inputs HOT 1
- Can't have this regexp to work whatsoever
- Form Won't Submit HOT 1
- showErrors: Field not found if using diferrent selector in addField()
- Skip validation rules for fields that are hidden with CSS HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from just-validate.