Jak upravit funkci validate pro mé pužití

jAkErCZ
Člen | 321
+
-3
-

Zdravím,

Ze začátku chci říct že js není má silná stránka a proto se obracím na profíky tady :)

Takže mám funkci validate a ta je:

function validateForm() {
    // This function deals with validation of the form fields
    var x, y, i, valid = true;

    x = document.getElementsByClassName("tab");
    y = x[currentTab].getElementsByTagName("input");
    // A loop that checks every input field in the current tab:
    for (i = 0; i < y.length; i++) {
        // If a field is empty...
        if (y[i].value == "") {
            // add an "invalid" class to the field:
            y[i].className += " invalid";
            // and set the current valid status to false
            valid = false;
        }
    }

    // If the valid status is true, mark the step as finished and valid:
    if (valid) {
        document.getElementsByClassName("step")[currentTab].className += "finish";
    }
    return valid; // return the valid status
}

Ale mám problém že mám input který obstarává nahrávání souborů ale pokud do něj něco nenahraji hodí mu to class invalid ale jak ošetřit aby to inputy které dělají file ignorovaly?

<div class="input-group image-preview">
      <span class="input-group-btn">

       <button type="button" class="btn btn-warning image-preview-clear" style="display:none;">
             <span class="glyphicon glyphicon-remove"></span> Vymazat
       </button>

        <div class="btn btn-default image-preview-input">
             <span class="glyphicon glyphicon-folder-open"></span>
             <span class="image-preview-input-title">Nahrát přílohu</span>
             <input n:name="file" type="file" accept="application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"/> <!-- rename it -->
        </div>
        </span>
</div>

Zde přikládám inputy na file.

Díky všem za pomoc.

Gappa
Nette Blogger | 199
+
+2
-

Mělo by stačit doplnit podmínku na typ inputu, něco jako:

if (y[i].type !== "file" && y[i].value == "") {
...
}

Edit: Proč ale radši nepoužít nette forms validaci?

Editoval Gappa (19. 11. 2019 15:43)

jAkErCZ
Člen | 321
+
0
-

Gappa napsal(a):

Mělo by stačit doplnit podmínku na typ inputu, něco jako:

if (y[i].type !== "file" && y[i].value == "") {
...
}

Edit: Proč ale radši nepoužít nette forms validaci?

Díky tohle pomohlo no nepoužívám nette validaci i když bych rád kvůli tomu že používám toto:

Form step

Jelikož přesně tohle jsem potřeboval a nevím jak pomocí nette bych tohle udělal.

A ještě sem narazil na jednu věc že nevím proč ale formulář se jakoby nepošle i když mám:

if (currentTab >= x.length) {
       // ... the form gets submitted:
       $("#loader").show();
       alert("Jede");
       document.getElementById("inquiryForm").submit();
       return false;
   }

Použije se loader i vypíše alert ale ten formulář se prostě nepošle… v čem dělám chybu?

Opět děkuji za pomoc

Gappa
Nette Blogger | 199
+
0
-

jAkErCZ napsal(a):
A ještě sem narazil na jednu věc že nevím proč ale formulář se jakoby nepošle i když mám:

if (currentTab >= x.length) {
       // ... the form gets submitted:
       $("#loader").show();
       alert("Jede");
       document.getElementById("inquiryForm").submit();
       return false;
   }

Použije se loader i vypíše alert ale ten formulář se prostě nepošle… v čem dělám chybu?

Opět děkuji za pomoc

Nemáš náhodou v tom formuláři více odesílacích tlačítek? Zavolání metody submit na formuláři použije první tlačítko (v kódu).

jAkErCZ
Člen | 321
+
0
-

Gappa napsal(a):

jAkErCZ napsal(a):
A ještě sem narazil na jednu věc že nevím proč ale formulář se jakoby nepošle i když mám:

if (currentTab >= x.length) {
       // ... the form gets submitted:
       $("#loader").show();
       alert("Jede");
       document.getElementById("inquiryForm").submit();
       return false;
   }

Použije se loader i vypíše alert ale ten formulář se prostě nepošle… v čem dělám chybu?

Opět děkuji za pomoc

Nemáš náhodou v tom formuláři více odesílacích tlačítek? Zavolání metody submit na formuláři použije první tlačítko (v kódu).

Mám tam přesně to co mají i ve zdroji :D

var imageIndex = 0;
/* Custom inquiry js */
var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the current tab

function showTab(n) {
    // This function will display the specified tab of the form...
    var x = document.getElementsByClassName("tab");
    x[n].style.display = "block";
    //... and fix the Previous/Next buttons:
    if (n == 0) {
        document.getElementById("prevBtn").style.display = "none";
    } else {
        document.getElementById("prevBtn").style.display = "inline";
    }
    if (n == (x.length - 1)) {
        //document.getElementById("nextBtn").setAttribute('type', 'submit');
        document.getElementById("nextBtn").innerHTML = "Získat cenu a dárek";
    } else {
        document.getElementById("nextBtn").innerHTML = "Další otázka <i class='icon-circle-arrow-right'></i>";
    }
    //... and run a function that will display the correct step indicator:
    fixStepIndicator(n)
}

function nextPrev(n) {
    // This function will figure out which tab to display
    var x = document.getElementsByClassName("tab");
    // Exit the function if any field in the current tab is invalid:
    if (n == 1 && !validateForm()) return false;
    // Hide the current tab:
    x[currentTab].style.display = "none";
    // Increase or decrease the current tab by 1:
    currentTab = currentTab + n;
	var numberStep = 1 + currentTab;
	var next_class = ' next_inquiry' + numberStep;
    document.getElementById("number").innerHTML = numberStep;
    document.getElementById("next").className =
        document.getElementById("next").className.replace
        ( /(?:^|\s)next_inquiry([0-9]{0,8})/g , next_class );
    // image slider
    //imageIndex = (imageIndex + images.length -1) % (images.length);
    $("#image").attr('src', images[currentTab]);
    if (currentTab >= x.length) {
        // ... the form gets submitted:
        $("#loader").show();
        alert("Jede");
        document.getElementById("inquiryForm").submit();
        return false;
    }
    // Otherwise, display the correct tab:
    showTab(currentTab);
}

function validateForm() {
    // This function deals with validation of the form fields
    var x, y, i, valid = true;

    x = document.getElementsByClassName("tab");
    y = x[currentTab].getElementsByTagName("input");
    // A loop that checks every input field in the current tab:
    for (i = 0; i < y.length; i++) {
        // If a field is empty...
        if (y[i].type !== "file" && y[i].value == "") {
            // add an "invalid" class to the field:
            y[i].className += " invalid";
            // and set the current valid status to false
            valid = false;
        }
    }

    // If the valid status is true, mark the step as finished and valid:
    if (valid) {
        document.getElementsByClassName("step")[currentTab].className += "finish";
    }
    return valid; // return the valid status
}

function fixStepIndicator(n) {
    // This function removes the "active" class of all steps...
    var i, x = document.getElementsByClassName("step");
    for (i = 0; i < x.length; i++) {
        x[i].className = x[i].className.replace("active", "");
    }
    //... and adds the "active" class on the current step:
    x[n].className += " active";
}

A tady mát ty tlačítka

<div id="next" class="col-lg-4 col-md-4 next_inquiry0">
                                                               <button n:name="submit" class="button button-medium button-rounded" type="button" id="nextBtn" onclick="nextPrev(1)">Další otázka </button> <br />
                                                           <p><a href="javascript:void(0);" id="prevBtn" onclick="nextPrev(-1)"><u>Vrátit se k přechozí otázce</u></a></p>
                                                       </div>

Takže nechápu proč jim to tam jede a mě ne :D

Editoval jAkErCZ (19. 11. 2019 19:41)

David Matějka
Moderator | 6445
+
0
-

a ma ten <form> spravne id? nevypisuje se neco v developer konzoli?

jAkErCZ
Člen | 321
+
0
-

David Matějka napsal(a):

a ma ten <form> spravne id? nevypisuje se neco v developer konzoli?

Ano má

<form n:name="inquiryForm" id="inquiryForm" class="validate-form">
David Matějka
Moderator | 6445
+
0
-

a odpoved na druhou otazku je..?

jAkErCZ
Člen | 321
+
-1
-

David Matějka napsal(a):

a odpoved na druhou otazku je..?

Taky ne ale už sem to vyřešil.

if (n == (x.length - 1)) {
        document.getElementById("nextBtn").setAttribute("type","submit");
        document.getElementById("nextBtn").innerHTML = "Získat cenu a dárek";
    } else {
        document.getElementById("nextBtn").setAttribute("type","button");
        document.getElementById("nextBtn").innerHTML = "Další otázka <i class='icon-circle-arrow-right'></i>";
    }

A už to funguje :D Díky tedy všem za pomoc vážím si toho :)