trs_matrimony/app/Http/Requests/AuthRequest.php
2024-08-22 09:31:12 +05:30

97 lines
3.2 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class AuthRequest extends FormRequest
{
/**
* 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 [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'phone' => 'required_without:email|nullable|string|unique:users',
'email' => 'required_without:phone|nullable|email|unique:users',
'gender' => 'required',
'on_behalf' => 'required|integer',
'date_of_birth' => 'required|date',
'password' => 'required|string|min:8|confirmed',
'referral_code' => 'sometimes',
];
}
/**
* Get the validation messages of rules that apply to the request.
*
* @return array
*/
public function messages()
{
return [
'gender.required' => translate('gender is required'),
'on_behalf.required' => translate('on_behalf is required'),
'on_behalf.integer' => translate('on_behalf should be integer value'),
'date_of_birth.required' => translate('date_of_birth is required'),
'date_of_birth.date' => translate('date_of_birth should be in date format'),
'first_name.required' => translate('first_name is required'),
'last_name.required' => translate('last_name is required'),
'email.required' => translate('Email is required'),
'email.email' => translate('Email must be a valid email address'),
'email.unique' => translate('An user exists with this email'),
'phone.unique' => translate('An user exists with this phone'),
'phone.required' => translate('Phone is required'),
'password.required' => translate('Password is required'),
'password.confirmed' => translate('Password confirmation does not match'),
'password.min' => translate('Minimum 8 digits required for password'),
];
}
protected function prepareForValidation()
{
$this->merge([
'on_behalves_id' => $this->on_behalf,
'birthday' => date('Y-m-d', strtotime($this->date_of_birth)),
]);
}
/**
* Get the error messages for the defined validation rules.*
* @return array
*/
public function failedValidation(Validator $validator)
{
// dd($this->expectsJson());
if ($this->expectsJson()) {
throw new HttpResponseException(response()->json([
'message' => $validator->errors()->all(),
'result' => false
], 422));
} else {
throw (new ValidationException($validator))
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
}
}