Updated value is not coming from `EditForm`

Hi @johnny

I am trying to show limited values in dropdown on Edit based on certain condition. It is not working as per expectation. Can you please review following code.
It just displays blank drop down.

$cardsKeyValArray = array();
      if ($crud->getState() === 'EditForm') {
        $stateInfo = $crud->getStateInfo();
        $detailsId = $stateInfo->primaryKeyValue;   
        $entryDetails = $this->model->getCurrentEntry($detailsId);
        $cardModel = new \App\Models\CardModel;
        $cardsData = $cardModel->fetchCardsByDepartment($entryDetails[0]['depart_id']);
        foreach($cardsData as $card) {
          $cardsKeyValArray[$card['card_id']] = $card['card_short_name'];
        }        
      }
      
      $crud->fieldTypeEditForm('card_id', 'dropdown', $cardsKeyValArray); 

Hello @kapilchugh ,

You should use callbackEditField instead in your case. For example something like this:

$crud->callbackEditField('card_id', function ($value = '', $primaryKeyValue = null) {
    $cardsKeyValArray = [];
    $detailsId = $primaryKeyValue;   
    $entryDetails = $this->model->getCurrentEntry($detailsId);
    $cardModel = new \App\Models\CardModel;
    $cardsData = $cardModel->fetchCardsByDepartment($entryDetails[0]['depart_id']);
    foreach($cardsData as $card) {
        $cardsKeyValArray[$card['card_id']] = $card['card_short_name'];
    }

    $selectHtml = '<select class="form-control form-select" name="card_id" id="gc-card_id">';
    $selectHtml .= '<option value=""></option>';
    foreach ($cardsKeyValArray as $id => $name) {
        $selected = ($id == $value) ? 'selected' : ''; // Check if $value is selected
        $selectHtml .= '<option value="' . $id . '" ' . $selected . '>' . $name . '</option>';
    }
    $selectHtml .= '</select>';

    return $selectHtml;
});

Let me know if that worked for you.

Yes, this works!
Thanks for your answer!

1 Like