setFormatter not being honored with bubble edit.

setFormatter not being honored with bubble edit.

clazetteclazette Posts: 26Questions: 7Answers: 2

When I submit the full editor form, this setFormatter is honored and the database name field is updated as expected. However, when using bubble editing, the setFormatter is not applied and the name field is not updated.

Am I doing something incorrectly?

Field::inst( 'name' )
                ->set( Field::SET_BOTH ) // Do this for create and edit.
                ->setFormatter( function($val, $data, $field) {return $data['last_name'] . ', ' . $data['first_name'];}), 

Answers

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

    The Editor form will only submit the changed values from a bubble edit by default (formOptions.bubble). So if you aren't explicitly editing the name parameter then the set formatter shouldn't run.

    If you are setting it and it still isn't running, there is something wrong going on. If the name parameter in the data submitted to the server?

    Allan

  • clazetteclazette Posts: 26Questions: 7Answers: 2

    It is not, it's just an additional field in the database that needs to be updated with any changes to first_name/last_name. It is a hidden field on the Editor so there is probably a way for me to update that on change or some other event and then that data would be sent to the server. Does that make sense?

    Thanks,
    Chad

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

    Hi Chad,

    It sounds like you might want to use the Field->setValue() option - possibly with a server-side event.

    Possibly something like:

        ->on( 'preCreate', function ( $editor, $values ) {
            if ( isset( $values['first_name'] ) && isset( $values['last_name'] ) ) {
              $editor
                  ->field( 'name' )
                  ->setValue( $values['first_name'].' '.$values['last_name'] );
            }
        } )
    

    Will need to be modified to suit your needs of course, but that's how it might be used.

    Allan

  • clazetteclazette Posts: 26Questions: 7Answers: 2
    edited February 2016

    Allan,
    Thank you for the recommended approach. I modified my code accordingly and the preCreate event doesn't appear to be firing. The code you have above should work in my case from what I can tell and the server-side event documentation results in the same conclusion...this should work.

    Of course, with my established Editor history of just doing things wrong, that could be the case again here as well.

    Editor::inst( $db, 'operators', 'ID' ) // Having the PK set here ensures that a non-identity PK is captured for associative inserts.
            ->fields(
                Field::inst( 'ID' ) // ID is not automatically set by the database on create
                    ->set( Field::SET_BOTH )
                    ->setFormatter( function($val, $data, $field) {return $data['badge_ID'];}),   
                
                Field::inst( 'name' )
                    ->set( Field::SET_BOTH ), // Do this for create and edit.
                    //->setFormatter( function($val, $data, $field) {return $data['last_name'] . ', ' . $data['first_name'];}),             
                
                Field::inst( 'first_name' )
                    ->validator( 'Validate::notEmpty', array( 'message' => "First name is required.")  ),   
                    
                Field::inst( 'last_name' )
                    ->validator( 'Validate::notEmpty' ), 
                
                Field::inst( 'badge_ID' )
                    ->validator( 'Validate::unique' )
                    ->validator( 'Validate::notEmpty' ),
                
                Field::inst( 'email' )
                    ->validator( 'Validate::email' ),
                
                Field::inst( 'deactive_date' )
                    ->validator( 'Validate::dateFormat', array(
                        "format"  => Format::DATE_ISO_8601,
                        "message" => "Please enter a date in the format yyyy-mm-dd"
                    ) )
                    ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
                    ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
                
                Field::inst( 'domain_user' )
                    ->validator( 'Validate::unique' )
                    ->validator( 'Validate::notEmpty' ),
                
                Field::inst( 'password' )
                    ->validator( 'Validate::notEmpty' )
            )        
            ->join(
                Mjoin::inst( 'roles' )
                    ->link( 'operators.ID', 'operator_roles.operator_ID' )
                    ->link( 'roles.ID', 'operator_roles.role_ID' )
                    ->fields(
                        Field::inst( 'ID' )
                            ->validator( 'Validate::notEmpty' )
                            ->options( 'roles', 'ID', 'name' ),
                        Field::inst( 'name' ) // supports display in DataTable not Editor.
                    )
            )
            ->join(
                Mjoin::inst( 'areas' )
                    ->link( 'operators.ID', 'operator_areas.operator_ID' )                    
                    ->link( 'areas.ID', 'operator_areas.area_ID' )                
                    ->fields(
                        Field::inst( 'ID' )
                            ->validator( 'Validate::notEmpty' )
                            ->options( 'areas', 'ID', 'area_desc', 
                                       function($q){ $q->where('areas.active', true ); // Ensure only active areas are in the editor select.
                            } ),
                        Field::inst( 'area_desc' ) // supports display in DataTable not Editor.                                            
                    )
                    ->where('areas.active', true) // ensures only active areas are displayed in the table.
            )
            ->on( 'preCreate', function ( $editor, $values ) {
                if ( isset( $values['first_name'] ) && isset( $values['last_name'] ) ) {
                    $editor
                        ->field( 'name' )
                        ->setValue( $values['last_name'] . ', ' . $values['first_name'] );
                }
            } )
            ->on( 'preEdit', function ( $editor, $values ) {
                if ( isset( $values['first_name'] ) && isset( $values['last_name'] ) ) {
                    $editor
                        ->field( 'name' )
                        ->setValue( $values['last_name'] . ', ' . $values['first_name'] );
                }
            } )
            ->process( $_POST )
            ->json();
    
  • clazetteclazette Posts: 26Questions: 7Answers: 2

    Obviously, I'll want to encapsulate setting the name field in its own function to eliminate the duplicate code for now I am just trying to get it working.

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

    If you were to add:

    echo "In preCreate";
    

    into the preCreate function (outside the if) does it get shown in the (now invalid) JSON return? As far as I can see the above code looks good (as long as you are using Editor 1.5+).

    Allan

  • clazetteclazette Posts: 26Questions: 7Answers: 2

    As far as I know, I am using 1.5.4. However, I don't know how to verify that. The echo didn't affect anything. The behavior is as if nothing has even changed so it appears that the event is not firing.

This discussion has been closed.