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

2019-06-13 20:47:55 浏览数 (1)

第一种

代码语言:javascript复制
<?php
$js = <<<JS
$("#button").click(function(e){
代码语言:javascript复制
if(验证规则){
 $("form#{$model->formName()}").submit();
 }else{
 return false;
 }
代码语言:javascript复制
})
JS;
$this->registerJs($js);

第二种

代码语言:javascript复制
<?php
/**
* User: yiqing
* Date: 14-9-15
* Time: 下午12:09
*/
 
namespace commonwidgets;
 
use yiiwebView ;
use yiiwidgetsBlock ;
 
class JsBlock extends Block{
 
/**
* @var null
*/
public $key = null;
/**
* @var int
*/
public $pos = View::POS_END ;
/**
* Ends recording a block.
* This method stops output buffering and saves the rendering result as a named block in the view.
*/
public function run()
{
$block = ob_get_clean();
if ($this->renderInPlace) {
throw new Exception("not implemented yet ! ");
// echo $block;
}
$block = trim($block) ;
/*
$jsBlockPattern = '|^<script[^>]*>(. ?)</script>$|is';
if(preg_match($jsBlockPattern,$block)){
$block = preg_replace ( $jsBlockPattern , '${1}' , $block );
}
*/
$jsBlockPattern = '|^<script[^>]*>(?P<block_content>. ?)</script>$|is';
if(preg_match($jsBlockPattern,$block,$matches)){
$block = $matches['block_content'];
}
 
$this->view->registerJs($block, $this->pos,$this->key) ;
}
}
代码语言:javascript复制
<?php JsBlock::begin() ?>
<script>
$(function () {
jQuery('form#apitool').on('beforeValidate', function (e) {
if(验证规则){
 //不用执行什么操作,自动会提交
 }else{
 return false;
 }
});
</script>
<?php JsBlock::end() ?>
     (adsbygoogle = window.adsbygoogle || []).push({});  

0 人点赞