I have a model Paper
. Paper can have different sizes and colors but sizes only relate to some colors and so on. I have a pivot table set up where sizes correspond to colors. I'm using form request validation and I have models with belongsToMany
relationships set up for sizes and colors. When I do Paper:create
I want to send an error if a color and size are a mismatch based on that pivot table. Is there a built-in "Laravel way" of achieving this or should I just loop through the pivot table when color and size are set?
使用数据透视表进行Laravel请求验证
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- duanqing2209 2015-11-17 09:55关注
The most "Laravely" way that I can think of, is using the form request class like so:
<?php namespace App\Http\Requests; use App\Http\Requests\Request; class PaperFormRequest extends Request { public function authorize() { return true; } public function rules() { // rules here } protected function getValidatorInstance() { $validator = parent::getValidatorInstance(); $validator->after(function() use ($validator) { // logic for detecting mismatches goes here // To add validation-errors, simply do: $validator->errors()->add( 'colorMistmatch', 'This color does not go with this paper' ); return $validator; } } }
Once errors have been added to the validator-instance, you can display them in your blade-views by doing this:
@if ($errors->has('colorMistmach')) {{ $errors->first('colorMistmach') }} @endif
解决 无用评论 打赏 举报
悬赏问题
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
- ¥15 ETLCloud 处理json多层级问题
- ¥15 matlab中使用gurobi时报错
- ¥15 这个主板怎么能扩出一两个sata口
- ¥15 不是,这到底错哪儿了😭
- ¥15 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么