开发者

symfony 1.4 validate two fields at the same time [was "modify response content in processForm"]

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()->sendC开发者_如何学编程ontent();
}

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...


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜