在yii2验证之前执行一些额外自定义验证

第一种

 
  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);

第二种

 
  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() ?>

转载于:https://my.oschina.net/u/3371661/blog/3061227

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值