MeEdu---开发插件之【数据校验】

在开发项目的时候,在做后台开发的时候,例如:新增,修改数据,这部分的数据就需要进行数据校验,在laravel框架里面,数据校验在 Request 里面做。

Meedu 插件的 request 约定在插件根目录 Http/Requests 目录存放:

拆分端口来进行划分,可以在Http / Request 目录新建:

Frontend
Backend Api

新建:Http / Requests / Backend / SchoolRequest.php

<?php
namespace Addons\School\Http\Requests\Backend;
use App\Http\Requests\Backend\BaseRequest;

class SchoolRequest extends BaseRequest
{
    public function authorize()
    {
        return true;
    }

    // rules
    public function rules()
    {
        return [
            'title' => 'required|max:50',
            'content' => 'min:2',
            'seo_title' => 'max:255',
            'seo_keywords' => 'max:255',
            'seo_description' => 'max:255',
            'published_at' => 'required',
            'category_id' => 'required',
            'short_description'=>'required',
            'ico'=>'required'
        ];
    }

    // messages
    public function messages()
    {
        return [
            'title.required' => '请输入标题',
            'title.max' => '标题的长度不能超过50个字符',
            'seo_title.max' => 'SEO标题的长度不能超过255个字符',
            'seo_keywords.max' => 'SEO关键字的长度不能超过255个字符',
            'seo_description.max' => 'SEO描述的长度不能超过255个字符',
            'published_at.required'=>'请选择发布时间',
            'category_id.required'=>'请选择文章分类',
            'short_description.required'=>'请填写简介',
            'ico.required'=>'缺少缩略图'
        ];
    }

    // filldata
    public function filldata()
    {
        $data = [
            'category_id' => $this->input('category_id',0),
            'title' => $this->input('title'),
            'subtitle' => $this->input('subtitle',''),
            'author' => $this->input('author',''),
            'source' => $this->input('source',''),
            'ico' => $this->input('ico',''),
            'pico' => (string)$this->input('pico',''),
            'short_description' => $this->input('short_description',''),
            'content' => $this->input('content',''),
            'seo_title' => $this->input('seo_title',''),
            'seo_keywords' => $this->input('seo_keywords',''),
            'seo_description' => $this->input('seo_description',''),
            'is_top' => (int)$this->input('is_top',0),
            'is_rec' => (int)$this->input('is_rec',0),
            'is_show' => (int)$this->input('is_show',1),
            'sort' => $this->input('sort',255),
            'href' => $this->input('href',''),
            'published_at' => $this->input('published_at')
        ];
        return $data;
    }
}

到此,后台数据校验完成。

posted @ 2022-07-05 14:04  帅到要去报警  阅读(58)  评论(0编辑  收藏  举报