开发者

How to call javascript function if form invalid in Symfony

I would like to call a javascript function in Symfony after I validate the form, and it开发者_C百科 was invalid. Where can I call this function?


You would need to make the call from your view. How exactly you want to do that depends on what youre trying to achieve.

For example:

public function executeFormSubmitAction(sfWebRequest $request)
{
    $this->form = new MyForm();
    $this->form->bind($request->getParamter($$this->form->getName());
    $this->valid = null;

    if($this->form->isValid())
    {
       // do stuff with values

       $this->valid = true;
    }
    else
    {
       $this->valid = false;
    }
}

And then in your view:

<?php if($valid === true): ?>
  Thanks!
<?php else: ?>
  <?php echo $form; ?>
  <?php if(false === $valid): ?>
    <!-- your javascript call and related stuff -->
  <?php endif; ?>
<?php endif; ?>

IF there is an entirely different structure for the views of each condition for $valid then you might want to use actuall put all that in partials and then use include_partial inside the conditions, or you could use seperate view templates and use setTemplate in your action to render the correct one... Or if they are really complex and require special logic you could use forward and seperate actions entirely. Or if this is an ajax submission you could return json instead of html and then execute your js function as part of the callback - checking the returned json for a variable flagging the result of validation.

Its really up to you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜