2014-04-21 31 views
0

我在zend 2中使用jquery驗證。 除minlength外,所有其他驗證類型正在工作。
這裏是形式的代碼:最小長度與jQuery驗證不起作用

$this->add(array(
     'name' => 'usr_name', 

     'attributes' => array(
      'type' => 'text', 
      'id' => 'firstName', 
      'class' => 'form-control', 

     // jquery validation rules: 
      'required' => 'true', 
      'minlength' => '2', 
      'maxlength' => '20', 
     ), 
     'options' => array(
      'label' => 'Name', 
     ), 
    )); 

這裏是js代碼:

$('.form-validation').each(function() { 
    $(this).validate({ 


    errorElement: "span", 
    errorClass: "help-block", 

    highlight: function(element) { 
     $(element).closest('.control-group').removeClass('success').addClass('error'); 
     $(element).attr('style', "border-radius: 5px; border:#FF0000 1px solid;"); 
    }, 
    unhighlight: function(element) { 
     $(element).addClass('valid').closest('.control-group').removeClass('error').addClass('success'); 
     $(element).attr('style', "border-radius: 4px; border:1px solid #ccc;"); 
    }, 
    errorPlacement: function (error, element) { 
     $(error).attr('style', "color: #FF0000;"); 
     if (element.prop('type') === 'checkbox') { 
      error.insertAfter(element.parent()); 
     } else { 
      error.insertAfter(element); 
     } 
    } 

    }); 
}); 

UPDATE:
這是生成的HTML:

<input name="usr_name" type="text" required="required" maxlength="20" id="firstName" class="form-control" value="" aria-required="true"> 

看本文:

Strange, how can it be possible!

+0

不顯示Zend代碼,而是顯示瀏覽器看到的相關HTML標記。 – Sparky

+0

然後很明顯,這是Zend如何創建'input'元素的問題,與jQuery Validate無關。 – Sparky

+0

'maxlength'無法正常工作......注意它也跳過創建'maxlength'屬性。顯然,似乎某些HTML5屬性是Zend生成的問題。 – Sparky

回答

1

我發現下面的article

這很奇怪如何ZF2人們認爲這是一個邏輯!

所以對於一個醜陋的解決辦法我注入minlengthattribute在以下方式中需要的元素:

$('.minLength').each(function() { 
    $(this).attr("minlength", "8"); 
}); 

哪裏minLength是任選地加入到需要的元素給jQuery的工作的機會在一類他們正確!