Conditional validation for a form inputs

1.7k Views Asked by At

Starting off there is a append button that generates a row with 1 select box, 1 inputbox, and 4 checkboxes. The limit of adding this would be 1-10 rows at max. I have no idea how to make a jquery validation using for example http://formvalidation.io/ - or a standalone jquery code. The rules I would like to apply:

If the role chosen is user (not an admin) , I must validate that there is at least one checkbox checked and the user doesn't appears twice in the selections

The thing is I don't even know where to start from, can you point me any hints?

Live example :: http://jsfiddle.net/Yy2gB/131/

Append method onClick

$(document).ready(function(){
    var obj = {"1":"Admin istrator","2":"User2"};

    //$('.selectpicker').selectpicker();
    $(".addCF").click(function(){

        count = $('#customFields tr').length + 1;
        var sel = $('<select name="user'+count+'">');

        for(key in obj){
            // The key is key
            // The value is obj[key]
            sel.append($("<option>").attr('value',key).text(obj[key]));
        }

        $('.selectpicker').selectpicker();
        $("#customFields").append('<tr><td>'+sel[0].outerHTML
        +'</td><td><input class="form-control" class="valid_role"'
        +' data-fv-field="emails" type="text" name="role'+count
        +'" /></td><td><input type="checkbox" class="mycheckbox"'
        +' name="can_edit'+count+'"></td><td><input type="checkbox" '
        +'class="mycheckbox" name="can_read'+count+'"></td><td><input '
        +'type="checkbox" class="mycheckbox" name="can_execute'+count+'">'
        +'</td><td><input type="checkbox" class="mycheckbox" '
        +'name="is_admin'+count+'"></td><td><a href="javascript:void(0);"'
        +'class="remCF">Remove</a></td></tr>');
        $('.mycheckbox').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue'
        });
        $('.selectpicker').selectpicker();
    });
    $("#customFields").on('click','.remCF',function(){
    $(this).parent().parent().remove();
    });
});

HTML Form

<div class="col-md-12 col-lg-12"> 
  <table class="table table-user-information" id="customFields">
    <thead>
      <tr>
        <th class="standardTable_Header">User</th>
        <th class="standardTable_Header">Role</th>
        <th class="standardTable_Header">
          <span title="administrator projektu">Can read</span>
        </th>
        <th class="standardTable_Header">
           <span title="uprawnienie do edycji danych projektu">
             edit
           </span>
        </th>
        <th class="standardTable_Header">
          <span title="uprawnienie do odczytu danych projektu oraz przypisanych do niego zadań">
           excute
          </span>
        </th>
        <th class="standardTable_Header">
          <span title="uprawnienie do edycji danych projektu">
            admin
          </span>
         </th>
      </tr>
    </thead>
  <tbody>
    <button type="button" class="btn btn-default addCF">
      Append
    </button>
    </div>
  </tbody>
</table>
1

There are 1 best solutions below

3
On

Using this jQuery Validation Plugin and through this demo, We could do the following:
Assumption: Roles check boxes must have at least one checked if - and only if - the select tag have the value User2 1- wrap your table with form tag that has a submit button:

<div class="col-md-12 col-lg-12">
    <form id="myform">
        <table class="table table-user-information" id="customFields">
            ...
        </table>
        <input type="submit" value="submit" />
    </form>
</div>

2- We need to edit the checkboxes html to make them all have the same name but with different values, for example:

<input type="checkbox" class="mycheckbox" name="ourRoles" value="can_edit' ...>
<input type="checkbox" class="mycheckbox" name="ourRoles" value="can_read' ...>

and so on.

3- Add the following script:

// just for the demos, avoids form submit
    jQuery.validator.setDefaults({
        debug: true,
        success: "valid"
    });
    $("#myform").validate({
        rules: {
            ourRoles: {
                required: function () {
                    if ($("select[name=user2]").val() == 2) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }
    }); 

See all together with this fiddle