亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過在控制器中添加新數據來動態更新驗證規則

通過在控制器中添加新數據來動態更新驗證規則

PHP
呼喚遠方 2021-06-11 14:23:28
我正在開發一個基于 Web 的應用程序,技術堆棧是:VueJS,用于表示層,Laravel(PHP)用于RESTFUL API服務,以及一個名為neo4j 的基于 nosql 圖的數據庫 。這是問題上下文,我有一個名為Post的模型,此處定義的屬性共享所有帖子類型,因此它們都將擁有它:use NeoEloquent;use Spatie\Sluggable\HasSlug;use Spatie\Sluggable\SlugOptions;use Vinelab\NeoEloquent\Eloquent\SoftDeletes;class Post extends NeoEloquent{protected $label = 'Post';protected $dates = ['created_at', 'updated_at', 'deleted_at'];/** * The attributes that are mass assignable. * * @var array */protected $fillable = [    'title',    'slug',    'body',    'status',    'image',    'published_at',    'read_time',    'isModerate',    'link',    'external_id'];/**protected $hidden = ['remember_token'];/** * relations */public function authors(){    return $this->belongstoMany('App\User', 'AUTHOR');}public function getSlugOptions(): SlugOptions{    return SlugOptions::create()->generateSlugsFrom('title')->saveSlugsTo('slug');}//Post typepublic function contentType(){  return $this->hasOne('App\ContentType','IS_OF_TYPE');}}PostType:這里的類型可以是文件、鏈接、事件等。例如:[name=>'Event','description'=>'description','slug'=>'event']class PostType extends NeoEloquent{    protected $label='ContentType';    protected $dates=['created_at','updated_at','deleted_at'];    protected $fillable=[      "name",      "description",      "slug"    ];//Ce input contentype sera associe a plusieurs input fields    public function customFields(){      return $this->belongsToMany('App\CustomField',"HAS_FIELD");    }    public function post(){      return $this->belongsToMany('App\Post','IS_OF_TYPE');    }}最后是CustomField模型:class CustomField extends NeoEloquent{  protected $label="CustomType";  protected $dates=["created_at","updated_at","deleted_at"];  protected $fillable=[    "field_name",    "field_type",    "field_order",    "validation_rules"  ];  public function contentTypes(){    return $this->belongsToMany('App\CustomField','HAS_FIELD');  }}
查看完整描述

1 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

您可以使用請求中的數據動態構建驗證規則:


use Illuminate\Http\Request;


class StorePost extends FormRequest

{

    public function authorize()

    {

        return true;

    }


    public function rules(Request $request)

    {

        $rules = [

            'title' => 'required|string|min:1',

            'body' => 'required',

            'status' => 'required',

            'image' => 'sometimes|image',

            'published_at' => 'required',

            'link' => 'required_if:type,==,RSS|required_if:type,==,LINK',

            'external_id' => 'sometimes'

        ];


        if($typeSlug = $request->get('type'))

        {

            $type = PostType::where('slug', $typeSlug)->with('customFields');


            // map custom fields into a `['name' => 'rules']` format

            $customFieldRules = $type->customFields->mapWithKeys(function($customField) {

                return [$customField->field_name => $customField->validation_rules];

            });


            $rules = array_merge($rules, $customFieldRules);

        }


        return $rules;

    }

}


查看完整回答
反對 回復 2021-06-13
  • 1 回答
  • 0 關注
  • 153 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號