Multiple setFormatter

Multiple setFormatter

ECEGROUPEECEGROUPE Posts: 78Questions: 29Answers: 1
edited April 2022 in Editor

Is it possible to use multiple setFormatter for a same field ?

Exemple / more informations :

This alone work :

Field::inst( 'XXXXXX' )
            ->setFormatter( function ( $val, $data ) {
            return strtoupper( $val );
            } )

This alone also work :

Field::inst( 'XXXXXX' )
                ->setFormatter( Format::ifEmpty( null ) ),

but when i try to use both at the same time it doesn't work (only the last one declared work)

Field::inst( 'XXXXXX' )
            ->setFormatter( function ( $val, $data ) {
            return strtoupper( $val );
            } )
            ->setFormatter( Format::ifEmpty( null ) )
            } ),

Thank in advance for your answer :)

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    Answer ✓

    No sorry. It is a single scalar value rather than an array which is iterated over.

    However! As you've seen you can define your own functions, so what to do is just combine the two actions together into a single function:

    function ( $val ) {
      if ( $val === '' ) {
        return null;
      }
      return strtoupper($val);
    }
    

    Allan

  • ECEGROUPEECEGROUPE Posts: 78Questions: 29Answers: 1

    That work perfectly,

    thank you

Sign In or Register to comment.