前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在yii2验证之前执行一些额外自定义验证

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

作者头像
双面人
发布2019-06-13 20:47:55
8060
发布2019-06-13 20:47:55
举报
文章被收录于专栏:热爱IT热爱IT热爱IT

第一种

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

第二种

<?php
/**
* User: yiqing
* Date: 14-9-15
* Time: 下午12:09
*/
 
namespace common\widgets;
 
use yii\web\View ;
use yii\widgets\Block ;
 
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) ;
}
}
<?php JsBlock::begin() ?>
<script>
$(function () {
jQuery('form#apitool').on('beforeValidate', function (e) {
if(验证规则){
 //不用执行什么操作,自动会提交
 }else{
 return false;
 }
});
</script>
<?php JsBlock::end() ?>
     (adsbygoogle = window.adsbygoogle || []).push({});  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档