Forms Symfony2检查表单行的值
我需要向表中添加一些数据。主要思想是每个工人都有自己的身份证。这是每个工人自己设定的。它们都是弦 表中的结构: 名字 姓 电话号码 识别通行证 我的问题是: 我需要: 检查IDENTPASS的值是否为UNIQE 但每次我都想编辑这个表单,但改变了不同的行。我得到一个错误,因为他说IDENTPASS的值存在。因为他想再次保存,并且已经有了这个值 现在我有很多“如果”,这是我的坏主意。并不是所有的工作 我的控制器:Forms Symfony2检查表单行的值,forms,symfony,Forms,Symfony,我需要向表中添加一些数据。主要思想是每个工人都有自己的身份证。这是每个工人自己设定的。它们都是弦 表中的结构: 名字 姓 电话号码 识别通行证 我的问题是: 我需要: 检查IDENTPASS的值是否为UNIQE 但每次我都想编辑这个表单,但改变了不同的行。我得到一个错误,因为他说IDENTPASS的值存在。因为他想再次保存,并且已经有了这个值 现在我有很多“如果”,这是我的坏主意。并不是所有的工作 我的控制器: 好的,我不知道我是否涵盖了所有内容,但我想这可以开始了。请注意,我们使用相同的表格进
好的,我不知道我是否涵盖了所有内容,但我想这可以开始了。请注意,我们使用相同的表格进行insert nad更新。这决不是完全完成的代码,您可以即插即用,而是触及您可以做的表面
public function insertEditAction($id){
$employeeRepo = $this->getDoctrine()->getRepository('MainCoreBundle:Employee');
if (!is_null($id)){
$employee=$employeeRepo->find($id); // For update
}else{
$employee = new Employee(); // For insert
}
$employeeForm = $this->createForm(new EmployeeType(), $employee);
if ( $request->getMethod() === "POST"){
$form->bind($request);
if ( $form->isValid()){
$em = $this->getDoctrine()->getManager();
$existingByIdentPass = $employeeRepo->findOneByIdentpass($employee->getIdentpass());
// If there is some other employee with this IDENT_PASS then redirect back to form
if($existingByIdentPass !== NULL && $existingByIdentPass->getId() !== $employee->getId()){
return $this->redirect($this->generateUrl('employee_edit', array('id' => $id)));
}
// New employee, set the SPOT and PERSIST
// calling persist is only required when creating new record
if ( $employee->getId() === NULL ){
$employee->setSpot($spot);
$em->persist($employee);
}
// Flush the changes
$em->flush();
return $this->redirect($this->generateUrl('employee_index'));
}
}
return $this->render('MainBundle:Employee:entity.html.twig', $this->getViewConstants(array('form' => $employeeForm->createView(), 'pass'=>$passFromBase)));
}
好的,这里有几件事你可以改进。。。现在写答案。。。
public function insertEditAction($id){
$employeeRepo = $this->getDoctrine()->getRepository('MainCoreBundle:Employee');
if (!is_null($id)){
$employee=$employeeRepo->find($id); // For update
}else{
$employee = new Employee(); // For insert
}
$employeeForm = $this->createForm(new EmployeeType(), $employee);
if ( $request->getMethod() === "POST"){
$form->bind($request);
if ( $form->isValid()){
$em = $this->getDoctrine()->getManager();
$existingByIdentPass = $employeeRepo->findOneByIdentpass($employee->getIdentpass());
// If there is some other employee with this IDENT_PASS then redirect back to form
if($existingByIdentPass !== NULL && $existingByIdentPass->getId() !== $employee->getId()){
return $this->redirect($this->generateUrl('employee_edit', array('id' => $id)));
}
// New employee, set the SPOT and PERSIST
// calling persist is only required when creating new record
if ( $employee->getId() === NULL ){
$employee->setSpot($spot);
$em->persist($employee);
}
// Flush the changes
$em->flush();
return $this->redirect($this->generateUrl('employee_index'));
}
}
return $this->render('MainBundle:Employee:entity.html.twig', $this->getViewConstants(array('form' => $employeeForm->createView(), 'pass'=>$passFromBase)));
}