Commit 9fe0ae18 authored by Julien Fastré's avatar Julien Fastré

improve person creation to fix error handling

parent 04c56f66
......@@ -170,12 +170,12 @@ class PersonController extends Controller
->getGroupCenters()[0]
->getCenter();
$person = (new Person())
$person = (new Person(new \DateTime('now')))
->setCenter($defaultCenter);
$form = $this->createForm(
new CreationPersonType(CreationPersonType::FORM_NOT_REVIEWED),
array('creation_date' => new \DateTime(), 'center' => $defaultCenter),
$person,
array('action' => $this->generateUrl('chill_person_review'))
);
......@@ -197,18 +197,22 @@ class PersonController extends Controller
*/
private function _bindCreationForm($form)
{
$date = new \DateTime();
$person = new Person($form['creation_date']->getData());
$date_of_birth = new \DateTime();
$person->setFirstName($form['firstName']->getData())
->setLastName($form['lastName']->getData())
->setGender($form['gender']->getData())
->setBirthdate($form['birthdate']->getData())
->setCenter($form['center']->getData())
;
/**
* @var Person
*/
$person = $form->getData();
$periods = $person->getAccompanyingPeriodsOrdered();
$period = $periods[0];
$period->setOpeningDate($form['creation_date']->getData());
// $person = new Person($form['creation_date']->getData());
//
// $person->setFirstName($form['firstName']->getData())
// ->setLastName($form['lastName']->getData())
// ->setGender($form['gender']->getData())
// ->setBirthdate($form['birthdate']->getData())
// ->setCenter($form['center']->getData())
// ;
return $person;
}
......@@ -251,15 +255,18 @@ class PersonController extends Controller
$form = $this->createForm(
new CreationPersonType(CreationPersonType::FORM_BEING_REVIEWED),
null, array('action' => $this->generateUrl('chill_person_create')));
new Person(),
array('action' => $this->generateUrl('chill_person_create')));
$form->handleRequest($request);
$person = $this->_bindCreationForm($form);
var_dump($person);
$errors = $this->_validatePersonAndAccompanyingPeriod($person);
if ( count($errors) > 0) {
$this->get('logger')->info(sprintf('Person created with %d errors ', count($errors)));
if ($errors->count() > 0) {
$this->get('logger')->info('The created person has errors');
$flashBag = $this->get('session')->getFlashBag();
$translator = $this->get('translator');
......@@ -271,11 +278,14 @@ class PersonController extends Controller
$form = $this->createForm(
new CreationPersonType(CreationPersonType::FORM_NOT_REVIEWED),
array('action' => $this->generateUrl('chill_person_create')));
new Person(),
array('action' => $this->generateUrl('chill_person_review')));
$form->handleRequest($request);
return $this->_renderNewForm($form);
} else {
$this->get('logger')->info('Person created without errors');
}
$em = $this->getDoctrine()->getManager();
......@@ -331,7 +341,7 @@ class PersonController extends Controller
$form = $this->createForm(new CreationPersonType());
$form->handleRequest($request);
var_dump($form->getData());
$person = $this->_bindCreationForm($form);
$errors = $this->_validatePersonAndAccompanyingPeriod($person);
......@@ -349,7 +359,11 @@ class PersonController extends Controller
return $this->redirect($this->generateUrl('chill_person_general_edit',
array('person_id' => $person->getId())));
} else {
$r = new Response('this should not happen if you reviewed your submission');
$text = "this should not happen if you reviewed your submission\n";
foreach ($errors as $error) {
$text .= $error->getMessage()."\n";
}
$r = new Response($text);
$r->setStatusCode(400);
return $r;
}
......
......@@ -24,9 +24,7 @@ namespace Chill\PersonBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Chill\PersonBundle\Form\Type\CivilType;
use Chill\PersonBundle\Form\Type\GenderType;
use CL\BelgianNationalNumberBundle\Form\BelgianNationalNumberType;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
class CreationPersonType extends AbstractType
......@@ -53,20 +51,26 @@ class CreationPersonType extends AbstractType
if ($this->form_status === self::FORM_BEING_REVIEWED) {
$dateToStringTransformer = new DateTimeToStringTransformer(
null, null, 'dd-MM-yyyy', true);
null, null, 'd-m-Y', false);
$builder->add('firstName', 'hidden')
->add('lastName', 'hidden')
->add( $builder->create('birthdate', 'hidden')
->addModelTransformer($dateToStringTransformer)
)
->add('birthdate', 'hidden', array(
'property_path' => 'birthdate'
))
->add('gender', 'hidden')
->add( $builder->create('creation_date', 'hidden')
->addModelTransformer($dateToStringTransformer)
)
->add('form_status', 'hidden')
->add('creation_date', 'hidden', array(
'mapped' => false
))
->add('form_status', 'hidden', array(
'mapped' => false
))
->add('center', 'center')
;
$builder->get('birthdate')
->addModelTransformer($dateToStringTransformer);
$builder->get('creation_date', 'hidden')
->addModelTransformer($dateToStringTransformer);
} else {
$builder
->add('firstName')
......@@ -76,10 +80,16 @@ class CreationPersonType extends AbstractType
->add('gender', new GenderType(), array(
'required' => true, 'empty_value' => null
))
->add('creation_date', 'date', array('required' => true,
'widget' => 'single_text', 'format' => 'dd-MM-yyyy',
->add('creation_date', 'date', array(
'required' => true,
'widget' => 'single_text',
'format' => 'dd-MM-yyyy',
'mapped' => false,
'data' => new \DateTime()))
->add('form_status', 'hidden', array('data' => $this->form_status))
->add('form_status', 'hidden', array(
'data' => $this->form_status,
'mapped' => false
))
->add('center', 'center')
;
}
......@@ -94,9 +104,9 @@ class CreationPersonType extends AbstractType
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
// $resolver->setDefaults(array(
// 'data_class' => 'Chill\PersonBundle\Entity\Person'
// ));
$resolver->setDefaults(array(
'data_class' => 'Chill\PersonBundle\Entity\Person'
));
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment