Field.setValue based on the value of other fields.

Field.setValue based on the value of other fields.

clazetteclazette Posts: 26Questions: 7Answers: 2

I want to set the name field's value based on the value of fields last_name and first_name. I have tried the source field's getValue function but I receive a call to undefined function fatal error. The Editor documentation lists getValue as an existing function, and the method exists in Field.php, for a field instance. Clearly, I am doing something incorrectly. What am I missing?

Field::inst( 'name' )
                ->set( Field::SET_BOTH ) // Do this for create and edit.
                ->setValue(Field::inst( 'last_name' ).getValue()), 

Answers

  • clazetteclazette Posts: 26Questions: 7Answers: 2

    Well, this appears to work although, at this point, I'm not exactly sure why.

    ->setFormatter( function($val, $data, $field) {return $data['last_name'] . ', ' . $data['first_name'];}),
    
  • dataBdataB Posts: 23Questions: 5Answers: 1

    Did you ever get an answer to this? It's strange that setFormatter would work and getValue returns undefined function.

  • allanallan Posts: 63,839Questions: 1Answers: 10,518 Site admin

    There are two options to do what clazette wants:

    1. Use the setFormatter to get the values of the other fields as the second post suggests
    2. Use a server-side event to get / set values as required.

    Allan

This discussion has been closed.