Reputation: 5088
I'm having a problem validating the length of an email in Laravel 5.4.
In the migration I created the email field by default, and it was done correctly in DB as varchar (191) utf8mb4_unicode_ci
.
In the frontend there is no problem with validation, but in backend it only allows up to 73 characters.
In Controller on save and update methods, I tried:
$this->validate($request, [
// ...
'email' => 'nullable|email',
// ....
]);
$this->validate($request, [
// ...
'email' => 'nullable|email|max:191',
// ....
]);
View form input:
{!! Form::label('email', 'Email:') !!}
{!! Form::email('email', null, ['class' => 'form-control' . ($errors->has('email') ? ' is-invalid' : ''), 'maxlength' => 191]) !!}
@if ($errors->has('email'))
<span class="invalid-feedback">{{ $errors->first('email') }}</span>
@endif
This passes the validation (73 characters):
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@test.com
but this, or longer ones, do not pass the validation (74 characters):
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@test.com
(also I tried other characters, not just aaa, just in case it's not the lenght with no luck)
What am I doing wrong?
Upvotes: 0
Views: 931
Reputation: 14278
RFC specifications for email and what is technically valid within one.
- An e-mail address consists of local part and domain separated by an at sign (@) character (RFC 2822 3.4.1).
- The local part may consist of alphabetic and numeric characters, and the following characters: !, #, $, %, &, ', *, +, -, /, =, ?, ^, _, `, {, |, } and ~, possibly with dot separators (.), inside, but not at the start, end or next to another dot separator (RFC2822 3.2.4).
- The local part may consist of a quoted string—that is, anything within quotes ("), including spaces (RFC 2822 3.2.5).
- Quoted pairs (such as @) are valid components of a local part, though an obsolete form from RFC 822 (RFC 2822 4.4).
- The maximum length of a local part is 64 characters (RFC 2821 4.5.3.1).
- A domain consists of labels separated by dot separators (RFC1035 2.3.1).
- Domain labels start with an alphabetic character followed by zero or more alphabetic characters, numeric characters or the hyphen (-), ending with an alphabetic or numeric character (RFC 1035 2.3.1).
- The maximum length of a label is 63 characters (RFC 1035 2.3.1).
- The maximum length of a domain is 255 characters (RFC 2821 4.5.3.1).
- The domain must be fully qualified and resolvable to a type A or type MX DNS address record (RFC 2821 3.6).
So in conclusion if you would want to use a longer one you should add your custom email rule which will avoid some of this checks.
Upvotes: 1