StoreAliasRequest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Rules\ValidAliasLocalPart;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Validation\Rule;
  6. class StoreAliasRequest extends FormRequest
  7. {
  8. /**
  9. * Determine if the user is authorized to make this request.
  10. *
  11. * @return bool
  12. */
  13. public function authorize()
  14. {
  15. return true;
  16. }
  17. /**
  18. * Get the validation rules that apply to the request.
  19. *
  20. * @return array
  21. */
  22. public function rules()
  23. {
  24. return [
  25. 'domain' => [
  26. 'required',
  27. 'string',
  28. Rule::in($this->user()->domainOptions())
  29. ],
  30. 'description' => 'nullable|max:100',
  31. 'format' => 'nullable|in:uuid,random_words,custom'
  32. ];
  33. }
  34. public function withValidator($validator)
  35. {
  36. $validator->sometimes('local_part', [
  37. 'required',
  38. 'max:50',
  39. Rule::unique('aliases')->where(function ($query) {
  40. return $query->where('local_part', $this->validationData()['local_part'])
  41. ->where('domain', $this->validationData()['domain']);
  42. }),
  43. new ValidAliasLocalPart
  44. ], function () {
  45. $format = $this->validationData()['format'] ?? 'uuid';
  46. return $format === 'custom';
  47. });
  48. }
  49. public function messages()
  50. {
  51. return [
  52. 'local_part.unique' => 'That alias already exists.',
  53. ];
  54. }
  55. }