在yii2驗證以前執行一些額外自定義驗證

第一種php

 
  1. <?php
  2. $js = <<<JS
  3. $("#button").click(function(e){
 
  1. if(驗證規則){
    $("form#{$model->formName()}").submit();
    }else{
    return false;
    }
 
  1. })
  2. JS;
  3. $this->registerJs($js);

第二種web

 
  1. <?php
  2. /**
  3. * User: yiqing
  4. * Date: 14-9-15
  5. * Time: 下午12:09
  6. */
  7.  
  8. namespace common\widgets;
  9.  
  10. use yii\web\View ;
  11. use yii\widgets\Block ;
  12.  
  13. class JsBlock extends Block{
  14.  
  15. /**
  16. * @var null
  17. */
  18. public $key = null;
  19. /**
  20. * @var int
  21. */
  22. public $pos = View::POS_END ;
  23. /**
  24. * Ends recording a block.
  25. * This method stops output buffering and saves the rendering result as a named block in the view.
  26. */
  27. public function run()
  28. {
  29. $block = ob_get_clean();
  30. if ($this->renderInPlace) {
  31. throw new \Exception("not implemented yet ! ");
  32. // echo $block;
  33. }
  34. $block = trim($block) ;
  35. /*
  36. $jsBlockPattern = '|^<script[^>]*>(.+?)</script>$|is';
  37. if(preg_match($jsBlockPattern,$block)){
  38. $block = preg_replace ( $jsBlockPattern , '${1}' , $block );
  39. }
  40. */
  41. $jsBlockPattern = '|^<script[^>]*>(?P<block_content>.+?)</script>$|is';
  42. if(preg_match($jsBlockPattern,$block,$matches)){
  43. $block = $matches['block_content'];
  44. }
  45.  
  46. $this->view->registerJs($block, $this->pos,$this->key) ;
  47. }
  48. }
 
  1. <?php JsBlock::begin() ?>
  2. <script>
  3. $(function () {
  4. jQuery('form#apitool').on('beforeValidate', function (e) {
  5. if(驗證規則){
    //不用執行什麼操做,自動會提交
    }else{
    return false;
    }
  6. });
  7. </script>
  8. <?php JsBlock::end() ?>
相關文章
相關標籤/搜索