ifEmpty() Formatter set to another column's value

ifEmpty() Formatter set to another column's value

scc_gsiscc_gsi Posts: 4Questions: 2Answers: 0

I am looking to set the value to write to the database if an empty string is submitted from the client-side.
If the value for perm_id is empty, I would like to set the value to default to another required field's projname value.
I have tried:

Field::inst( 'perm_id' )
        ->setFormatter( Format::ifEmpty('projname'))

but it does not seem to work. Any ideas? Thanks!

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    edited September 2018

    Hi @mpalmer ,

    Looking at a combination of this thread and this one, this should work:

        Field::inst( 'perm_id' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return $val ? '' : $data['projname'];
            } )
    

    Cheers,

    Colin

This discussion has been closed.