Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Implement a Validation Flow | Validation Flows and Safe Execution Patterns
Practice
Projects
Quizzes & Challenges
Quizer
Challenges
/
PHP Error Handling and Exceptions
Seksjon 3. Kapittel 2
single

single

bookChallenge: Implement a Validation Flow

Sveip for å vise menyen

Oppgave

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

Oppgave

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 desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 2
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt