Conditional remote verification

I have a form that I validate through the jQuery Validate plugin. Everything is working fine, except for one thing.

I have a field that I use, a remote rule, and I use AJAX to check if there is already an application record with the same name. I need to perform this check only if the user changes the value in the field.

This is the verification code I have:

$('#createapp').validate({
    rules: {
      appname: {
        minlength: 8,
        required: true,
        remote: {
                url: "<?php echo base_url();?>app/application/check_app",
                type:"POST",
                async: false,
                data:  {
                appname: function() {
                return $("#appname").val();
          }
        }  
      }
      },
      apptitle: {
        required: true
      }
    },
    messages: {
        appname: {
      remote: "Application with same name already exists"
      }
     },
    highlight: function(label) {
        $(label).closest('.control-group').addClass('error');
    },
    success: function(label) {
        label
            .text('OK!').addClass('valid')
            .closest('.control-group').addClass('success');
    }
  });

I need to do this:

Perform a remote check only if the user changes the text in the application name field. I tried to use depends, but I can not get it to work. This is basically what I tried:

remote: {
            depends: function(element){
            return ($('#appname').val() != "<?php echo trim($application[0]->app_name) ?>" );
            }
+3
source share
2 answers

Finally I used a workaround. Which looks like this:

  • <input type="hidden" name="orgappletname" id="orgappletname"
      value="<?php echo $applet[0]->applet_name ?>">
    
  • jQuery , - . , ajax-, .

    $('#appletname').blur(function(){
     if($(this).val() != $('#orgappname').val()){
        $.ajax({
            url: "<?php echo base_url() . "apl/applet/check_applet" ?>",
            data: "appletname=" + $(this).val(),
            type: "POST",
            success: function (msg) {
            }
        });
      }
      });
    

, , .

0

@user1017268

, , , , ;-) , - (coffeescript):

getEmailRule = () ->
  which = window.location.pathname.split('/')[1]
  if which == 'renew' then required: true
  else remote: { url: "/user/exists", type: "POST", async: false } 

isValid = $('#subscriptionForm').validate({
  rules: {
    email:      getEmailRule()
    password:   "strongPass"
    orderTotal: "gtZero"
  }
}).form()

, (.. ), email must exist, ; -)

0

All Articles