添加新学生需要填写姓氏、给定姓名和出生日期字段。然而,当编辑学生时,出生日期是不必要的。到目前为止,只有"存储"方法有效:
表单请求:
public function rules()
{
return [
'group_id' => 'required|numeric',
'last_name' => 'required|alpha|min:2',
'given_name' => 'required|alpha|min:2',
'date_of_birth' => 'required',
];
}
控制器:
public function store(StudentValidation $request)
{
Student::create($request->all());
return redirect()->route('students.index');
}
public function update(StudentValidation $request, Student $student)
{
$student->update($request->except(['date_of_birth']));
return redirect()->route('students.index');
}
如果学生更新了,我如何让它忘记出生日期字段?
假设您使用标准RESTful路由,例如Post
用于创建和PUT/PATCH
用于更新,则可以执行以下操作:
public function rules()
{
$rules = [
'group_id' => 'required|numeric',
'last_name' => 'required|alpha|min:2',
'given_name' => 'required|alpha|min:2',
'date_of_birth' => 'required',
];
if ($this->method() === 'POST') {
$rules['date_of_birth'] = 'required';
}
return $rules;
}
您必须使用这样的规则,还必须使用PUT|PATCH
public function rules()
{
switch($this->method())
{
case 'GET':
case 'DELETE':
{
return [];
}
case 'POST':
{
return [
'group_id' => 'required|numeric',
'last_name' => 'required|alpha|min:2',
'given_name' => 'required|alpha|min:2',
'date_of_birth' => 'required',
];
}
case 'PUT':
case 'PATCH':
{
return [
'group_id' => 'required|numeric',
'last_name' => 'required|alpha|min:2',
'given_name' => 'required|alpha|min:2',
];
}
default:break;
}
}
你可以在下面这样做
public function rules()
{
$rules = [
'group_id' => 'required|numeric',
'last_name' => 'required|alpha|min:2',
'given_name' => 'required|alpha|min:2',
];
if (!$this->has('student_id')) // primary key i assume it is student_id
{
$rules += ['date_of_birth'=> 'required'];
}
return $rules;
}
这基本上是如果你没有学生证(意味着你正在创建一个学生(,那么就需要一个出生日期。这是假设你的主键是"student_id"。您可以/应该将"student_id"更改为主键的列名。
你可以这样做
StudentValidation请求类
<?php
namespace AppHttpRequests;
use IlluminateFoundationHttpFormRequest;
use IlluminateSupportFacadesValidator;
class StudentValidation extends FormRequest
{
private $rulesValidation = [
'group_id' => 'required|numeric',
'last_name' => 'required|alpha|min:2',
'given_name' => 'required|alpha|min:2',
];
/**
* 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 [
//
];
}
public function addStudent($request)
{
$this->rulesValidation['date_of_birth'] = 'required';
Validator::make($request,
$this->rulesValidation
)->validate();
}
public function updateStudent($request)
{
Validator::make($request,$this->rulesValidation)->validate();
}
}
控制器端代码。。
public function store(StudentValidation $request)
{
//if fail automatically throw error ...
$request->addStudent($request->all()); //call here method before create student...
Student::create($request->all());
return redirect()->route('students.index');
}
public function update(StudentValidation $request, Student $student)
{
$student->updateStudent($request->all()); //same as create
$student->update($request->except(['date_of_birth']));
return redirect()->route('students.index');
}
假设将来您将方法类型post更改为put,则您的代码为失败,但如果您喜欢这样做,那么您的代码就可以完美地工作并且再次重用您的代码,如果您想在另一个控制器中使用相同的验证请求类,则可以使用。。。