Laravel Vuejs 实战:开发知乎 (7)验证问题表单字段
上一节代码中已经实现 下面代码中的validate内部配置就是:
1 public function store(Request $request)
2 {
3 //
4 $data = $request->validate([
5 'title' => 'required|min:8',
6 'content' => 'required|min:8',
7 ]);
8
9 $data['user_id'] = auth()->user()->id;
10
11 $question = Question::create($data);
12
13 return redirect()->route('questions.show', $question);
14 }
注:validate方法详见:表单验证
且 create.blade.php中也已经使用
1 <p class="text text-danger"> @error('title') {{ $message }} @enderror </p>
2
3 <p class="text text-danger"> @error('content') {{ $message }} @enderror </p>
4
实现错误提示。不过差一个错误后,留存用户上一次填写的内容,输出给用户用于再次修改,
然后代码如下:
1 @extends('layouts.app')
2 @section('content')
3 @include('vendor.ueditor.assets')
4 <div class="container">
5 <div class="row">
6 <div class="col-md-8 col-md-offset-2">
7 <div class="card">
8 <div class="card-header">
9 发布问题
10 </div>
11 <div class="card-body">
12 <form action="{{ route('questions.store') }}" method="post">
13 {{--注意要有csrftoken--}}
14 @csrf
15 <div class="form-group">
16 <label for="title">标题</label>
17 <input type="text" name="title" class="form-control" placeholder="标题" id="title"
18 value="{{ old('title') }}">
19 <p class="text text-danger"> @error('title') {{ $message }} @enderror </p>
20 </div>
21 <!-- 编辑器容器 -->
22 <script id="container" name="content" type="text/plain">{!! old('content') !!}</script>
23 <p class="text text-danger"> @error('content') {{ $message }} @enderror </p>
24 <!--发布按钮-->
25 <button type="submit" class="btn btn-primary mt-2 float-md-right">发布问题</button>
26 </form>
27 </div>
28 </div>
29 </div>
30 </div>
31 </div>
32
33 <!-- 实例化编辑器 -->
34 <script type="text/javascript">
35 var ue = UE.getEditor('container');
36 ue.ready(function () {
37 ue.execCommand('serverparam', '_token', '{{ csrf_token() }}'); // 设置 CSRF token.
38 });
39 </script>
40
41 @endsection
当然如果验证要求比较多,可以执行:
1 php artisan make:request QuestionStoreRequest
然后将QuestionController.php文件中验证部分注释掉,我们将使用依赖注入QuestionStoreRequest实例的方式处理请求验证,
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Http\Requests\QuestionStoreRequest;
6 use App\Models\Question;
7 use App\User;
8 use Illuminate\Http\Request;
9
10 class QuestionController extends Controller
11 {
12 public function __construct()
13 {
14 $this->middleware('auth', ['except' => ['index', 'show']]);//非注册用户只能查看不能编辑添加更改删除
15 }
16
17 /**
18 * Display a listing of the resource.
19 *
20 * @return \Illuminate\Http\Response
21 */
22 public function index()
23 {
24 //
25
26 }
27
28
29 /**
30 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
31 */
32 public function create()
33 {
34 //
35 return view('questions.create');
36 }
37
38
39 /**
40 * @param QuestionStoreRequest $request
41 * @return \Illuminate\Http\RedirectResponse
42 */
43 public function store(QuestionStoreRequest $request)//依赖注入QuestionStoreRequest实例
44 {
45 //
46 // $data = $request->validate([
47 // 'title' => 'required|min:8',
48 // 'content' => 'required|min:28',
49 // ]);
50 $data = $request->all();
51 $data['user_id'] = auth()->user()->id;
52
53 $question = Question::create($data);
54
55 return redirect()->route('questions.show', $question);
56 }
57
58
59 /**
60 * @param Question $question
61 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
62 */
63 public function show(Question $question)
64 {
65 //
66 return view('questions.show', compact('question'));
67 }
68
69 /**
70 * Show the form for editing the specified resource.
71 *
72 * @param int $id
73 * @return \Illuminate\Http\Response
74 */
75 public function edit($id)
76 {
77 //
78 }
79
80 /**
81 * Update the specified resource in storage.
82 *
83 * @param \Illuminate\Http\Request $request
84 * @param int $id
85 * @return \Illuminate\Http\Response
86 */
87 public function update(Request $request, $id)
88 {
89 //
90 }
91
92 /**
93 * Remove the specified resource from storage.
94 *
95 * @param int $id
96 * @return \Illuminate\Http\Response
97 */
98 public function destroy($id)
99 {
100 //
101 }
102 }
103
104
将表单的验证移动到生成的Requests/QuestionStoreRequest.php文件内,代码如下:
1 <?php
2
3 namespace App\Http\Requests;
4
5 use Illuminate\Foundation\Http\FormRequest;
6
7 class QuestionStoreRequest extends FormRequest
8 {
9 /**
10 * Determine if the user is authorized to make this request.
11 *
12 * @return bool
13 */
14 public function authorize()
15 {
16 //一般判断当前用户是否有权发送本实例的请求,先默认设置为true,不过可以先写一个判断当前用户是否是请求中的用户来判断返回值
17
18 return true;
19 }
20
21 /**
22 * Get the validation rules that apply to the request.
23 * 验证规则
24 * @return array
25 */
26 public function rules()
27 {
28 return [
29 //
30 'title' => 'required|min:8',
31 'content' => 'required|min:28',
32 ];
33 }
34
35 public function messages()
36 {
37 // return parent::messages(); // TODO: Change the autogenerated stub
38 //错误提示
39 return [
40 'title.required' => '标题必须要输入的哦!',
41 'title.min' => '标题长度要大于8个的哦!',
42 'content.required' => '内容必须要输入的哦!',
43 'content.min' => '内容长度必须要大于28个的哦!',
44 ];
45 }
46 }
47
48
Laravel Vuejs 实战:开发知乎 (7)验证问题表单字段的更多相关文章
- Laravel Vuejs 实战:开发知乎 (6)发布问题
1.view部分: 安装一个扩展包:Laravel-UEditor composer require "overtrue/laravel-ueditor:~1.0" 配置 添加下面 ...
- Laravel Vuejs 实战:开发知乎 (10)使用 Select2 优化话题选择
1.添加选择Topic 使用Select2,如何安装Select2 ,具体使用实例 Select2 and Laravel: Ajax Autocomplete 及 Loading data remo ...
- Laravel小项目之第4节 Laravel-通过表单实现新增及操作状态提示功能
第4节 Laravel-通过表单实现新增及操作状态提示功能 4.1 显示新增表单视图 4.2 通过模型实现新增 4.3 操作状态提示 4.1 显示新增表单视图 修改边栏的链接 \resources\v ...
- 第一百八十四节,jQuery-UI,验证注册表单
jQuery-UI,验证注册表单 html <form id="reg" action="123.html" title="会员注册" ...
- validate验证注册表单
点击预览; <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- js验证form表单示例
js验证form表单示例 检测测试了js表单验证,无jQuery(简单的功能有时无需jQuery版本) js代码如下: <script type="text/javascript& ...
- 第十七篇 JS验证form表单
JS验证form表单 这节课做一个实际的,项目里会遇到的东西,例如登录页面,我们输入‘用户名’和‘密码’或者‘手机号’还有‘验证码’等等,它都会做一个前端验证,比如验证码,是6位有效数字组成,那么 ...
- Laravel 表单验证创建“表单请求”实现自定义请求类
按照文档创建表单请求自定义类以后,调用总是403页面,咨询大佬说: public function authorize() { // 在表单验证类的这个方法这里要返回true,默认返回false,这个 ...
- Yii2 关闭和打开csrf 验证 防止表单多次重复提交
原文地址:http://blog.csdn.net/terry_water/article/details/52221007 1.在Yii2配置中配置所有:所有的controller都将关闭csrf验 ...
随机推荐
- 牛客多校第七场H Pair 数位dp理解
Pair 题意 给出A B C,问x取值[1,A]和y取值[1,B]存在多少组pair<x,y>满足以下最小一种条件,\(x \& y >c\),\(x\) xor \(y& ...
- MyEclipse把普通的项目变成hibernate项目
- js时间相关操作
取值 this.divEndDate.attr("value",new Date()); var date = new Date(); date.getYear(): 取2 ...
- MyBatis-单表的增删改查(CRUD)操作
在学习MyBatis的单表的增删改查操作之前,还是再次熟悉下MyBatis这个框架,只有对其熟悉的情况下,才能很好的使用,灵活的开发. MyBatis优点: ...
- <<甄嬛传>>后感
2020年疫情和休假的时间里,刷了几部喜欢的电视剧,从<好先生>孙红雷和车晓的现代剧,到刷了至少6遍的<新三国>后,一直想了解司马懿和曹丕,接着就到了<大军师司马懿之军师 ...
- 压缩包安装mysql8.0
在使用django的时候遇到一个错误,就是用脚本改变数据库的时候,发现mysql的版本不够,需要的版本应该大于5.8,而我的只有5.5,就很烦,恰好我之前有8.0的压缩包.(mysql重装已经不下十次 ...
- 手机大厂必备测试技能-GMS 认证
GMS认证背景 在之前的一篇文章有给各位小伙伴们科普过关于GMS的作用,http://www.lemfix.com/topics/266 "墙"内的小伙伴可能很少会用到这样的服务, ...
- Django路由层、视图层
一.路由匹配: 第一个参数是正则表达式,匹配规则按照从上往下一次匹配,匹配到一个后立即停止 urlpatterns = [ url(r'^admin/', admin.site.urls), url( ...
- python网络编程(通过tcp或者udp协议通信)
1.基于tcp协议传送文件: 客户端: import socketimport osimport jsonimport structclient = socket.socket()client.con ...
- python之路之生成器和的迭代器
生成器的基本原理 生成器实现xrange 迭代器