如何在保存表单之前转换值? [英] How to convert a value before saving the form?

查看:31
本文介绍了如何在保存表单之前转换值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在保存表单之前转换一个值.我不知道在哪里添加代码,因为唯一的方法是 form->save()

I want to convert a value before saving the form. I don't know where to add the code because the only method is form->save()

你能帮我吗?

  if ($this->form->isValid())
  {
    $url = $this->form->getObject()->getLink();
    parse_str(parse_url($url, PHP_URL_QUERY), $my_array_of_vars);
    $this->form->getObject()->setLink($my_array_of_vars['v']); 

    $song = $this->form->save();

    $this->getUser()->setFlash('notice', 'Thank you, the song has been added');

    $this->redirect('@homepage');
  }

推荐答案

@pankar 的回答几乎是好的.但是 $this->form->save() 将覆盖 setLink.

The @pankar answer is almost good. But the $this->form->save() will override the setLink.

首先,定义检索 youtube id 的函数,如此处在新工具中的定义 类:lib/myTools.class.php

First of all, define the function to retrieve the youtube id, as defined here in a new tools class: lib/myTools.class.php

<?php

class myTools
{
  /**
   * Get youtube video ID from URL
   *
   * @see https://stackoverflow.com/a/6556662/569101
   * @param string $url
   * @return string Youtube video id or FALSE if none found. 
   */
  public static function youtube_id_from_url($url)
  {
    $pattern = 
      '%^# Match any youtube URL
      (?:https?://)?  # Optional scheme. Either http or https
      (?:www\.)?      # Optional www subdomain
      (?:             # Group host alternatives
        youtu\.be/    # Either youtu.be,
      | youtube\.com  # or youtube.com
        (?:           # Group path alternatives
          /embed/     # Either /embed/
        | /v/         # or /v/
        | /watch\?v=  # or /watch\?v=
        )             # End path alternatives.
      )               # End host alternatives.
      ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
      $%x'
      ;
    $result = preg_match($pattern, $url, $matches);
    if (false !== $result)
    {
      return $matches[1];
    }
    return false;
  }
}

然后,更新您的操作:

if ($this->form->isValid())
{
  // save the form
  $song = $this->form->save();

  // update saved value
  $youtube_id = myTools::youtube_id_from_url($song->getLink());
  $song->setLink($youtube_id);
  $song->save();

  $this->getUser()->setFlash('notice', 'Thank you, the song has been added');

  $this->redirect('@homepage');
}

顺便说一下,如果您只在一个地方使用您的表单,则此方法是可以的.由于更新是在操作中执行而不是在表单类中执行.否则,正如@glerendegui 所说,您将不得不在表单类中执行此操作.但我宁愿在 doUpdateObject 中而不是 doSave 中进行.由于代码说:

By the way, this method is ok if you use your form in only one place. Since the update is perform in the action and not in the form class. Otherwise, as @glerendegui said, you will have to do this action in the form class. But I will rather do it in the doUpdateObject instead of doSave. Since the code says:

  /**
   * Updates the values of the object with the cleaned up values.
   *
   * If you want to add some logic before updating or update other associated
   * objects, this is the method to override.
   *
   * @param array $values An array of values
   */
  abstract protected function doUpdateObject($values);

所以我会以这种方式在你的表单中(lib/form/doctrine/yourForm.class.php):

So I will do it in this way, inside your form (lib/form/doctrine/yourForm.class.php):

class yourForm extends BaseYourForm
{
  public function configure()
  {
  }

  protected function doUpdateObject($values)
  {
    $youtube_id = myTools::youtube_id_from_url($values['link']);
    $this->getObject()->setLink($youtube_id);

    return parent::doUpdateObject($values);
  }
}

这篇关于如何在保存表单之前转换值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆