Hi,
I’ve been hunting this all weekend. No success.
I’m converting my Yii 1 to a Yii 2 web application.
I have my custom exception:
namespace app\dao\user;
use yii\base\Exception;
class EmailDuplicateException extends Exception
{
}
My DAO
public function createUser()
{
try
{
$sql = "INSERT INTO ";
$command = Yii::$app->db->createCommand($sql);
$command->bindValue(":first_name", $this->daoRequest->firstName);
$command->bindValue(":last_name", $this->daoRequest->lastName);
$command->bindValue(":email", $this->daoRequest->email);
$command->bindValue(":password_hash", $this->daoRequest->passwordHash);
$rows = $command->execute();
}
catch ( IntegrityException $e )
{
$this->exceptionLogging( $e );
if ( strpos( $e->errorInfo[2], 'email_unq' ) !== false )
{
throw new EmailDuplicateException( $e );
}
}
}
My Bo catches the exception. Sets a few values and returns to the controller.
public function createUser()
{
try
{
$returnValue = $this->dao->createUser();
return true;
}
catch ( EmailDuplicateException $e )
{
$this->boResponse->errors[] = Yii::t( "app", "text.error.duplicate.email" );
$this->boResponse->focus = "firstName";
return false;
}
}
My controller
public function actionNewsave()
{
$request = Yii::$app->request;
$bo = new UserBo();
$bo->boRequest = new UserBoRequest();
$bo->boResponse = new UserBoResponse();
$bo->dao = new UserDao();
// retrieve posted data
$request = Yii::$app->request;
$bo->boRequest->firstName = $request->post('firstName');
$bo->boRequest->lastName = $request->post('lastName');
$bo->boRequest->email = $request->post('email');
$bo->boRequest->password = $request->post('password');
$bo->boRequest->passwordConfirmation = $request->post('passwordConfirmation');
// create the user
$returnValue = $bo->createUser();
// return data
$responseData = array();
if ( !$returnValue )
{
$responseData["focus"] = $bo->boResponse->focus;
$responseData["errors"] = $bo->boResponse->errors;
$responseData["status"] = "fail";
}
else
{
$responseData["status"] = "success";
}
exit( json_encode(
$responseData
));
}
Finally, my code in the web page is this.
The issue is that when there is an EmailDuplicateException the ERROR function is called on the AJAX call.
I do not want this, I want to handle the messaging myself in the success function.
$( ‘#saveBtn’ ).click (function() {
var formData =
$.ajax({
url: "/lescapsules/web/index.php?r=site/newsave",
type: "POST",
dataType: "json",
data: {
firstName: $( "#firstName" ).val(),
lastName: $( "#lastName" ).val(),
email: $( "#email" ).val(),
password: $( "#password" ).val(),
passwordConfirmation: $( "#passwordConfirmation" ).val(),
_csrf: "<?=Yii::$app->request->getCsrfToken()?>"
},
success: function( responseData ) {
processCall( responseData );
},
error: function(err){
alert( err );
console.log("server error");
}
});
return false;
});
I have put these statements in the index.php file. This has no effect.
defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER', false);
defined('YII_ENABLE_EXCEPTION_HANDLER') or define('YII_ENABLE_EXCEPTION_HANDLER', false);
How can I get this to work the way I want it to?
Thank you, any feedback is greatly appreciated.
Glenn