I have two entities 'status' and 'doctor'. Every doctor can add a status in his profile. When the doctor wants to add a status, he can add status for another doctor because of the choice field .
It's not logic, I want that every doctor add only his status, not for other doctor.
How can I fix it ?
This is the status form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('medecin','entity',array('class'=>'DoctorBundle:medecin','property'=>'prenom','multiple'=>false))
->add('text')
->add('description')
->add('image',new ImageType())
;
}`
Ok, based on your comments I think you have several ways to tackle this:
Remove medecin
form field altogether and assign it later. For example:
$status = ....; // Your entity
$currentDoctor = ... // logic for getting myself :)
$status->setMedecin($currentDoctor);
$form = $this->createForm(StatusType(), $status);
$form->handleRequest($request);
if ( $form->isValid() ){
// The rest is the same
}
Bind data
attribute of medecin
form field. This further complicates things as you need to pass yourself (currentDoctor
) into the form type. I would definitely go for #1 above.
Hope this helps...
Remove the field Doctor (medecin for you) from the Form and then in the controller when you are handling the form response just do this
if ('POST' === $request->getMethod()) {
$statusForm->handleRequest($request);
if ($statusForm->isValid()) {
$status = $statusForm->getData();
$status->setDoctor($this->getUser());
$statusManager->flush($status);
}
}
$this->getUser() if you're logged in with the doctor, if you're not, get him however you are doing it.
I have this code in my status controller of create form:
/**
* Creates a form to create a statut entity.
*
* @param statut $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(statut $entity)
{
$form = $this->createForm(new statutType(), $entity, array(
'action' => $this->generateUrl('statut_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
`
i try this code its OK with the choice field but the Doctor-id take the value null, this is the code create form:$id= $entity->setMedecin(); $form = $this->createForm(new statutType(), $entity, array( 'action' => $this->generateUrl('statut_create'), 'method' => 'POST', ));