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

single

bookChallenge: Implement a Validation Flow

Glissez pour afficher le menu

Tâche

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".

Solution

Tâche

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".

Solution

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 desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 2
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

some-alt