StoreAliasRequest.php 1.7 KB

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