79 lines
2.4 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2019-11-16 10:21:14 +03:00
namespace App\Http\Requests\Common;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\FormRequest;
use Illuminate\Support\Str;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
class Contact extends FormRequest
2017-09-14 22:21:00 +03:00
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
2021-04-29 16:09:10 +03:00
$email = '';
2021-05-14 18:29:24 +03:00
$logo = 'nullable';
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$type = $this->request->get('type', 'customer');
2021-07-18 17:12:37 +03:00
2022-06-01 10:15:55 +03:00
if (empty(config('type.contact.' . $type))) {
2021-07-18 17:12:37 +03:00
$type = null;
}
$company_id = (int) $this->request->get('company_id');
2017-09-14 22:21:00 +03:00
// Check if store or update
if ($this->getMethod() == 'PATCH') {
2021-02-17 22:38:11 +03:00
$model = $this->isApi() ? 'contact' : $type;
$id = is_numeric($this->$model) ? $this->$model : $this->$model->getAttribute('id');
2017-09-14 22:21:00 +03:00
} else {
$id = null;
}
if (!empty($this->request->get('email'))) {
2022-06-01 10:15:55 +03:00
$email .= 'email:rfc,dns|unique:contacts,NULL,'
2021-04-29 16:09:10 +03:00
. $id . ',id'
. ',company_id,' . $company_id
. ',type,' . $type
. ',deleted_at,NULL';
2021-04-29 15:19:05 +03:00
if (isset($model) && $this->$model->user_id) {
2021-04-29 16:09:10 +03:00
$email .= '|unique:users,NULL,' . $this->$model->user_id . ',id,deleted_at,NULL';
2021-04-29 15:19:05 +03:00
}
}
2017-09-14 22:21:00 +03:00
2021-05-14 18:29:24 +03:00
if ($this->files->get('logo')) {
$logo = 'mimes:' . config('filesystems.mimes')
. '|between:0,' . config('filesystems.max_size') * 1024
. '|dimensions:max_width=' . config('filesystems.max_width') . ',max_height=' . config('filesystems.max_height');
2021-05-14 18:29:24 +03:00
}
2017-09-14 22:21:00 +03:00
return [
'type' => 'required|string',
'name' => 'required|string',
'email' => $email,
'user_id' => 'integer|nullable',
'currency_code' => 'required|string|currency',
'enabled' => 'integer|boolean',
'logo' => $logo,
];
}
public function messages()
{
$logo_dimensions = trans('validation.custom.invalid_dimension', [
'attribute' => Str::lower(trans_choice('general.pictures', 1)),
'width' => config('filesystems.max_width'),
'height' => config('filesystems.max_height'),
]);
return [
'logo.dimensions' => $logo_dimensions,
2017-09-14 22:21:00 +03:00
];
}
}