Yii AngularJS POST / JSON validation

Отправить данные методом POST с AngularJS в Yii  и получить результаты валидации с Yii в JSON.

HTML

{{ message }}
{{ errorName }}
{{ errorPassword }}
{{ errorEmail }}

JavaScript


function($scope, $http) {
$scope.formData = {};
$scope.processForm = function() {
$http({
method : 'POST',
url : '/site/signupjson',
data : $.param($scope.formData), // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
})
.success(function(data) {
console.log(data);

if (!data.success) {
// if not successful, bind errors to error variables
$scope.errorName = (typeof data.errors.name !== "undefined") ? data.errors.name[0] : '';
$scope.errorPassword = (typeof data.errors.password !== "undefined") ? data.errors.password[0] : '';
$scope.errorEmail = (typeof data.errors.email !== "undefined") ? data.errors.email[0] : '';
} else {
// if successful, bind success message to message
$scope.message = data.message;
}
});
};

}

PHP


public function actionSignupJson()
{
$errors = array();
$data = array();

$model = new AccountData(AccountData::SCENARIO_SIGNUP);
if(isset($_POST['name'])) { $model->name = $_POST['name']; }
if(isset($_POST['password'])) { $model->password = $_POST['password']; }
if(isset($_POST['email'])) { $model->email = $_POST['email']; }
if($model->save()) {
$data['success'] = true;
$data['message'] = 'Success!';
} else {
$data['success'] = false;
$data['errors'] = $model->getErrors();
}

echo CJSON::encode($data);
Yii::app()->end();

}