开发者

Best practice for handling add/edit/delete forms

开发者 https://www.devze.com 2023-04-09 14:49 出处:网络
i\'m a little stuck of how i should approach forms in my zend-application. Right now i\'m creating two separate forms for addAction() and editAction() for each object i need. My delete-formular is alw

i'm a little stuck of how i should approach forms in my zend-application. Right now i'm creating two separate forms for addAction() and editAction() for each object i need. My delete-formular is always the same, i simply change some attributes on those.

What i'm wondering about: What is a best-practice here?

Personally i think it could be better just have Forms like "Object1, Object2" instead of "Object1Add, "Object1Edit, Object2Add, Object2Edit"

The 开发者_如何学Creson i ask is because i clearly need two different forms. Some elements are NOT to get changed at all, so i don't want the user to see them.

A Method could be to $form->removeElement('X') inside the controllers editAction()

Why am i thinking this? Well simple errors - i may add one field here and forget it in the second form or display fields with tag 'disabled=0' in one form but don't have that query on the other one.

Hope i'm making myself clear enough :) Thank you in advance!


I assume that you do extend the form class. I'd add functions called edit or add to your class that does the corresponding logic. This way you keep your form-logic in your form and not in your controller.


This is a perfect time to use the Factory Design Pattern. Don't create the form using the contructor, use factory methods instead. Here is a quick pseudocode layout:

static createAdd()
  form = new self()
  call form->initCommon
  call form->initAdd
  return form

static createEdit()
  form = new self()
  call form->initCommon
  call form->initEdit
  return form

initComon()
  add the common+validators elements

initAdd()
  add the elements+validators only used in Add

initEdit()
  add the elements+validators only used in Edit

Makes sense?


I would exactly do what you said : 1 form with all elements. Then depending on the case, just use removeElement method on the form to remove the elements that you don't need.

0

精彩评论

暂无评论...
验证码 换一张
取 消