zoukankan      html  css  js  c++  java
  • thinkphp自动验证方法的使用

    建一个表单:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <form action="{:U('Index/check')}" method='post'>
            <table>
                <tr>
                    <td>用户名:</td><td><input type="text" name='name'></td>
                </tr>
                <tr>
                    <td>密码:</td><td><input type="password" name='password'></td>
                </tr>
                <tr>
                    <td>性别:</td><td><input type="radio" name='sex' vlue='1'><input type="radio" name='sex' value='0'></td>
                </tr>
                <tr>
                    <td>年龄:</td><td><input type="text" name='age'></td>
                </tr>
                <tr>
                    <td>邮箱:</td><td><input type="text" name='email'></td>
                </tr>
                <tr>
                    <td>个人网页:</td><td><input type="text" name='mypage'></td>
                </tr>
                <tr>
                    <td>薪水:</td><td><input type="text" name='salary'></td>
                </tr>
            </table>
            <input type="submit" value='保存添加'>
        </form>
    </body>
    </html>

    建一个模型,UserModel.php

    <?php 
    namespace HomeModel;
    use ThinkModel;
    
    class UserModel extends Model{
        protected $_validate = array(
         array('name','require','帐号不能为空!'), 
         array('password','require','密码不能为空'), 
      );
    }
     ?>

    在控制器中调用方法

    <?php
    namespace HomeController;
    use ThinkController;
    class IndexController extends Controller {
        public function index(){
            $this->display();
            }
        public function check(){
    
             $user = D('User'); // 实例化User对象
       
            if (!$user->create()){
                 // 如果创建失败 表示验证没有通过 输出错误提示信息
                 exit($user->getError());
            }else{
                 // 验证通过 可以进行其他数据操作 
                echo 1;
                }
        }
    }
  • 相关阅读:
    三次请求(读-改-读)引出nibernate 一级缓存
    算法竞赛入门经典第一、二章摘记
    uva 10905 Children's Game
    uva 11205 The broken pedometer
    uva 10160 Servicing stations
    uva 208 Firetruck
    uva 167 The Sultan's Successors
    zoj 1016 Parencodings
    uva 307 Sticks
    uva 216 Getting in Line
  • 原文地址:https://www.cnblogs.com/hltswd/p/5096849.html
Copyright © 2011-2022 走看看