StoreAliasRequest.php 1.8 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. 'hostname' => 'nullable|string|max:100'
  40. ];
  41. }
  42. public function withValidator($validator)
  43. {
  44. $validator->sometimes('local_part', [
  45. 'required',
  46. 'max:50',
  47. Rule::unique('aliases')->where(function ($query) {
  48. return $query->where('local_part', $this->validationData()['local_part'])
  49. ->where('domain', $this->validationData()['domain']);
  50. }),
  51. new ValidAliasLocalPart
  52. ], function () {
  53. $format = $this->validationData()['format'] ?? 'random_characters';
  54. return $format === 'custom';
  55. });
  56. }
  57. public function messages()
  58. {
  59. return [
  60. 'local_part.unique' => 'That alias already exists.',
  61. ];
  62. }
  63. }