We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19388
    • 297 Posts
    Hi People,
    I have a problem with the API to make a simple username validation. Here is the code:

    <?php
    $error= array();
    $errorTxt= array();
    $errorMessage='';
    
    if(strlen($_POST['username'])<6) {
    	$error[] = "username";
    	$errorTxt[] = "El nombre de usuario debe tener más de 5 caracteres";
    }
    
    if(!preg_match("/[^a-zA-Z0-9]+$/s",$_POST['username'] )) {
    	$error[] = "username";
    	$errorTxt[] = "El nombre de usuario debe contener sólo números y letras";
    }
    
    if (count($error)>0) {
    	for ($i=0;$i<count($errorTxt);$i++) {
    		$errorMessage .=$errorTxt[$i];
    		if ($i<count($errorTxt)-1) {
    			$errorMessage.='.<br/> ';
    		}
    	}
    	echo $errorMessage;
    return false;
    }
    
    ?>


    What I want to do is, if an error is detected ( short username o invalid username ) to stop executing the snippet, dont save the user ( onBeforeSaveUser event ) and display the error message.
    The way I’ve done it, I show the message, but the snippet continue being executed and the user is registered. The example I used to do my implementation ( http://modxcms.com/forums/index.php?topic=34924.0 ) used $wlpe->FormatMessage($errorMessage); instead my "echo" and "return false;"; but to use it I have to do an small hack I don’t want to do. Is there another way?
    Thanks in advance
      • 19388
      • 297 Posts
      Any help? sad