友情链接模型添加验证数据

通过模型添加及验证数据

(1)模型控制器admin/model/Link.php

<?php
 namespace app\admin\model;

use think\Model;

class Link extends Model
{

}

(2)admin/controller/Link.php

<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Link as Links;
class Link extends Controller
{
    public function lst()
    {
    //   $cateres=\think\Db::name('cate')->select();
    //   $this->assign('cateres',$cateres);
        return $this->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            $links=new links;
            $links->data([
                'title'=>input('title'),
                'url'=>input('url'),
                'desc'=>input('desc'),
            ]);
            $validate = \think\Loader::validate('Link');
            if($validate->check(links)){//这一行验证有点问题
                // $db=\think\Db::name('Link')->insert($data);
                 if($links->save()){
                    return $this->success('添加链接成功!','lst');
                }else{
                    return $this->error('添加链接失败!');
                }
            }else{
                return $this->error($validate->getError());
            }
            return;
        }
        return $this->fetch();
    }

(3)admin/validate/Link.php

<?php

namespace app\admin\validate;

use think\Validate;

class Link extends Validate
{
    protected $rule = [
        'title'  =>  'require|max:25|unique:link',
        'url'  =>  'require',
    ];

protected $message  =   [
    'title.require' => '链接标题不能为空! ', 
    'title.unique' => '链接标题不能重复! ', 
    'title.max' => '链接标题不能大于25位! ', 
    'url.require' => '链接地址不能为空! ', 
];//按照代码次序验证

}
?>
2020-10-10_202000.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容