Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:16:36+00:00 2026-06-12T04:16:36+00:00

I want to convert a value before saving the form. I don’t know where

  • 0

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()

Could you please help me ?

Edit:

  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');
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:16:37+00:00Added an answer on June 12, 2026 at 4:16 am

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

    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;
      }
    }
    

    Then, update your action with this:

    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');
    }
    

    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);
    

    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);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert 8 digit value to a DateTime object. How can I
I want to convert a pointer *int to its real value int , in
In a SELECT statement I want to convert values to strings ie. SELECT TO_STRING(value).
I have some old mysql_query queries in my code which i want to convert
I want to convert a days-since-last-activity value in my SQL Server stored procedure to
I am need to convert a value in to decimal.Before that I am checking
I want to convert JSON into java code. My jsoncode as per given below.
I want to convert characters into integers based on predetermined values, for example: a
I have some double values I want to convert to a string with this
I want to make a function that takes an entered value and converts it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.