This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

lucas.sanner54070
lucas.sanner54070

I'd like to check some form values before a page is saved on the backend. Is it possible to do that from the boot() function of my plugin ?

ivan_g
ivan_g

I normally use the public function beforeSave() in the model related to the table where you are saving the form values. For example in a User model, every users will have your name ;-)

    public function beforeSave()
    {
         $this->name = "lucas";
     }

I don't think you can do this in the boot function

Last updated

lucas.sanner54070
lucas.sanner54070

Thanks, but you can't use it that way in the boot() function.
Moreover I cannot figure out what model I should invoke. I tried with Backend

 \Backend::extend(function ($model) {
     ...
  });

but I get an error.

mjauvin
mjauvin

lucas.sanner54070 said:

Thanks, but you can't use it that way in the boot() function.
Moreover I cannot figure out what model I should invoke. I tried with Backend

\Backend::extend(function ($model) {
    ...
 });

but I get an error.

You need to extend YOUR model

lucas.sanner54070
lucas.sanner54070

Sorry I'm a bit confused, but may be I came out wrong.
This is what I want to do, (hope it will be clearer).

On the October backend where the pages are created (backend/cms), when one of my component is used I'd like to check the filename field and throw a warning message if the filename doesn't match some criteria.
How my model will know when a page is saved/created ?

lucas.sanner54070
lucas.sanner54070

I don't know if it's the proper way to do it but here's what I found:

    \Cms\Controllers\Index::extend(function ($controller) {
        $controller->bindEvent('template.processSettingsBeforeSave', function ($dataHolder) {
            $data = post();  
            if ($data['templateType'] == 'page' &&
                in_array('myComponentName', $data['component_names']) &&
                !preg_match('#^myRegex$#', $data['fileName'])) {
                throw new \ApplicationException('Invalid page file name.');
            }   
        });
    });

Hope it helps.

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.