[英]I just can't extend the Symfony2 Form class. Tried everything
I have literally tried everything to try and extend the Symfony2 Form class. 我已经尝试了一切尝试来扩展Symfony2 Form类。
I want to add a new method to Form and call it in a controller: 我想向Form中添加一个新方法并在控制器中调用它:
$form = $this->createForm($this->get('my_form_service'), $entity);
$form->myNewMethod();
Because the Form class isn't defined as a service and is instantiated in another class (FormBuilder, line 221) then I can't see what I can do. 因为Form类没有定义为服务,而是在另一个类(FormBuilder,第221行)中实例化的,所以我看不到我能做什么。 I don't want to hack the core.
我不想破解核心。
I could extend the controller class, eg the createForm() method returns the instantiated Form object: 我可以扩展控制器类,例如createForm()方法返回实例化的Form对象:
// Extend Controller and do something with Form before returning
public function createForm($type, $data = null, array $options = array())
{
return $this->container->get('form.factory')->create($type, $data, $options);
In fact How to add a new method to a php object on the fly? 实际上, 如何动态地向php对象添加新方法? shows how I could do the above and add my new method that way, but my understanding is that you need to at least add __call to the Form class - which I can't do - or the added method won't be able to use $this (which I need).
展示了如何执行上述操作并以这种方式添加新方法,但是我的理解是,您至少需要将__call添加到Form类中-我不能这样做-否则添加的方法将无法使用$ this(我需要)。
I only need to add this for when the form is used in a controller. 我只需要在控制器中使用表单时添加它。 I don't need it for any CLI processing.
我不需要任何CLI处理。
Any ideas how I can add a method to the Form class without hacking the core? 有什么想法可以在不破坏核心的情况下向Form类添加方法吗?
==================================================================== ================================================== ==================
EDIT: Why am I doing this? 编辑:为什么我要这样做?
I want to 'silently fail' a form submit if a certain criteria is met. 如果要满足某些条件,我想“悄无声息”提交表单。 I believe the best way to do this would be to do this in a controller:
我相信最好的方法是在控制器中执行此操作:
if ($form->isValid()) {
if ($form->requiresSilentFail()) {
// Silently fail
} else {
// As normal, add to DB etc.
}
}
I suppose I could just do something like this: 我想我可以做这样的事情:
if ($form->isValid()) {
if ($this->get('check_my_form')->requiresSilentFail($form)) {
// Silently fail
} else {
// As normal, add to DB etc.
}
}
.... but I need to also perform a little bit of extra logic in Form::handleRequest() first, so I believe extending Form is the only option. ....但我还需要先在Form :: handleRequest()中执行一些额外的逻辑,所以我认为扩展Form是唯一的选择。
I don't really understand why you need to extend the form class simply on your controller/service where you process the form call your method under is valid check, see below: 我真的不明白为什么您只需要在控制器/服务上扩展表单类,在其中处理表单调用的方法就是有效检查,请参见下文:
$form->handleRequest($request);
if ($form->isValid()) {
$this->get($apiResource->getResourceHandlerName())->update($resourceData);
$response = new Response();
$response->setStatusCode($statusCode);
if ($this->isSuccessFullStatusCode($statusCode)) {
$serializedObject = $this->getSerializer()->serialize($resourceData, 'json',$serializationContext);
$response->setStatusCode($responseStatusCode);
$response->setContent($serializedObject);
return $response;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.