use ->setValue() to store null

use ->setValue() to store null

crush123crush123 Posts: 417Questions: 126Answers: 18
edited March 2015 in Editor

I am using setValue() to store a date if certain criteria are met.

On edit, I want to change the date value back to a null, if the criteria no longer apply

      if ( (Editor::action( $_POST ) === Editor::ACTION_CREATE) || (Editor::action( $_POST ) === Editor::ACTION_EDIT)) {

    $returnedqualityvalue = $_POST['data']['tblitem']['ItemQualityID'];
    if ($returnedqualityvalue == 3) {//if item is now a sale item, set the ItemDateonSale field to today
        $SaleDateField
        ->set( Field::SET_BOTH )
        ->setValue( $today );
     } else {
         $SaleDateField
         ->set( Field::SET_BOTH )
         ->setValue( null );
     }
  }

i can put a date value for the 'else' block, and it works, but how do i use a null ?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Unfortunately a setValue of null means 'don't use the set value'! However, what you might be able to do (I've haven't actually tried it yet I'm afraid) is to use a closure function that will return null:

    ->setValue( function () {
      return null;
    } )
    

    Its a little underhand, but I think that should work...

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    works for me !

This discussion has been closed.