Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The @pankar answer is almost good. But the <code>$this-&gt;form-&gt;save()</code> will override the <code>setLink</code>.</p> <p>First of all, define the function to retrieve the youtube id, as defined <a href="https://stackoverflow.com/a/6556662/569101">here</a> in a new <em>tools</em> class: <code>lib/myTools.class.php</code></p> <pre><code>&lt;?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; } } </code></pre> <p>Then, update your action with this:</p> <pre><code>if ($this-&gt;form-&gt;isValid()) { // save the form $song = $this-&gt;form-&gt;save(); // update saved value $youtube_id = myTools::youtube_id_from_url($song-&gt;getLink()); $song-&gt;setLink($youtube_id); $song-&gt;save(); $this-&gt;getUser()-&gt;setFlash('notice', 'Thank you, the song has been added'); $this-&gt;redirect('@homepage'); } </code></pre> <p>By the way, this method is ok if you use your form in <strong>only one place</strong>. 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 <code>doUpdateObject</code> instead of <code>doSave</code>. Since the code says:</p> <pre><code> /** * 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); </code></pre> <p>So I will do it in this way, inside your form (<code>lib/form/doctrine/yourForm.class.php</code>):</p> <pre><code>class yourForm extends BaseYourForm { public function configure() { } protected function doUpdateObject($values) { $youtube_id = myTools::youtube_id_from_url($values['link']); $this-&gt;getObject()-&gt;setLink($youtube_id); return parent::doUpdateObject($values); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload