0

I have literally tried everything to try and extend the Symfony2 Form class.

I want to add a new method to Form and call it in a controller:

$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. I don't want to hack the core.

I could extend the controller class, e.g. the createForm() method returns the instantiated Form object:

// 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? 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).

I only need to add this for when the form is used in a controller. I don't need it for any CLI processing.

Any ideas how I can add a method to the Form class without hacking the core?

====================================================================

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.

Community
  • 1
  • 1
user2143356
  • 5,467
  • 19
  • 51
  • 95
  • I'm not really familiar with Symfony, but if you want to extend classes you just do it by subclassing them (unless they're declared final, which is rarely done). Can't you do `class MyForm extends SymfonyForm`? – GordonM Sep 29 '14 at 15:51
  • Subclass the form.factory service too and replace the instantiation of Form with your own class there. – user3557327 Sep 29 '14 at 16:25
  • I think you should be handling this with either FormEvents or the Validator component – Peter Bailey Sep 30 '14 at 12:24

1 Answers1

0

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;
            }
        }
shacharsol
  • 2,326
  • 20
  • 14