Git Product home page Git Product logo

Comments (7)

PiemP avatar PiemP commented on September 22, 2024 1

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.

horprogs avatar horprogs commented on September 22, 2024

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.

PiemP avatar PiemP commented on September 22, 2024

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.

horprogs avatar horprogs commented on September 22, 2024

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.

PiemP avatar PiemP commented on September 22, 2024

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.

horprogs avatar horprogs commented on September 22, 2024

Fair enough. You could try these 2 solutions:

  1. 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 
        });

        ...
  })
  1. 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.

PiemP avatar PiemP commented on September 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.