12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace App\Http\Requests;
- use App\Rules\ValidAliasLocalPart;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Validation\Rule;
- class StoreAliasRequest 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 [
- 'domain' => [
- 'required',
- 'string',
- Rule::in($this->user()->domainOptions())
- ],
- 'description' => 'nullable|max:100',
- 'format' => 'nullable|in:uuid,random_words,custom'
- ];
- }
- public function withValidator($validator)
- {
- $validator->sometimes('local_part', [
- 'required',
- 'max:50',
- Rule::unique('aliases')->where(function ($query) {
- return $query->where('local_part', $this->validationData()['local_part'])
- ->where('domain', $this->validationData()['domain']);
- }),
- new ValidAliasLocalPart
- ], function () {
- $format = $this->validationData()['format'] ?? 'uuid';
- return $format === 'custom';
- });
- }
- public function messages()
- {
- return [
- 'local_part.unique' => 'That alias already exists.',
- ];
- }
- }
|