Can the dependent api be used to update inputs other than selects?

Can the dependent api be used to update inputs other than selects?

jhenryjhenry Posts: 11Questions: 1Answers: 0
edited November 2022 in Editor

I have this scenario:

permit table (pa_perm):
id, contact, title, phone, email

contact table (pa_contacts):
id, contact, title, phone, email

I want to open the editor to show the permit info. Some of the fields present on the form will be a select input to choose the contact as well as text inputs for title, phone and email. When a contact is chosen in the select input, I want the title, phone and email inputs to get updated with the chosen contacts info.

Based on what Ive seen, I'm thinking the dependent api is the right tool here, but all of the examples I am seeing involve a select input updating another select input.

editor.dependent( 'continent', '/api/countries' );
include_once( $_SERVER['DOCUMENT_ROOT']."/php/DataTables.php" );
 
$countries = $db
    ->select( 'country', ['id as value', 'name as label'], ['continent' => $_REQUEST['values']['continent']] )
    ->fetchAll();
 
echo json_encode( [
    'options' => [
        'country' => $countries
    ]
] );

I'm currently trying the following:

editor.dependent('pa_perm.Contact', './api/paContactInfo.php');
require('../../../includes/inc_session.php');

// DataTables Editor PHP library
include( '../DataTables_New/Editor-2.0.5/lib/DataTables.php' );

$ContactInfo = $db
    ->select( 'pa_contacts', ['Title', 'Phone', 'Email'], ['Contact' => $_REQUEST['values']['pa_perm.Contact']])
    ->fetch();

echo json_encode( [
  'data' => [
    0 => [
    'contacts' =>  $ContactInfo
    ]
  ]
] );

When using those, the api is firing and returning the following:

{
  "data": [
    {
      "contacts": {
        "Title": "Planner",
        "Phone": "1234567899",
        "Email": "blah.blah@blah.com"
      }
    }
  ]
}

...but the title, phone and email inputs are not being updated. Is it possible to do this? Can I get this json mapped to the right place in the editor after being returned from the api?

Thanks in advance.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,436Questions: 1Answers: 10,049 Site admin
    Answer ✓

    Yes indeed it is. Use values rather than data as part of the response (see the Return options / JSON section in the dependent() documentation).

    echo json_encode( [
      'values' => $ContactInfo
    ] );
    

    This assumes that your Editor fields are called Title, Phone and Email (capitalisation is important). Basically the entry in the values object needs to match the fields.name for the field's value to be updated.

    Allan

  • jhenryjhenry Posts: 11Questions: 1Answers: 0

    OK. I had tried values before, I see the problem now.

    This was part of my server script

     Field::inst( 'pa_perm.Contact' )
              ->options( Options::inst()
                  ->table( 'pa_contacts' )
                  ->value( 'Contact' )
                  ->label( 'Contact' )
                  ->order( 'Contact' )
                  )
                  ->validator( 'Validate::dbValues' ),
    Field::inst( 'contacts.Title' ) ->setFormatter( Format::ifEmpty( null )  ),
    Field::inst( 'contacts.Phone' ) ->setFormatter( Format::ifEmpty( null )  ),
    Field::inst( 'contacts.Email' ) ->setFormatter( Format::ifEmpty( null )  ),
    

    My field names did not match because of the contacts join in the server script. I had to remove the join, thereby getting the field names to be right, from my script to get it working.

    Can I assume now that with the contacts join now gone, the simple act of opening the editor and the select field populating and firing this dependent function will 'act as my join' in the sense that the existing title, phone and email data will be populated on editor open? That is what seems to be happening, I just want to make sure that is expected operation so that I can call that portion of my code good to go.

    Thanks.

  • allanallan Posts: 61,436Questions: 1Answers: 10,049 Site admin
    Answer ✓

    Yes, I would expect that to work.

    That said, it is possible to get linked data to work with the dependent() as well. You would just need to have your JSON return look like this:

    {
      "values": {
          "contacts.Title": "Planner",
          "contacts.Phone": "1234567899",
          "contacts.Email": "blah.blah@blah.com"
       }
    }
    

    Allan

Sign In or Register to comment.