李炎恢ThinkPHP講義ThinkPHP--自動驗證[上][12]

來源:互聯網
上載者:User

標籤:not   ted   ror   成功   back   nbsp   namespace   sql語句   confirm   

控制層IndexController

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        //$food = M(‘food‘);        //兩個表聯集查詢        //var_dump($food->field(‘a.food_id,b.user_id‘)->table(‘__FOOD__ a, __PERSON__ b‘)->select());        $food =D(‘Food‘);        var_dump($food->sql_one()->sql_two()->select());        $this->display();    }        //控制器create()方法自動調用驗證     //如果是直接 POST 過來的,直接使用 create()方法即可(假裝下面是post過來的)    public function login(){    $person = D(‘Person‘);     $data[‘name‘] = ‘11‘;     $data[‘password‘] = ‘1‘;     $data[‘repassword‘] = ‘2‘;    if ($person->create($data)) {        echo ‘所有資料驗證成功!‘;         } else {         var_dump($person->getError());         }    }}

PersonModel層

<?phpnamespace Home\Model;use Think\Model;class PersonModel extends Model {    //批量驗證     protected $patchValidate = true;    //自動驗證內容    protected $_validate = array(        array(‘name‘, ‘require‘, ‘使用者名稱不得為空白!‘, 0, ‘regex‘,3),        //array(‘name‘, ‘email‘, ‘郵箱格式不正確!‘),        //array(‘name‘, ‘url‘, ‘URL格式不合法!‘),        //array(‘name‘, ‘currency‘, ‘貨幣格式不正確!‘),        //array(‘name‘, ‘zip‘, ‘郵遞區號必須是六位正整數!‘),        //array(‘name‘, ‘number‘, ‘必須是正整數!‘),        //array(‘name‘, ‘integer‘, ‘必須是整數!‘),        //array(‘name‘, ‘double‘, ‘必須是浮點數!‘),        //array(‘name‘, ‘english‘, ‘必須是純英文!‘),        //array(‘name‘, ‘/^\d{3,6}$/‘, ‘必須是3-6位純數字‘, 0, ‘regex‘),        //array(‘name‘, ‘李炎恢‘, ‘傳遞過來的值不相等‘, 0, ‘equal‘),        //array(‘name‘, ‘李炎恢‘, ‘傳遞過來的必須不相等‘, 0, ‘notequal‘),        array(‘repassword‘,‘password‘, ‘兩個值不相等‘, 0, ‘confirm‘),        array(‘name‘, ‘checkLength‘, ‘使用者名稱必須在 3-5 位‘, 0, ‘callback‘, 3, array(3,5)),    );        //自訂驗證方法    protected function checkLength($str,$min,$max) {        preg_match_all("/./", $str, $matches);        $len = count($matches[0]);         if ($len < $min || $len > $max) {         return false;         } else {         return true;         }     }    //將sql語句封裝在模型定義類裡    protected $_scope =array(        ‘sql_one‘=>array(            ‘where‘=>array(‘food_id‘=>array(‘GT‘,3)),        ),        ‘sql_two‘=>array(            ‘order‘=>array(‘unit_price‘=>‘DESC‘),            ‘limit‘=>5,        ),    );    //函數建立一個新的對象 PHP內建的函數    public function __construct(){        parent::__construct();        var_dump(‘\Home‘);    }        }

 

李炎恢ThinkPHP講義ThinkPHP--自動驗證[上][12]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.