我正在开发一个项目,其中一部分是用户可以创建项目。
项目需要与用户耦合。如果用户在创建项目时登录,则没有问题,我可以将项目附加到经过身份验证的用户,但是当用户未登录时,我的表单中有以下部分
简而言之,有2个radiobuttons,第1个意思是“我有一个帐户,登录我”,下面有登录字段。第二个意味着“我是一个新用户,创建一个帐户”,它将显示创建用户的正确字段。
由于此表单已添加到项目创建表单中,因此必须立即验证所有内容。
现在我有一个处理所有验证的formRequest,但我正在努力使用登录部分。
验证时,我必须检查以下内容:
1)如果用户在开始填写表单时已登录,但在存储数据时不再登录,那么我们必须将其添加到errors数组
2)如果用户在开始填写表单时没有登录,并且他选择通过上面说明的表单登录,那么我们需要在创建项目时尝试登录用户,并且在登录失败时向错误数组添加消息“已提供错误凭据....”。
我尝试过以下FormRequest:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateJobsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
if (! auth()->check()) {
if($this->get('is_logged_in')) {
// If we reach this, then we were logged in when starting to fill in the form
// but are no longer when finishing the form (idle for to long, ....).
$this->getValidatorInstance()->errors()->add('authentication', 'You were no longer authenticated when creating the project.');
}
if($this->get('auth_type') == 'login') {
if(! auth()->attempt($this->getLoginParams())) {
// if we reach this, we have chosen to log in through the form, bu the provided details did not match our records.
$this->getValidatorInstance()->errors()->add('authentication', 'Failed to authenticate');
}
}
}
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'login.email' => 'required_if:auth_type,login',
'login.password' => 'required_if:auth_type,login',
'register.email' => 'nullable|required_if:auth_type,register|email|max:255',
'register.password' => 'required_if:auth_type,register,confirmed|max:255',
'register.firstname' => 'required_if:auth_type,register|max:255',
'register.name' => 'required_if:auth_type,register|max:255',
'jobs.*.title' => 'required',
'jobs.*.description' => 'required',
'jobs.*.category_id' => 'required|exists:job_categories,id',
'jobs.*.location_id' => 'required|exists:job_locations,id',
'jobs.*.profiles' => 'required|numeric'
];
}
private function getLoginParams()
{
return ['email' => $this->get('login.email'), 'password' => $this->get('login.password')];
}
}
最重要的部分是授权方法。我正在尝试将项目添加到errors数组中,但它无法正常工作。
1)如果规则函数中的所有其他验证都通过,那么我不会被重定向到创建页面,但代码会进入控制器并创建一个项目。
2)如果没有通过所有其他验证,那么我将被重定向到创建页面,但我在authorize方法中添加的错误不会添加到错误中,因此不会显示在创建页面上。
答案 0 :(得分:0)
我不认为authorize
方法是放置这种逻辑的正确位置。
我会根据登录状态向规则数组添加其他规则。
尝试这样的事情:
public function rules()
{
$rules = [
'login.email' => 'required_if:auth_type,login',
'login.password' => 'required_if:auth_type,login',
'register.email' => 'nullable|required_if:auth_type,register|email|max:255',
'register.password' => 'required_if:auth_type,register,confirmed|max:255',
'register.firstname' => 'required_if:auth_type,register|max:255',
'register.name' => 'required_if:auth_type,register|max:255',
'jobs.*.title' => 'required',
'jobs.*.description' => 'required',
'jobs.*.category_id' => 'required|exists:job_categories,id',
'jobs.*.location_id' => 'required|exists:job_locations,id',
'jobs.*.profiles' => 'required|numeric'
];
if (! auth()->check()) {
if($this->get('is_logged_in')) {
// If we reach this, then we were logged in when starting to fill in the form
// but are no longer when finishing the form (idle for to long, ....).
$rules = array_merge($rules, ['no_authentication' => 'required']);
}
if($this->get('auth_type') == 'login') {
if(! auth()->attempt($this->getLoginParams())) {
// if we reach this, we have chosen to log in through the form, bu the provided details did not match our records.
$rules = array_merge($rules, ['failed_authentication' => 'required', 'Failed to authenticate']);
}
}
}
return $rules;
}
public function messages()
{
return [
'no_authentication.required' => 'You were no longer authenticated when creating the project.',
'failed_authentication.required' => 'Failed to authenticate'
];
}