Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Implement a Validation Flow | Validation Flows and Safe Execution Patterns
/
PHP Error Handling and Exceptions
Avsnitt 3. Kapitel 2
single

single

bookChallenge: Implement a Validation Flow

Svep för att visa menyn

Uppgift

Swipe to start coding

Create a validation function for a user registration form that checks the username, email, and password fields. If a field is invalid, the function must throw a ValidationException with a relevant message.

  • If username is empty, throw a ValidationException with the message "Username cannot be empty".
  • If email is not a valid email address, throw a ValidationException with the message "Invalid email address".
  • If password is less than 8 characters long, throw a ValidationException with the message "Password must be at least 8 characters long".
  • If all fields are valid, return the string "Registration successful".

Lösning

Uppgift

Swipe to start coding

Create a validation function for a user registration form that checks the username, email, and password fields. If a field is invalid, the function must throw a ValidationException with a relevant message.

  • If username is empty, throw a ValidationException with the message "Username cannot be empty".
  • If email is not a valid email address, throw a ValidationException with the message "Invalid email address".
  • If password is less than 8 characters long, throw a ValidationException with the message "Password must be at least 8 characters long".
  • If all fields are valid, return the string "Registration successful".

Lösning

Create a validation function for a user registration form that checks the username, email, and password fields. If a field is invalid, the function must throw a ValidationException with a relevant message.

  • If username is empty, throw a ValidationException with the message Username cannot be empty;
  • If email is not a valid email address, throw a ValidationException with the message Invalid email address;
  • If password is less than 8 characters long, throw a ValidationException with the message Password must be at least 8 characters long;
  • If all fields are valid, return the string Registration successful.

Unit Test Checklist

  • Throws a ValidationException with the correct message if username is empty;
  • Throws a ValidationException with the correct message if email is not a valid email address;
  • Throws a ValidationException with the correct message if password is less than 8 characters long;
  • Returns "Registration successful" if all fields are valid.
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt