0

This is my first post here, so be gentle. :)

I have a channel form that utilizes a javascript (sliding.form.js) to assist with per-fieldset validation. If I group all my form questions into diff fieldsets, when a user clicks a different "tab", it validates that the fields required have content.

http://tympanus.net/codrops/2010/06/07/fancy-sliding-form-with-jquery/

This form works just fine when I utilize a standard "":

<form id="formElem" name="formElem" action="" method="post">

But, if I change the form to EE code, the "per fieldset" validation goes all wonky-like:

{exp:channel:form include_jquery="no" class="new_customer" id="formElem" error_handling="inline" name="formElem" channel="pets" logged_out_member_id="1" status="Pending" return="pet/review_submit_thanks"}

Any thoughts?

The javascript in question is:

$(function() {
    /*
    number of fieldsets
    */
    var fieldsetCount = $('#formElem').children().length;

    /*
    current position of fieldset / navigation link
    */
    var current     = 1;

    /*
    sum and save the widths of each one of the fieldsets
    set the final sum as the total width of the steps element
    */
    var stepsWidth  = 0;
    var widths      = new Array();
    $('#steps .step').each(function(i){
        var $step       = $(this);
        widths[i]       = stepsWidth;
        stepsWidth      += $step.width();
    });
    $('#steps').width(stepsWidth);

    /*
    to avoid problems in IE, focus the first input of the form
    */
    $('#formElem').children(':first').find(':input:first').focus(); 

    /*
    show the navigation bar
    */
    $('#navigation').show();

    /*
    when clicking on a navigation link 
    the form slides to the corresponding fieldset
    */
    $('#navigation a').bind('click',function(e){
        var $this   = $(this);
        var prev    = current;
        $this.closest('ul').find('li').removeClass('selected');
        $this.parent().addClass('selected');
        /*
        we store the position of the link
        in the current variable 
        */
        current = $this.parent().index() + 1;
        /*
        animate / slide to the next or to the corresponding
        fieldset. The order of the links in the navigation
        is the order of the fieldsets.
        Also, after sliding, we trigger the focus on the first 
        input element of the new fieldset
        If we clicked on the last link (confirmation), then we validate
        all the fieldsets, otherwise we validate the previous one
        before the form slided
        */
        $('#steps').stop().animate({
            marginLeft: '-' + widths[current-1] + 'px'
        },500,function(){
            if(current == fieldsetCount)
                validateSteps();
            else
                validateStep(prev);
            $('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();    
        });
        e.preventDefault();
    });

    /*
    clicking on the tab (on the last input of each fieldset), makes the form
    slide to the next step
    */
    $('#formElem > fieldset').each(function(){
        var $fieldset = $(this);
        $fieldset.children(':last').find(':input').keydown(function(e){
            if (e.which == 9){
                $('#navigation li:nth-child(' + (parseInt(current)+1) + ') a').click();
                /* force the blur for validation */
                $(this).blur();
                e.preventDefault();
            }
        });
    });

    /*
    validates errors on all the fieldsets
    records if the Form has errors in $('#formElem').data()
    */
    function validateSteps(){
        var FormErrors = false;
        for(var i = 1; i < fieldsetCount; ++i){
            var error = validateStep(i);
            if(error == -1)
                FormErrors = true;
        }
        $('#formElem').data('errors',FormErrors);   
    }

    /*
    validates one fieldset
    and returns -1 if errors found, or 1 if not
    */
    function validateStep(step){
        if(step == fieldsetCount) return;

        var error = 1;
        var hasError = false;
        $('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input:not(button)').each(function(){
            var $this       = $(this);
            var valueLength = jQuery.trim($this.val()).length;

            if(valueLength == ''){
                hasError = true;
                $this.css('background-color','#FFEDEF');
            }
            else
                $this.css('background-color','#FFFFFF');    
        });
        var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
        $link.parent().find('.error,.checked').remove();

        var valclass = 'checked';
        if(hasError){
            error = -1;
            valclass = 'error';
        }
        $('<span class="'+valclass+'"></span>').insertAfter($link);

        return error;
    }

    /*
    if there are errors don't allow the user to submit
    */
    $('#registerButton').bind('click',function(){
        if($('#formElem').data('errors')){
            alert('Please correct the errors in the Form');
            return false;
        }   
    });
});
4
  • Here's a sample form that is NOT working: woodwardpetsitting.com/pet/test and here is a sample form that IS working: woodwardpetsitting.com/pet/test_working You'll notice the issue thus: The first two fields are required. On the working one, if you choose a new "tab" (diff fieldset), the button does not validate and turns red, but is "green" on the broken one. If I just use html "form" items, it validates and works ok. Mar 21, 2015 at 10:50
  • How are you including your fields:by hand or using {field:my_field} ? Mar 21, 2015 at 12:31
  • In the two examples above, none of the fields are actually really EE fields, they're just example fields from the from. But the actual form I'm working on integrating this with, I'm using <li> <label for="title"><strong>Pet Name: </strong></label> <input class="required" type="text" name="title" id="title" value="{title}" size="50" maxlength="100" onkeyup="liveUrlTitle();" placeholder="Rex McFurry" required/> <span class="form_hint">Your pet's name. If there are two pets, Put them both in here.</span> </li> Mar 21, 2015 at 13:20
  • So, is that "by hand"? :-) Mar 21, 2015 at 13:31

0

Your Answer

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

Browse other questions tagged or ask your own question.