So I have these inputs from my form firstname
, lastname
, username
, email
, password
.
But I only want to complete the registration form and save it into my DB when all of these contain any info given by the user. I have this right now but it only shows an error if the password and/or password confirmation field do not match.
if( !empty ($_POST)){
if($_POST['password'] == $_POST['password_confirmation']) {
$user = new User();
$user->setFirstName($_POST['firstname']);
$user->setLastName($_POST['lastname']);
$user->setUsername($_POST['username']);
$user->setEmail($_POST['email']);
$user->setPassword($_POST['password']);
if($user->register()){
$user->login();
}
else{
$error_confirmation = true;
// This gives error in the front end if both of the PW do no match each other
}
}
I guess I am supposed to do something like this?
if( !empty ($_POST['firstname']) && !empty ($_POST['lastname']) && !empty ($_POST['username']) && !empty ($_POST['email']) && !empty ($_POST['password'])){
EDIT:
Okay I think I just figured it out by myself. The page doesn't send in the info but nor does it show the $error_firstname=true;
messages?
if( !empty ($_POST['firstname']) && !empty ($_POST['lastname']) && !empty ($_POST['username']) && !empty ($_POST['email']) && !empty ($_POST['password'])){
if( empty ($_POST['firstname'])){
$error_firstname = true;
}
if( empty ($_POST['lastname'])){
$error_lastname = true;
}
if( empty ($_POST['username'])){
$error_username = true;
}
if( empty ($_POST['email'])){
$error_email = true;
}
else if($_POST['password'] == $_POST['password_confirmation']) {
$user = new User();
$user->setFirstName($_POST['firstname']);
$user->setLastName($_POST['lastname']);
$user->setUsername($_POST['username']);
$user->setEmail($_POST['email']);
$user->setPassword($_POST['password']);
if($user->register()){
$user->login();
}
}
else{
$error_confirmation = true;
// show pw confirmation error
}
}
Final edit: This does fix the issue.
I fixed it with this and using required
on the input html tags.
The first line checks if all the input is given then proceeds to the next if
which checks if the pw that matches the pw confirmation and then saves into the DB
if( !empty ($_POST['firstname']) && !empty ($_POST['lastname']) && !empty ($_POST['username']) && !empty ($_POST['email']) && !empty ($_POST['password'])){
if($_POST['password'] == $_POST['password_confirmation']) {
$user = new User();
$user->setFirstName($_POST['firstname']);
$user->setLastName($_POST['lastname']);
$user->setUsername($_POST['username']);
$user->setEmail($_POST['email']);
$user->setPassword($_POST['password']);
if($user->register()){
$user->login();
}
}
else{
$error_confirmation = true;
// if no -> $error tonen
}
}
Php conditional expressions short-circuit, so they will stop as soon as they reach a false evaluation.
if(!empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['username']) && !empty($_POST['email']) && !empty($_POST['password']) && strlen($_POST['password'])>=8 && !empty($_POST['password_confirmation']) && $_POST['password'] == $_POST['password_confirmation']) {
$user = new User();
$user->setFirstName($_POST['firstname']);
$user->setLastName($_POST['lastname']);
$user->setUsername($_POST['username']);
$user->setEmail($_POST['email']);
$user->setPassword($_POST['password']);
if($user->register()){
$user->login();
}
} else {
$error_confirmation = true;
// if no -> $error tonen
}
As for cluing in the user about the password minimum, use a pattern
attribute for clientside validation and a placeholder
(or tooltip) for explanation.