mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-28 18:43:49 +00:00
Adding validation on address validFrom date (unique by person)
This commit is contained in:
@@ -44,13 +44,16 @@ class PersonAddressController extends Controller
|
||||
->getRepository('ChillPersonBundle:Person')
|
||||
->find($person_id);
|
||||
|
||||
if ($person === NULL) {
|
||||
if ($person === null) {
|
||||
throw $this->createNotFoundException("Person with id $person_id not"
|
||||
. " found ");
|
||||
}
|
||||
|
||||
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person,
|
||||
"You are not allowed to edit this person.");
|
||||
$this->denyAccessUnlessGranted(
|
||||
'CHILL_PERSON_SEE',
|
||||
$person,
|
||||
"You are not allowed to edit this person."
|
||||
);
|
||||
|
||||
return $this->render('ChillPersonBundle:Address:list.html.twig', array(
|
||||
'person' => $person
|
||||
@@ -63,13 +66,16 @@ class PersonAddressController extends Controller
|
||||
->getRepository('ChillPersonBundle:Person')
|
||||
->find($person_id);
|
||||
|
||||
if ($person === NULL) {
|
||||
if ($person === null) {
|
||||
throw $this->createNotFoundException("Person with id $person_id not"
|
||||
. " found ");
|
||||
}
|
||||
|
||||
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
|
||||
"You are not allowed to edit this person.");
|
||||
$this->denyAccessUnlessGranted(
|
||||
'CHILL_PERSON_UPDATE',
|
||||
$person,
|
||||
"You are not allowed to edit this person."
|
||||
);
|
||||
|
||||
$address = new Address();
|
||||
|
||||
@@ -87,34 +93,48 @@ class PersonAddressController extends Controller
|
||||
->getRepository('ChillPersonBundle:Person')
|
||||
->find($person_id);
|
||||
|
||||
if ($person === NULL) {
|
||||
if ($person === null) {
|
||||
throw $this->createNotFoundException("Person with id $person_id not"
|
||||
. " found ");
|
||||
}
|
||||
|
||||
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
|
||||
"You are not allowed to edit this person.");
|
||||
$this->denyAccessUnlessGranted(
|
||||
'CHILL_PERSON_UPDATE',
|
||||
$person,
|
||||
"You are not allowed to edit this person."
|
||||
);
|
||||
|
||||
$address = new Address();
|
||||
|
||||
$form = $this->createCreateForm($person, $address);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$address = $form->getData();
|
||||
$person->addAddress($address);
|
||||
$person->addAddress($address);
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
$validatePersonErrors = $this->validatePerson($person);
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success',
|
||||
if (count($validatePersonErrors) !== 0) {
|
||||
foreach ($validatePersonErrors as $error) {
|
||||
$this->addFlash('error', $error->getMessage());
|
||||
}
|
||||
} elseif ($form->isValid()) {
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash(
|
||||
'success',
|
||||
$this->get('translator')->trans('The new address was created successfully')
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('chill_person_address_list', array(
|
||||
'person_id' => $person->getId()
|
||||
));
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('chill_person_address_list', array(
|
||||
'person_id' => $person->getId()
|
||||
));
|
||||
} else {
|
||||
$this->addFlash('error', $this->get('translator')
|
||||
->trans('Error! Address not created!'));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillPersonBundle:Address:new.html.twig', array(
|
||||
@@ -129,13 +149,16 @@ class PersonAddressController extends Controller
|
||||
->getRepository('ChillPersonBundle:Person')
|
||||
->find($person_id);
|
||||
|
||||
if ($person === NULL) {
|
||||
if ($person === null) {
|
||||
throw $this->createNotFoundException("Person with id $person_id not"
|
||||
. " found ");
|
||||
}
|
||||
|
||||
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
|
||||
"You are not allowed to edit this person.");
|
||||
$this->denyAccessUnlessGranted(
|
||||
'CHILL_PERSON_UPDATE',
|
||||
$person,
|
||||
"You are not allowed to edit this person."
|
||||
);
|
||||
|
||||
$address = $this->findAddressById($person, $address_id);
|
||||
|
||||
@@ -146,40 +169,52 @@ class PersonAddressController extends Controller
|
||||
'address' => $address,
|
||||
'form' => $form->createView()
|
||||
));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function updateAction($person_id, $address_id, Request $request)
|
||||
{
|
||||
$person = $this->getDoctrine()->getManager()
|
||||
->getRepository('ChillPersonBundle:Person')
|
||||
->find($person_id);
|
||||
->getRepository('ChillPersonBundle:Person')
|
||||
->find($person_id);
|
||||
|
||||
if ($person === NULL) {
|
||||
if ($person === null) {
|
||||
throw $this->createNotFoundException("Person with id $person_id not"
|
||||
. " found ");
|
||||
. " found ");
|
||||
}
|
||||
|
||||
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
|
||||
"You are not allowed to edit this person.");
|
||||
$this->denyAccessUnlessGranted(
|
||||
'CHILL_PERSON_UPDATE',
|
||||
$person,
|
||||
"You are not allowed to edit this person."
|
||||
);
|
||||
|
||||
$address = $this->findAddressById($person, $address_id);
|
||||
|
||||
$form = $this->createEditForm($person, $address);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->getDoctrine()->getManager()
|
||||
->flush();
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$validatePersonErrors = $this->validatePerson($person);
|
||||
|
||||
$this->addFlash('success', $this->get('translator')->trans(
|
||||
"The address has been successfully updated"));
|
||||
|
||||
return $this->redirectToRoute('chill_person_address_list', array(
|
||||
'person_id' => $person->getId()
|
||||
));
|
||||
if (count($validatePersonErrors) !== 0) {
|
||||
foreach ($validatePersonErrors as $error) {
|
||||
$this->addFlash('error', $error->getMessage());
|
||||
}
|
||||
} elseif ($form->isValid()) {
|
||||
$this->getDoctrine()->getManager()
|
||||
->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')->trans(
|
||||
"The address has been successfully updated"
|
||||
));
|
||||
|
||||
return $this->redirectToRoute('chill_person_address_list', array(
|
||||
'person_id' => $person->getId()
|
||||
));
|
||||
} else {
|
||||
$this->addFlash('error', $this->get('translator')
|
||||
->trans('Error when updating the period'));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillPersonBundle:Address:edit.html.twig', array(
|
||||
@@ -190,7 +225,6 @@ class PersonAddressController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Person $person
|
||||
* @param Address $address
|
||||
* @return \Symfony\Component\Form\Form
|
||||
@@ -256,4 +290,22 @@ class PersonAddressController extends Controller
|
||||
|
||||
return $addresses->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Chill\PersonBundle\Entity\Person $person
|
||||
* @return \Symfony\Component\Validator\ConstraintViolationListInterface
|
||||
*/
|
||||
private function validatePerson(Person $person)
|
||||
{
|
||||
$errors = $this->get('validator')
|
||||
->validate($person, array('Default'));
|
||||
$errors_addresses_consistent = $this->get('validator')
|
||||
->validate($person, array('addresses_consistent'));
|
||||
|
||||
foreach($errors_addresses_consistent as $error) {
|
||||
$errors->add($error);
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user