【php】laravel学习笔记-表单验证

1 laravel restfull链接参数验证

说明

链接的参数默认是不能参与请求类的验证的,但restfull风格的参数基本集成在链接上,这时是需要验证的

环境参数

参数 说明
laravel 5.8 这个就不说明了

开始

生成请求文件, 命令行输入 php artisan make:request GenerateCreditPreOrder :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class GenerateCreditPreOrder extends FormRequest
{
protected function validationData()
{
return array_merge(
$this->route()->parameters(),
$this->input()
);

}

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [

];
}
}

然后在类文件中加入

1
2
3
4
protected function validationData()
{
return $this->route()->parameters();
}

方法来重写validationData

  • 解决思路来自

2 自己定义规则

 生成规则文件php artisan make:rule GoodsMustbeExits,会生成这个文件App/Rules/GoodsMustbeExits :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class GoodsMustbeExits implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{

}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
//
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return '//';
}
}

其中passes方法用来定义处理的逻辑,返回 booleanmessage方法则定义错误消息,attributevalue 为要验证的参数名和参数值。这是示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class GoodsMustbeExits implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{

}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$aop = $this->getAopInstance();
$Goodes = Goods::where('id', $value)
->where('status', 1)
->get();
return $Goodes->isEmpty() ? false : true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return '这个商品不存在';
}
}

然后在请求层进行验证(goods_id为上传的参数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\GoodsMustbeExits;

class GenerateCreditPreOrder extends FormRequest
{
protected function validationData()
{
return $this->route()->parameters();
}

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'goods_id' => ['required', new GoodsMustbeExits()]
];
}
}

坚持原创技术分享,您的支持将鼓励我继续创作!
0%