我为图片表单创建了验证规则。
它在存储方法上工作正常,但我不希望在更新时需要图像字段,因为我可能只更新标题,例如。
class ImageRequest extends Request
{
/**
* Rules array
*/
protected $rules = [
'title' => 'required|string|between:3,60',
'alt' => 'sometimes|string|between:3,60',
'image' => 'required|image|max:4000|dimensions:min_width=200,min_height=200',
];
/**
* 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 $this->rules;
}
}
对于唯一验证,我们可以添加自定义查询条件:
'email' => Rule::unique('users')->ignore($user->id, 'user_id')
或
'email' => Rule::unique('users')->where(function ($query) {
return $query->where('account_id', 1);
})
对于必需来实现类似的东西是否干净?
仅对新图片应用必需。
答案 0 :(得分:2)
您可以在规则
中使用switch语句 public function rules()
{
switch ($this->method()) {
case 'GET':
case 'DELETE': {
return [];
}
case 'POST': {
return [
'first_name'=>'required',
'last_name'=>'required',
'email'=>'required|email|unique:users,email,'.$this->id,
'password'=>'',
'dob'=>'required',
'phone_one'=>'required',
'phone_two'=>'required',
//'user_role'=>'required',
// 'profile_image'=>'required'
];
}
case 'PUT':
case 'PATCH': {
return [
];
}
default:break;
}
另外你可以像更新时使用condtion yuo有这样的基础,你可以检查它的更新或插入,因为插入你没有id所以
答案 1 :(得分:0)
创建另一个将Request类扩展到更新控制器操作中的类
class UpdateImageRequest extends Request
{
/**
* Rules array
*/
protected $rules = [
'title' => 'required|string|between:3,60',
'alt' => 'sometimes|string|between:3,60'
];
/**
* 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 $this->rules;
}
}
答案 2 :(得分:0)
更好的方法是在Laravel 5.5验证中使用nullable
参考Docs
验证字段可能为空。这特别有用 在验证原语时,如字符串和整数可以 包含空值。
class ImageRequest extends Request
{
/**
* Rules array
*/
protected $rules = [
'title' => 'required|string|between:3,60',
'alt' => 'nullable|string|between:3,60',
'image' => 'nullable|image|max:4000|dimensions:min_width=200,min_height=200',
];
/**
* 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 $this->rules;
}
}
然而,我最近使用了图像,它对我来说就像魅力一样。试一试!
答案 3 :(得分:0)
在这种情况下最简单的方式是另一种方式。默认情况下,有更新规则,如果它的商店添加需要,如下所示:
class ImageRequest extends Request
{
/**
* Rules array
*/
protected $rules = [
'title' => 'required|string|between:3,60',
'alt' => 'sometimes|string|between:3,60',
'image' => 'image|max:4000|dimensions:min_width=200,min_height=200',
];
/**
* 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()
{
$rules = $this->rules;
if ($this->isMethod('POST')) {
$rules['image'] = 'required|' . $rules['image']
}
return $rules;
}
}
答案 4 :(得分:-2)
我找到了解决方案。
我将图片重命名为文件。
商店上更新和 homestead.app/images 的路线 homestead.app/images/1 >所以 $ image 属性将 $ this-> image = 1 更新和 $ this-&gt 商店上的; image = null 。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
int main() {
int network_socket;
network_socket = socket(AF_INET, SOCK_STREAM, 0);
//specify an address for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = inet_addr("192.168.1.5");
int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
char server_response[256];
recv(network_socket, &server_response, sizeof(server_response), 0);
printf("THE SERVER SENT THE DATA: %s \n", server_response);
close(network_socket);
return 0;
}