ThinkPHP 프레임 워 크 폼 검증 작업 방법
8738 단어 tp 프레임 워 크폼 검증
정적 검증
(1)홈/controller/경로 에서 Index 컨트롤 러 를 새로 만 듭 니 다.IndexController

IndexController.class.php 페이지
메모:정적 정의 방식 은 모델 클래스 를 정의 해 야 하기 때문에 D 함수 로 만 모델 을 예화 할 수 있 습 니 다.
create 방법 은 폼 이 제출 한 POST 데 이 터 를 자동 으로 검증 하 는 것 입 니 다.
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function yanzheng(){
$u= D("users");//
if(empty($_POST)){
$this->show();
}else{
if($u->create()){//
echo" ";
}else{
echo $u->getError();//
}
}
}
}
(2)view/Index 폴 더 아래 yanzheng.html 페이지 만 들 기
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> </title>
<script src="__ROOT__/Public/js/jquery-3.2.0.min.js"></script>
</head>
<body>
<h1> </h1>
<form action="__ACTION__" method="post">
<div> :<input type="text" name="uid" /></div>
<div> :<input type="password" name="pwd1"/></div>
<div> :<input type="password" name="pwd2"/></div>
<div> :<input type="text" name="age"/></div>
<div> :<input type="text" name="Email"/></div>
<div><input type="submit" value=" " /></div>
</form>
</body>
</html>
효과 그림:
(3)Model 층 에 정적 검증 을 쓰 는 인증:(경 로 는 그림 참조)

UsersModel.class.php
<?php
namespace Home\Model;
use Think\Model;
class UsersModel extends Model{
//
protected $_validate = array(
array("uid","require"," !"), //
array("pwd1","require"," !"),
array("pwd2","require"," !"),
array("pwd2","pwd1"," ",0,"confirm"), //
array("age","18,50"," ",0,"between"),
array("Email","email"," "),
);
}
순차 검증 효과 도:모두 비어 있 을 때 인증 을 누 르 십시오.

점프 를 잘 하 다
사용자 이름 입력,기타 입력 하지 않 을 때 이동
두 번 의 암호 입력 이 일치 하지 않 을 때 알려 줍 니 다.나이 가 범위 내 에 있 지 않 으 면 알려 줍 니 다.메 일 형식 이 올 바 르 지 않 을 때 알려 줍 니 다.



정확 한 형식 내용 입력 후
동적 검증
(1) IndexController.class.php 페이지
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function yz(){
$u= M("users");//
if(empty($_POST)){
$this->show();
}else{
$rules = array(
array("uid","require"," !"),
);
if($u->validate($rules)->create()){//
$this->ajaxReturn("ok","eval");
}else{
$this->ajaxReturn("no","eval");
}
}
}
}
(2)yz.html 페이지:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> </title>
<script src="__ROOT__/Public/js/jquery-3.2.0.min.js"></script>
</head>
<body>
<h1> </h1>
<form action="__ACTION__" method="post">
<div><input type="text" name="uid" id="uid" /><span id="ts"></span></div>
<div><input type="submit" value=" " /></div>
</form>
</body>
<script type="text/javascript">
$("#uid").blur(function(){
var uid = $(this).val();
$.ajax({
url:"__ACTION__",
data:{uid:uid},
type:"POST",
dataType:"TEXT",
success: function(data){
if(data.trim()=="ok")
{
$("#ts").html(" ");
}
else
{
$("#ts").html(" ");
}
}
});
})
</script>
</html>
효과 보기:
텍스트 상자 가 초점 을 잃 었 을 때:

텍스트 상자 에 내용 이 있 을 때 초점 을 잃 습 니 다:

위 에서 말씀 드 린 것 은 편집장 님 께 서 소개 해 주신 ThinkpHP 프레임 워 크 폼 검증 작업 방법 입 니 다.여러분 께 도움 이 되 셨 으 면 좋 겠 습 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 님 께 서 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!