How to Use Method to update form

I have a form with the following fields.

Job Title
JobURLSlug
Job Descriptiong.

I want JobURLSlug to be a label field.

On update, I want to put Job Title in this function to Return the JobURLSlug (a search engine friendly url version of job title).

function generateSlug($phrase, $maxLength)
{
  $result = strtolower($phrase);

  $result = preg_replace("/[^a-z0-9s-]/", "", $result);
  $result = trim(preg_replace("/[s-]+/", " ", $result));
  $result = trim(substr($result, 0, $maxLength));
  $result = preg_replace("/s/", "-", $result);

  return $result;
}

Under programming I see I can add methods but I don’t see any examples of how to use this or specify variables.

Can someone show me the steps?
I created a Variable for the Job Title as var_job_title.

What is the format of inputing the parameters into the method? After that, how/where do I call the method?
I am a seasoned PHP programmer but learning the scriptcase interface is difficult. I would appreciate some help.

Re: How to Use Method to update form

Hi,

in your form / Events / onBeforeInsert and onBeforeUpdate put your code without function / return:



$maxLength = ??;

$result = strtolower({JobTitle});

$result = preg_replace("/[^a-z0-9s-]/", "", $result);
$result = trim(preg_replace("/[s-]+/", " ", $result));
$result = trim(substr($result, 0, $maxLength));
$result = preg_replace("/s/", "-", $result);

{JobURLSlug} = $result;

{JobURLSlug} is the fieldname.

Re: How to Use Method to update form

Thank you!

I will be happy to do it this way as it works.
But it would be nice to know how to use functions/methods should the need arise. :slight_smile:

Re: How to Use Method to update form

Open the form, in the end of the left menu there a tab to create new method …

why dont you create a method called generateSlug and put your single function instead of duplicate this code in every event?

and in the validate event you can just call

{jobUrlSlug} = generateSlug(…);

Re: How to Use Method to update form

Hi Diogo,

you are right , but that was not the question …

Re: How to Use Method to update form

He asked to use functions/methods … just create the methods, define the parameters clicking in the icon “var” … and only this.