Jump to content

Headache checking for duplicated user emails


Albert
 Share

Recommended Posts

Hello @ all,

I am trying to check a users input on the register process: 

if($input->post->email){
    $em = $sanitizer->email($input->post->email);
    var_dump($validMail, $checkMail->count, $em);
    echo "<br>--------------<br>";
    $checkMail = $wire->pages->find("template=user, email=$em");    
    if($checkMail->count !== 0){
        $validMail = 0;
    } else {
        $validMail = 1;
    }
}

int(1) int(0) and some mailstring is returned in the var_dump below this code, as the mail address is in the DB, it should return int(0) int (1) for valid email = 0; 

Can anyone assist me please? 

 

Thank you in advance! 

Link to comment
Share on other sites

Somehow now it works....but I don't really know why....can you help me? 

 

if($input->post->email){
    $em = $sanitizer->email($input->post->email);
    var_dump($validMail, $checkMail->count, $em);
    echo "<br>--------------<br>";
    $checkMail = $wire->pages->find("template=user, email=$em");    
    var_dump($checkMail);
    echo "<br>--------------<br>";
    if($checkMail->count == 0){
        $validMail = 1;
    } else {
        $validMail = 0;
    }
}

 

Link to comment
Share on other sites

It looks to me like they should both work just fine.

Just one thing to mention though. You could simplify the whole thing to:

if($wire->users->count("email=$em") === 0) {
    $validMail = 1;
}
else {
    $validMail = 0;
}

or in my mind, even better:

$validEmail = $wire->users->count("email=$em") === 0 ? true : false;

Of course you could do the 1 : 0, but I think true : false is more appropriate because then later on you can do:

if($validEmail) {

Note in both cases I am also using 'users' instead of 'pages' which is more descriptive in this case.

Also, using the PW count() method is much more efficient as it doesn't actually grab all the data, it just counts matches.

You can even take it one step further if you only need to check if it's a valid email just one time:

if($wire->users->count("email=$em") === 0) {
	//email is valid so do whatever here - maybe this is where you add them like:
	$u = new User()
	// etc etc
}

 

  • Like 5
Link to comment
Share on other sites

So I gave it a try now. I am a bit confused on this wire concept:

$validEmail = $wire->users->count("email=$em") == 0 ? true : false;
// result is NULL on valid and not valid Emails

if I try to wire inside a function I get a call to member function error??

function checkMail($em) {
    if($wire->users->count("email=$em") === 0) {
    $validMail = 1;
    }
    else {
        $validMail = 0;
    }
    return $validMail;
} 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...