How to validate after preCreate?

How to validate after preCreate?

Hi,

At creation I want my users to pick a word that will be part of a url. When they have choosen a word I add the rest ("https://myexample.com/") of the url by using a server side preCreate function. After that I would like to validate the url. But my code right now only validates the single word written by the user, and not the whole url created with my function. Any suggestions how to validate after, not before I set my new value?

    ->on( 'preCreate', function ( $editor, $values ) {           
  $editor
            ->field( 'link' )
            ->setValue("https://myexample.com/".$word)
            ->validator( 'Validate::url', array(
              'message' => 'Not a url'
              ));
 
  
            } ) 

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Right - the validator runs on the raw data submitted by the end user (before the formatter is applied, which is what setValue ends up being here).

    The result is that in this case you'd need to effectively format it twice. Perhaps moving it into its own function would help keep the code DRY.

    Allan

This discussion has been closed.