Last active
May 2, 2016 22:42
-
-
Save AliHichem/f1966e7d11288d108869 to your computer and use it in GitHub Desktop.
angularjs sf2 form form_error has_error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.factory('Base', function ($http, $q, $location) { | |
/** | |
* handleError | |
* @param {type} response | |
* @returns {unresolved} | |
*/ | |
var handleError = function (response) { | |
if (!angular.isObject(response.data) || !response.data.message | |
) { | |
return ($q.reject("An unknown error occurred.")); | |
} | |
return ($q.reject(response.data)); | |
}; | |
/** | |
* handleSuccess | |
* @param {type} response | |
* @returns {unresolved} | |
*/ | |
var handleSuccess = function (response) { | |
return (response.data); | |
}; | |
/** | |
* Public | |
*/ | |
return { | |
handleError: handleError, | |
handleSuccess: handleSuccess, | |
}; | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$scope.save = function () { | |
var data = $('#PatientFormCtrl').serialize(); | |
Patient.save(data) | |
.then(function (res) { | |
console.log('succes', res); | |
}, function (res) { | |
$scope.errors = res.errors; | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use FOS\RestBundle\Controller\Annotations as Rest; | |
use FOS\RestBundle\View\View; | |
class PatientController { | |
/** | |
* save action | |
* | |
* @Rest\Post | |
* @Rest\View | |
*/ | |
public function saveAction(Request $req) | |
{ | |
$em = $this->sp()->getEntityManager(); | |
if ($req->get('patient_id')) | |
{ | |
if (!$p = $em->find('CareConnectCoreBundle:Patient', $req->get('patient_id'))) | |
{ | |
throw $this->createNotFoundException(sprintf('cannot find the patient [%s]', $req->get('patient_id'))); | |
} | |
} | |
else | |
{ | |
$p = (new Patient()); | |
} | |
$form = $this->createForm(new PatientType(), $p); | |
if ($req->getMethod() == 'POST') | |
{ | |
$form->submit($req); | |
if ($form->isValid()) | |
{ | |
$em->persist($p); | |
$em->flush(); | |
return $p; | |
} | |
} | |
return View::create($form, 400); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* SF2 form_error | |
*/ | |
.directive('formError', ['$compile', function ($compile) { | |
return { | |
restrict: 'A', | |
replace: true, | |
link: function (scope, element, attrs) { | |
var field = attrs.formError; | |
scope.$watch('errors', function (errors, _errors) { | |
element.html(''); | |
var path = 'errors.children.' + field.replace('.', '.children.').replace('[', '.children[') + '.errors'; | |
if (errors && eval(path)) { | |
// show only the first error because everyone hates errors :) | |
var msg = eval(path)[0]; | |
var html = '<span class="help-block has-error">' + msg + '.</span>'; | |
element.html(html).show(); | |
// compile needed only if you have angular element/hooks in your html variable | |
$compile(element.contents())(scope); | |
} | |
}); | |
} | |
}; | |
}]) | |
/** | |
* SF2 has_error | |
*/ | |
.directive('hasError', ['$compile', function ($compile) { | |
return { | |
restrict: 'A', | |
replace: true, | |
link: function (scope, element, attrs) { | |
var field = attrs.hasError; | |
var path = 'errors.children.' + field.replace('.', '.children.').replace('[', '.children[') + '.errors'; | |
scope.$watch('errors', function (errors, _errors) { | |
element.removeClass('has-error'); | |
if (errors && eval(path)) { | |
element.addClass('has-error'); | |
} | |
}); | |
} | |
}; | |
}]) | |
/** | |
* SF2 is_granted | |
*/ | |
.directive('isGranted', ['$compile', 'AclService', function ($compile, AclService) { | |
return { | |
restrict: 'A', | |
replace: true, | |
link: function (scope, element, attrs) { | |
var isGranted = false; | |
var roles = scope.user.roles; | |
var acl = scope.acl; | |
var field = attrs.isGranted; | |
AclService.setAbilities(acl); | |
for (var r in roles) { | |
AclService.attachRole(roles[r]); | |
} | |
if (!AclService.can(field)) { | |
element.remove(); | |
} | |
} | |
}; | |
}]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.factory('Patient', function ($http, $q, Base) { | |
/** | |
* save | |
* @param params | |
* @returns {*} | |
*/ | |
var save = function (params) { | |
var request = $http({ | |
method: 'POST', | |
url: Routing.generate('api_patient_save'), | |
data: params, | |
headers: {'Content-Type': 'application/x-www-form-urlencoded'} | |
}); | |
return (request.then(Base.handleSuccess, Base.handleError)); | |
}; | |
/** | |
* Public | |
*/ | |
return { | |
save: save, | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment