0

I'm trying to modify the content of the response from some actions in Symfony 1.4.

When I process some forms, and after some conditions, in processForm method (the one called for form binding, and giving the final result of the form processing operation).

I tried

function processForm(sfWebRequest $request, sfForm $form)
{
   $form->bind(...);
   // ... some validations/operations ...

   $cont = $this->getResponse()->getContent();
   $this->getResponse()->setContent("MODIFICATION!" . $cont);
   $this->getResponse()->sendContent();
}

but this currently sends me a "Cannot modify header information - headers already sent" error...

Is this at all possible? or am I talking nonsense? Any ideas?

Thank you!

EDIT: What I am trying to do is the following.

In the executeNew action, I have to make some validations. All the field specific validations are correctly done with the corresponding form validators.

But there is a validation that takes into account the value of 2 of the fields at the same time, not just one of them. So, haven't found a way to make a validator for this two fields at the same time, I decided to validate by overriding the isValid method of the form, calling the parent::isValid method to, and validating my 2-field condition here too. When validation fails, I return false, as isValid() should. But also, I haven't found how to write the Error message near the form when this particular error happens. That's why I wish to alter the content of the Response, so I can add a customized error message for this particular error condition, only when it happens...

Hope this helps to clarify my post...

Dmitri
  • 28,702
  • 8
  • 32
  • 51
Javier Novoa C.
  • 8,734
  • 10
  • 47
  • 71

1 Answers1

2

EDIT:

What you need to do is create a post or pre validator. This will allow you to validate two fields at once for some kind of additional condition. Its hard to say how to implement since we dont know your validation rules but the following docs should help: http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms#chapter_06_ignoring_embedded_forms

That show you how to make a postValidator and use it on a form. Sometimes though you dont need to reuse this code so creating a class is overkill. In that case you can jsut create a custom method on your form and use a sfValidatorCallback.. for example:

class myForm {


  public function configure()
  {
     // normal config stuff

    $this->mergePostValidator(new sfValidatorCallback(array(
      'callback' => array($this, 'postValidate')
    )));
  }

  public function postValidate($validator, $values, $arguments)
  {
     // your post validator code
  }
}

Doing it this way will allow you to define error messages as you normally would for another validator.


processForm isnt an action youre forwarding to so the action calling it is still going to try and send the content and what not as normal. The problem is sendContent actually echoes the content to the browser... so when the calling action tries to send headers and content again as its expected to do you get the typical headers already sent error.

If you need to send the content immediately then throw a sfStopExecutionException after sendContent. But i doubt thats what you really want to do. PRobably the best thing to do is to not send the content but allow control to return tot the calling action as normal and then handle sending the content from there.

You could also attach a listener to the response.filter_content event, but in this situation im not sure if that will do much good. If you give an better description of what you are trying to accomplish beyond adding a test string we could give you better direction.

prodigitalson
  • 58,127
  • 8
  • 92
  • 110
  • I tried to send content from the calling action, but didn't work, the error was the same... – Javier Novoa C. Feb 16 '11 at 15:50
  • Thank you very much! your tip in the **EDIT** has done it! Thank you very much.... I guess the title of the post is currently not reflecting the current state of the problem/solution, guess I'll edit that too... – Javier Novoa C. Feb 16 '11 at 16:55
  • wow, just stumbled with a little problem... I used this same thing with a New action/form but ading the postValidator gives me a: "Invalid argument supplied for foreach() in path/to/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php on line 169 ... and it just stores me Null values in my Database. Trying to inspect $form->getValues in the processForm gives me no values :o any idea why is this? – Javier Novoa C. Feb 16 '11 at 20:20
  • Wed need to see your action and validator code. You should create a new question and post the code. – prodigitalson Feb 16 '11 at 21:17
  • you are right... thanks. http://stackoverflow.com/questions/5022761/symfony-1-4-using-post-validator-in-new-action-throws-error – Javier Novoa C. Feb 16 '11 at 22:19