0

I have this form field called Template name and i have to check its uniqueness using validator addMethod function in my cakephp driven application.I can not understand how this works and how it will be validated if name is not unique.It is stoping form submission if name is unique.Below is my code.Please Help me with this and try to rectify my errors.

Client side Code

function validatefrm() {            
            var validate_frm = $("#EmailTemplateEditForm").validate({
                onkeyup: false,
            rules: {
                "data[EmailTemplate][template_name]": {
                    required:true,
                    validateTemplatename:true                        
                },
           },
           messages: {
                "data[EmailTemplate][template_name]": {
                    required:"<br/>Please Enter Template name !", 

                },
              }
            });
        jQuery.validator.addMethod("validateTemplatename",function (value,elememt){
        var inputElem = $('#EmailTemplateEditForm :input[name="data[EmailTemplate][template_name]"]'),
        data = { "template_name" : inputElem.val() },
        eReport = '';           
        $.ajax(
            {
            type: "POST",
            url: "<?php echo HTTP_ROOT.'email_templates/check_unique/'?>",
            dataType: "json",
            data: data,
            success: function(data) {                               
            if (data !== 'true'){
                return false;
            }
            else{                   
                return true;
            }
    },
    error: function(xhr, textStatus, errorThrown) {
        alert('ajax loading error... ... ');
        return false;
    }
}); 
}, ''); 

Server Side Coding in Controller

    function check_unique(){                
         if (isset($this->params['form']['template_name'])  && !empty($this->params['form']['template_name'])){
            $template_name = trim($this->params['form']['template_name']);           
            $totaltemplates = $this->EmailTemplate->find('count', array('conditions'=>array('EmailTemplate.template_name' => $template_name)));            
            if($totaltemplates == 0)    {               
            echo(json_encode(true));  //Name is Unique
            exit;
            }
            else{
            echo(json_encode(false)); //Name already exists         

            exit;
            }
        }
            else{
            echo "false"; //invalid post var
        }

       }
    }
  • You would not create a whole new method for this. You would simply use [the `remote` method](http://jqueryvalidation.org/remote-method/). – Sparky Jan 29 '15 at 20:12
  • Try changing the echo in the server side code to return. Return the value to the calling ajax method instead of echoing it – Colonel Mustard Jan 29 '15 at 20:14
  • @mcgowan.b, `echo` is absolutely correct for this application. – Sparky Jan 29 '15 at 20:21
  • Heard of remote method,but dont know how that works.Please help me if you can @Sparky – Rudraksh Panigrahi Jan 29 '15 at 20:24
  • I've always used `return` in my ajax queries and it never gives a problem. Why use `echo` over `return`? – Colonel Mustard Jan 29 '15 at 20:25
  • @mcgowan.b, in this case, his server-side code is correct as specified for using this plugin and its `remote` method. – Sparky Jan 29 '15 at 20:29
  • i checked the response on the client side and it is coming fine but dont know why the form isnt submitting if it is unique – Rudraksh Panigrahi Jan 29 '15 at 20:31
  • @RudrakshPanigrahi, follow the links I gave you. Linked to `remote` documentation in my first comment and linked to [duplicate question](http://stackoverflow.com/questions/16577120/jquery-validate-remote-method-usage) at top of your posting. – Sparky Jan 29 '15 at 20:31
  • Also click here to see hundred of other questions tagged with jQuery Validate (keyword "remote") with code: http://stackoverflow.com/search?q=%5Bjquery-validate%5D+remote – Sparky Jan 29 '15 at 20:32
  • 1
    @mcgowan.b, as per PHP documentation, `return` _"returns program control to the calling {PHP} module"_; which has nothing to do with Ajax or JavaScript. However, the PHP documentation states that `echo` will _"output one or more strings"_; which is exactly what we're doing... we are "outputting" a string that Ajax can pick up. – Sparky Jan 29 '15 at 20:40

0 Answers0