setformatter: return false

setformatter: return false

rf1234rf1234 Posts: 3,028Questions: 88Answers: 422

Depending on a condition I want to avoid the update of a field. I.e. I want to do "->set( false )" but based on a condition ... Can I do this with a setFormatter?

Field::inst( 'name' )
                    ->setFormatter( function($val, $data, $opts) {
                        if ($val = 'whatever') {
                             return false;
                        } else {
                             return $val;
                        }
                    }),

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,871Questions: 1Answers: 10,522 Site admin
    Answer ✓

    No - you can't to that with a set formatter I'm afraid. However, you can do it with an event :smile:.

    Allan

  • rf1234rf1234 Posts: 3,028Questions: 88Answers: 422

    ok, then I will use preCreate and preEdit. Thanks, Allan.
    This should work:

    ->on('preCreate', function ( $editor, $values ) {
       if ($values['name'] === 'whatever') {
           $editor            
               ->field('name')
               ->set(false);
        }
    })
    
This discussion has been closed.