Slide 1

Slide 1 text

FORM validation #TheLaravelKid BEGINS LARACON.EU The Art of

Slide 2

Slide 2 text

So, validtaion…

Slide 3

Slide 3 text

So, validation… It's required, right?

Slide 4

Slide 4 text

I'm Caneco Full-Stack Developer ! MEDICARE caneco.xyz

Slide 5

Slide 5 text

I'm Caneco .PHP .JS .HTML .CSS .JSON .SQL .GIT .SH .SVG .PNG .PSD .AI

Slide 6

Slide 6 text

I'm Caneco @enunomaduro's "Schoger"

Slide 7

Slide 7 text

I'm Caneco from Lisbon, Portugal Netherlands here Spain here Portugal here

Slide 8

Slide 8 text

WARNING ALL THE FOLLOWING SLIDES CONTAIN VALUABLE INFORMATION FOR YOUR DAILY LIFE… ALL GIFS ARE FROM THE BADASS TV SHOW COBRA KAI THAT YOU ALL MUST WATCH… AND ALL THE SLIDES, AND CONTENT ARE 100% MADE ON KEYNOTE ̈

Slide 9

Slide 9 text

LET'S BEGIN

Slide 10

Slide 10 text

Why validate?

Slide 11

Slide 11 text

On a regular day in 2019

Slide 12

Slide 12 text

500 million tweets source: weforum.org

Slide 13

Slide 13 text

294 billion emails source: weforum.org

Slide 14

Slide 14 text

5 billion searches source: weforum.org

Slide 15

Slide 15 text

~300 new posts on laracasts.com/discuss source: the Jeffrey Way

Slide 16

Slide 16 text

~64.5K downloads of Spatie packages source: spatie.be/open-source

Slide 17

Slide 17 text

It's a LOT of data

Slide 18

Slide 18 text

It's a LOT of data

Slide 19

Slide 19 text

through APIs [GET] skyscanner-example.dev/v1/flights [PATCH] zomato-example.dev/place/123/book [GET] openweather-example.dev/fatima [GET] musixmatch-example.dev/top-10 [GET] nasa-example.dev/v9999/planet-x [GET] numbers-example.dev/random-number [GET] urbandictionary-example.dev/tldr [GET] tempmail-example.dev/fresh [GET] moviedatabase-example.dev/v1/top/horror [GET] ip-example.dev/127.0.0.1

Slide 20

Slide 20 text

or in forms

Slide 21

Slide 21 text

Validation… do we really need it?

Slide 22

Slide 22 text

Can't we trust our users?

Slide 23

Slide 23 text

MOST DEF

Slide 24

Slide 24 text

It's purely just for fun

Slide 25

Slide 25 text

Validation 101

Slide 26

Slide 26 text

SIGN IN Sign in Email address Password SENDING

Slide 27

Slide 27 text

SIGN IN Sign in These credentials do not match our records. Email address Password

Slide 28

Slide 28 text

1 2 3 4 ▶ 9 10 11 12

Slide 29

Slide 29 text

1 2 3 4 5 ▶ 8 9 10 11 12

Slide 30

Slide 30 text

SIGN IN Sign in Email address Password Please fill in this field. Please fill in this field.

Slide 31

Slide 31 text

Sign in Email address example Password ●●●●●● SIGN IN SENDING

Slide 32

Slide 32 text

SIGN IN Sign in These credentials do not match our records. Email address example Password

Slide 33

Slide 33 text

1 2 3 4 5 ▶ 8 9 10 11 12

Slide 34

Slide 34 text

1 2 3 4 5 ▶ 8 9 10 11 12

Slide 35

Slide 35 text

SIGN IN Sign in Email address example Password ●●●●●● Please include an '@' in the email address.

Slide 36

Slide 36 text

SIGN IN Sign in Email address example@ Password ●●●●●● Please enter a part following '@'.

Slide 37

Slide 37 text

SIGN IN Sign in Email address example@ Password ●●●●●● SENDING email

Slide 38

Slide 38 text

SIGN IN Sign in These credentials do not match our records. Email address example@email Password ●●●●●●

Slide 39

Slide 39 text

1 2 3 4 5 ▶ 8 9 10 11 12

Slide 40

Slide 40 text

1 2 3 4 5 6 ▶ 8 9 10 11 12

Slide 41

Slide 41 text

1 2 3 4 5 6 ▶ 8 9 10 11 12 13

Slide 42

Slide 42 text

1 2 3 4 5 6 ▶ 8 9 10 11 12 13 14

Slide 43

Slide 43 text

1 2 3 4 5 6 ▶ 8 9 10 11 12 13 14

Slide 44

Slide 44 text

Thank you

Slide 45

Slide 45 text

Not exactly…

Slide 46

Slide 46 text

HTML5 validation depends on the browser

Slide 47

Slide 47 text

And it's not that simple nor pretty

Slide 48

Slide 48 text

1 2 3 4 5 6 ▶ 8 9 10 11 12 13

Slide 49

Slide 49 text

1 2 3 4 5 6 ▶ 20 function "'invoke(Request $request) { $request"(validate([ 'username' ") 'required|email', 'password' ") 'required|min:6|strong_password', ]); ""$ }

Slide 50

Slide 50 text

JavaScript

Slide 51

Slide 51 text

1 ▶ 42 ""$ "*form>

Slide 52

Slide 52 text

1 ▶ 42 ""$ "*form>

Slide 53

Slide 53 text

jQueryValidate.js

Slide 54

Slide 54 text

1 2 3 4 5 6 7 8 9 10 var rules = { username: { required: true, email: true, }, password: { required: true, minlength: 6, }, }

Slide 55

Slide 55 text

1 2 3 4 5 6 7 8 9 10 11 12 var rules = { username: { required: true, email: true, }, password: { required: true, minlength: 6, }, } $("#login").validate(rules)

Slide 56

Slide 56 text

validate.js

Slide 57

Slide 57 text

1 2 3 4 let input = { username: document.getElementById('username').value(), password: document.getElementById('password').value(), }

Slide 58

Slide 58 text

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 let input = { username: document.getElementById('username').value(), password: document.getElementById('password').value(), } let rules = { username: { presence: true, email: true }, password: { presence: true, length: { minimum: 6 }, }, }

Slide 59

Slide 59 text

1 ▶ 4 7 6 ▶ 15 16 17 18 19 let input = { !!" } let rules = { ""$ } if (validate(input, rules)) { "+ }

Slide 60

Slide 60 text

VeeValidate

Slide 61

Slide 61 text

> npm install vee-validate

Slide 62

Slide 62 text

1 2 import Vue from 'vue' import VeeValidate from 'vee-validate'

Slide 63

Slide 63 text

1 2 3 4 import Vue from 'vue' import VeeValidate from 'vee-validate' Vue.use(VeeValidate)

Slide 64

Slide 64 text

1 2 3 4 5

Slide 65

Slide 65 text

1 2 3 4 5 6 7

Slide 66

Slide 66 text

1 2 3 4 5 6 7

Slide 67

Slide 67 text

1 2 3 4 import Vue from 'vue' import VeeValidate from 'vee-validate' Vue.use(VeeValidate)

Slide 68

Slide 68 text

1 2 3 4 5 6 import Vue from 'vue' import VeeValidate from 'vee-validate' Vue.use(VeeValidate, { useConstraintAttrs: false, })

Slide 69

Slide 69 text

1 2 3 4 5 6 7

Slide 70

Slide 70 text

1 2 3 4 5 6

Slide 71

Slide 71 text

1 2 3 4 5 6 7 8 9 10 11 alpha alpha_dash alpha_num alpha_spaces before between confirmed credit_card date_between date_format decimal digits dimensions email image included integer ip is is_not length max_value mimes min max min_value excluded numeric regex required required_if size url

Slide 72

Slide 72 text

Add custom rules

Slide 73

Slide 73 text

1 import { Validator } from 'vee-validate'

Slide 74

Slide 74 text

1 2 3 4 5 6 7 import { Validator } from 'vee-validate' import { STRONG_PASS_REGEX } from '@/helpers/regex_rules' const strongPassword = { getMessage: field ") `${field} is weak.`, validate: value ") STRONG_PASS_REGEX.test(value) }

Slide 75

Slide 75 text

1 2 3 4 5 6 7 import { Validator } from 'vee-validate' import { STRONG_PASS_REGEX } from '@/helpers/regex_rules' const strongPassword = { getMessage: field ") `${field} is weak.`, validate: value ") STRONG_PASS_REGEX.test(value) }

Slide 76

Slide 76 text

1 2 3 4 5 6 7 import { Validator } from 'vee-validate' import { STRONG_PASS_REGEX } from '@/helpers/regex_rules' const strongPassword = { getMessage: field ") `${field} is weak.`, validate: value ") STRONG_PASS_REGEX.test(value) }

Slide 77

Slide 77 text

1 2 3 4 5 6 7 8 9 import { Validator } from 'vee-validate' import { STRONG_PASS_REGEX } from '@/helpers/regex-rules' const strongPassword = { getMessage: field ") `${field} is weak.`, validate: value ") STRONG_PASS_REGEX.test(value) } Validator.extend('strong_password', strongPassword)

Slide 78

Slide 78 text

1 2 3 4 5 6

Slide 79

Slide 79 text

Using ErrorBag

Slide 80

Slide 80 text

1 2 3 4 5 6 7

Slide 81

Slide 81 text

1 2 3 4 5 6 7 8 9 10 Email address "*label>

Slide 82

Slide 82 text

1 2 3 4 5 6 7 8 9 10 11 12 13 14 Email address "*label>

Slide 83

Slide 83 text

1 ▶ 6 7 ▶ 14 15 16 17 ""$ "*label> The username must be a valid email. "*p>

Slide 84

Slide 84 text

1 ▶ 6 7 ▶ 14 15 16 17 ""$ "*label> {{ errors.first('username') }} "*p>

Slide 85

Slide 85 text

SIGN IN Sign in The username must be a valid email. Email address example Password ●●●●●● Email address example

Slide 86

Slide 86 text

1 2 3 4 {{ errors.any() }} {{ errors.all() }} {{ errors.count() }} {{ errors.clear() }}

Slide 87

Slide 87 text

WAIT?! ALL OF THAT?

Slide 88

Slide 88 text

Why validate on the client side?

Slide 89

Slide 89 text

Assure the data is good

Slide 90

Slide 90 text

Quick feedback

Slide 91

Slide 91 text

Real-time feedback

Slide 92

Slide 92 text

Reduce server validation

Slide 93

Slide 93 text

Now in the server side

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

Why validate on the server side?

Slide 96

Slide 96 text

Reassure the data is good

Slide 97

Slide 97 text

Server-side checking

Slide 98

Slide 98 text

Domain rules

Slide 99

Slide 99 text

1 2 3 4 5 6 ▶ 20 function "'invoke(Request $request) { $request"(validate([ 'username' ") 'required|email', 'password' ") 'required|min:6|strong_password', ]); ""$ }

Slide 100

Slide 100 text

1 2 3 4 5 6 7 8 9 HTTP/1.1 422 Unprocessable Entity Content-Type: application/json; charset=utf-8 ""$ "data": { "errors": { "email": [ "The email must be a valid email address." ], ""$

Slide 101

Slide 101 text

1 2 3 4 5 6 7 8 9 HTTP/1.1 422 Unprocessable Entity Content-Type: application/json; charset=utf-8 ""$ "data": { "errors": { "email": [ "The email must be a valid email address." ], ""$

Slide 102

Slide 102 text

1 2 3 4 5 6 7 8 9 10 11 12 13 HTTP/1.1 422 Unprocessable Entity Content-Type: application/json; charset=utf-8 ""$ "data": { "errors": { "email": [ "The email must be a valid email address." ], "password": [ "The password must be at least 6 characters", "The password must have 2 uppercase letters 1 special letter 2 digi ] ""$

Slide 103

Slide 103 text

1 2 ▶ 8 ▶ 12 13 if (response.data.errors.email) { if (/required/i.test(response.data.errors)) { ""$ } else if (/invalid/i.test(response.data.errors)) { ""$ } }

Slide 104

Slide 104 text

How to simplify the conversation?

Slide 105

Slide 105 text

> art make:request SubmitRequest

Slide 106

Slide 106 text

1 2 3 4 5 6 7 public function rules() { return [ 'username' ") 'required|email', 'password' ") 'required|min:6|strong_password', ]; }

Slide 107

Slide 107 text

1 2 3 4 5 6 7 8 9 public function messages() { return [ 'required' ") ':attribute.required', 'email' ") 'email.invalid', 'password.min' ") 'password:min', 'strong_password' ") 'password_not_strong', ]; }

Slide 108

Slide 108 text

1 2 3 4 5 6 7 8 9 public function failedValidation(Validator $validator) { throw (new HttpResponseException( response()"(json([ 'message' ") 'The given data was invalid.', 'errors' ") $validator"(errors()"(all(), ], 422) )); }

Slide 109

Slide 109 text

1 2 3 4 5 6 7 8 9 public function failedValidation(Validator $validator) { throw (new HttpResponseException( response()"(json([ 'message' ") 'The given data was invalid.', 'errors' ") $validator"(errors()"(all(), ], 418) )); }

Slide 110

Slide 110 text

1 2 3 4 5 6 7 8 9 HTTP/1.1 418 I'm a teapot Content-Type: application/json; charset=utf-8 ""$ { "message": "The given data was invalid.", "errors": [ "email.invalid" ] }

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

Validation examples

Slide 113

Slide 113 text

email fields

Slide 114

Slide 114 text

Simple form Email address SUBMIT The email field is required.

Slide 115

Slide 115 text

Simple form Email address SUBMIT The email must be a valid email address. example

Slide 116

Slide 116 text

1 2 3 4 5 6

Slide 117

Slide 117 text

" prevent the errors

Slide 118

Slide 118 text

Simple form Email address SUBMIT Did you mean [email protected]? [email protected]

Slide 119

Slide 119 text

Slide 120

Slide 120 text

1 2 3 4 5 6 7 8 9 function "'invoke(Request $request) { $request"(validate([ 'email' ") [ 'required', 'email', ] ]); }

Slide 121

Slide 121 text

1 2 3 4 5 6 7 8 9 function "'invoke(Request $request) { $request"(validate([ 'email' ") [ 'required', 'email', "+ [email protected] ] ]); }

Slide 122

Slide 122 text

Simple form Email address SUBMIT [email protected]

Slide 123

Slide 123 text

1 2 3 4 5 6 7 8 9 function "'invoke(Request $request) { $request"(validate([ 'email' ") [ 'required', 'email', ] ]); }

Slide 124

Slide 124 text

1 2 3 4 5 6 7 8 9 function "'invoke(Request $request) { $request"(validate([ 'email' ") [ 'required', 'email:rfc,dns', "+ new in Laravel 5.8.33 ] ]); }

Slide 125

Slide 125 text

1 2 3 4 5 6 7 8 9 10 function "'invoke(Request $request) { $request"(validate([ 'email' ") [ 'required', 'email:rfc,dns', 'unique:users.email', ] ]); }

Slide 126

Slide 126 text

1 2 3 4 5 6 7 8 9 10 function "'invoke(Request $request) { $request"(validate([ 'email' ") [ 'required', 'email:rfc,dns', "+ ", ATTENTION 'unique:users.email', "+ ", ATTENTION ] ]); }

Slide 127

Slide 127 text

1 2 3 4 5 6 7 8 9 10 11 function "'invoke(Request $request) { $request"(validate([ 'email' ") [ 'bail', 'required', 'email:rfc,dns', 'unique:users.email', ] ]); }

Slide 128

Slide 128 text

Remember SubmitRequest?

Slide 129

Slide 129 text

1 2 3 4 5 6 7 8 HTTP/1.1 418 I'm a teapot Content-Type: application/json; charset=utf-8 ""$ "data": { "errors": [ "email.invalid", ] ""$

Slide 130

Slide 130 text

1 2 3 4 5 6 7 this.$http.post('/submit’) .then(response ") { "+ }) .catch({ response } ") { "+ })

Slide 131

Slide 131 text

1 ▶ 5 6 7 8 9 this.$http.post('/submit’) ""$ .catch({ response } ") { if (response.status ""- 418) { "+ } })

Slide 132

Slide 132 text

1 ▶ 5 6 7 8 9 10 11 this.$http.post('/submit’) ""$ .catch({ response } ") { if (response.status ""- 418) { if (response.data.errors.includes('email.invalid')) { "+ } } })

Slide 133

Slide 133 text

1 ▶ 5 6 7 8 9 10 11 12 13 14 15 this.$http.post('/submit’) ""$ .catch({ response } ") { if (response.status ""- 418) { let errors = response.data.errors if (response.data.errors.includes('email.invalid')) { this.$validator.errors.add({ field: 'email', msg: 'The email must be a valid email address.', }) } } })

Slide 134

Slide 134 text

1 ▶ 5 6 7 8 9 10 11 12 13 14 15 16 this.$http.post('/submit’) ""$ .catch({ response } ") { if (response.status ""- 418) { let errors = response.data.errors if (errors.includes('email.invalid')) { this.$validator.errors.add({ field: 'email', msg: 'The email must be a valid email address.', }) this.$el.username.focus() } } })

Slide 135

Slide 135 text

phone fields

Slide 136

Slide 136 text

1 2 3 4 5 6

Slide 137

Slide 137 text

1 2 3 4 5 6

Slide 138

Slide 138 text

1 2 3 4 5 6 7 8 import { Validator } from 'vee-validate' const phone = { getMessage: field ") `The ${field} must be a valid phone number.`, validate: value ") { return /^[0-9]{9}$/.test(value) } }

Slide 139

Slide 139 text

1 2 3 4 5 6 7 8 import { Validator } from 'vee-validate' const phone = { getMessage: field ") `The ${field} must be a valid phone number.`, validate: value ") { return /^2[0-9]{8}|9[1236][0-9]{7}$/.test(value) } }

Slide 140

Slide 140 text

Simple form Mobile number SUBMIT The phone must be a valid phone number. 201000123

Slide 141

Slide 141 text

" prevent the errors

Slide 142

Slide 142 text

Simple form Mobile number SUBMIT 912367125637512123987

Slide 143

Slide 143 text

Simple form Mobile number SUBMIT asdasdasd

Slide 144

Slide 144 text

1 2 3 4 5 6

Slide 145

Slide 145 text

1 2 3 4 5 6 7

Slide 146

Slide 146 text

> npm install vee-mask

Slide 147

Slide 147 text

1 2 import Vue from 'vue' import VeeMask from 'vee-mask'

Slide 148

Slide 148 text

1 2 3 4 import Vue from 'vue' import VeeMask from 'vee-mask' Vue.use(VeeMask)

Slide 149

Slide 149 text

1 2 3 4 5 6 7 8

Slide 150

Slide 150 text

1 2 3 4 5 6 7 8

Slide 151

Slide 151 text

Simple form Mobile number SUBMIT 213 123 123

Slide 152

Slide 152 text

zip, iban, address, weight…

Slide 153

Slide 153 text

They follow the same idea

Slide 154

Slide 154 text

WAX ON, WAX OFF

Slide 155

Slide 155 text

Extra tricks

Slide 156

Slide 156 text

autofocus

Slide 157

Slide 157 text

1

Slide 158

Slide 158 text

1

Slide 159

Slide 159 text

Sign up Full name Email address Phone CONTINUE |

Slide 160

Slide 160 text

" attention to mobile

Slide 161

Slide 161 text

Sign up Full name Email address Phone CONTINUE | Done Q W E R T Y U I O P space return 123 A S D F G H J K L ⌫ ⇧ Z X C V B N M

Slide 162

Slide 162 text

1

Slide 163

Slide 163 text

1

Slide 164

Slide 164 text

1 2 3 4 5 6 7 8 Vue.directive('autofocus', { inserted: (el, binding) ") { if (! isMobile) { el.setAttribute('autofocus', 'autofocus') el.focus() "+ Justin Case } }, })

Slide 165

Slide 165 text

input types

Slide 166

Slide 166 text

1

Slide 167

Slide 167 text

SUBMIT The Form Email field | Done Q W E R T Y U I O P space return 123 A S D F G H J K L ⌫ ⇧ Z X C V B N M Done Q W E R T Y U I O P space return 123 A S D F G H J K L ⌫ ⇧ Z X C V B N M @ .

Slide 168

Slide 168 text

1

Slide 169

Slide 169 text

The Form Phone field SUBMIT | Done ⌫ + ❋ # 2 1 3 5 4 6 8 7 9 0 ABC GHI JKL MNO DEF TUV PQRS WXYZ

Slide 170

Slide 170 text

1

Slide 171

Slide 171 text

The Form Number field SUBMIT | Done 1 2 3 4 5 6 7 8 9 0 space return ABC ⌫ #+= . , ? ! ´ - / : ; ( ) $ & @ "

Slide 172

Slide 172 text

1

Slide 173

Slide 173 text

1

Slide 174

Slide 174 text

inputmode

Slide 175

Slide 175 text

66 20 76 Nope Chrome Firefox Edge Safari Desktop Mobile / Tablet 75 Nope 67 12.2-12.3 Android Chrome Android Firefox Android iOS Safari

Slide 176

Slide 176 text

1

Slide 177

Slide 177 text

1 2 3 4 5 6

Slide 178

Slide 178 text

1 2 3 4 5 6

Slide 179

Slide 179 text

1 2 3 4 5 6

Slide 180

Slide 180 text

The Form Decimal field SUBMIT | Done ⌫ . 2 1 3 5 4 6 8 7 9 0 ABC GHI JKL MNO DEF TUV PQRS WXYZ

Slide 181

Slide 181 text

placeholder

Slide 182

Slide 182 text

1 2 3 4 5

Slide 183

Slide 183 text

REGISTER Sign up Your email Caneco

Slide 184

Slide 184 text

Prevent the Dory Syndrome

Slide 185

Slide 185 text

REGISTER Sign up Your email Caneco Your full name

Slide 186

Slide 186 text

REGISTER Sign up Your full name Your email Caneco The full name must be a valid name. Caneco2

Slide 187

Slide 187 text

REGISTER Sign up [email protected] Your full name Caneco Your email

Slide 188

Slide 188 text

Now you're ready

Slide 189

Slide 189 text

READY TO STRIKE

Slide 190

Slide 190 text

But first

Slide 191

Slide 191 text

VALIDATE FIRST VALIDATE BACK NO MERCY

Slide 192

Slide 192 text

Thank you

Slide 193

Slide 193 text

FORM validation #TheLaravelKid The Art of