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;
                }
        }
    }
  • 相关阅读:
    javascript实战演练,制作新按钮,‘新窗口打开网站’,点击打开新窗
    P1332 血色先锋队
    P4643 [国家集训队]阿狸和桃子的游戏
    T149876 公约数
    P1462 通往奥格瑞玛的道路
    P1083 借教室
    Tribles UVA
    Fence Repair POJ
    Crossing Rivers
    关于一轮
  • 原文地址:https://www.cnblogs.com/hltswd/p/5096849.html
Copyright © 2011-2022 走看看