1

I have a CRUD application and everything works well except editing an asset as shown here.

When I click on edit, all the fields are populated with the data about the asset except for two fields, Date Of Purchase and Date Of Entry as shown here enter image description here. The two fields are using date pickers.

Here is were it gets interesting, when I echo the array <?= $asset ?> in the edit.ctp as passed from the edit() method in the AssetsController.php, this is what the echo shows;

{ "id": 4, "school_unit_id": 35, "asset_source_id": 19, "asset_description": "iPhone 12", "date_of_entry": "2021-10-11T00:00:00+00:00", "date_of_purchase": "2021-10-08T00:00:00+00:00", "grn_number": "KBHBBH62", "name_of_supplier": "SEAGHON TECH", "serial_number": "YTDIYTFYUFOGOOAA", "location": "CICT", "asset_category_id": 22, "asset_group_class_id": 49, "full_asset_number": "GGFUYG66", "condition_id": 12, "asset_status_id": 13, "value": "20,000", "custodian_name": "JOE BANDA", "custodian_phone": "0966010101", "custodian_email": "bursar@unza.zm", "created": "2021-05-31T08:45:17+00:00", "modified": "2021-10-12T10:45:11+00:00", "created_by": "admin", "asset_status": { "id": 13, "name": "OPERATIONAL", "created_by": "admin" }, "asset_group": { "id": 49, "asset_group": "Mobile Phone / Office Extension / Land Phone", "category_id": 22, "created": "2021-05-06T16:48:40+00:00", "modified": "2021-05-06T16:48:40+00:00", "created_by": "admin" }, "asset_source": { "id": 19, "name": "Procurement", "created_by": "" }, "asset_category": { "id": 22, "category": "EQUIPMENT", "created": "2021-04-28T08:27:51+00:00", "modified": "2021-10-06T09:48:30+00:00", "created_by": "admin" }, "asset_condition": { "id": 12, "condition_name": "New", "created": "2021-03-31T10:40:43+00:00", "modified": "2021-03-31T10:40:43+00:00", "created_by": "admin" }, "school_unit": { "id": 35, "school_unit_name": "CENTER FOR INFORMATION AND COMMUNICATION TECHNOLOGIES", "created": "2021-03-31T10:18:42+00:00", "modified": "2021-03-31T10:18:42+00:00", "created_by": "admin" } }

Clearly, the date_of_purchase and the date_of_entry date values are displaying. With that, this is how am echoing these dates on the fields in the form.

<div class="col-sm-4">
   <?= $this->Form->input('date_of_purchase', array('type' => 'date', 'required' => 'required', 'class' => 'form-control date', 'value' => ($asset->date_of_purchase))) ?>
</div>

And

<div class="col-sm-4">
  <?= $this->Form->input('date_of_entry', array('type' => 'date', 'required' => 'required', 'class' => 'form-control date', 'value' => ($asset->date_of_entry))) ?>
</div>

But the date values are not displaying in the edit form. I have tried storing the date in another variable, then strtotime() it, still nothing. I don't know were am missing it.

Kovo_
  • 25
  • 7
  • If your form is correctly initialized, you shouldn't ever need to pass the `'value'` key to the `input` function, it'll figure that out for itself. – Greg Schmidt Oct 15 '21 at 12:50
  • By initializing the form, you mean this? `
    = $this->Form->create(null, ['name' => 'asset', 'url' => ['controller' => 'Assets', 'action' => 'editasset']]) ?> = $this->Form->unlockField('date_of_entry') ?> = $this->Form->unlockField('date_of_purchase') ?>
    = $this->Form->button(__('Edit Asset'), ['class' => 'btn btn-primary', 'escape' => false]) ?> = $this->Form->end() ?>
    `
    – Kovo_ Oct 15 '21 at 13:15
  • `$this->Form->create($asset, ....)` – Greg Schmidt Oct 15 '21 at 13:19
  • I made the change to this `$this->Form->create($asset, ....)`, I removed the `'value' key` on the date fields. Added back the `'value' key` while maintaining the `$this->Form->create($asset, ....)` but still no change – Kovo_ Oct 15 '21 at 13:46
  • What does the HTML generated by the `input` call look like? – Greg Schmidt Oct 15 '21 at 14:25
  • If am getting you correctly, you mean on the GUI side? If that's the case, I included a screen shot of the GUI look in my original question. It appears I can't add it from here – Kovo_ Oct 15 '21 at 14:39
  • No, I mean exactly what HTML is generated. `` – Greg Schmidt Oct 15 '21 at 17:04
  • sorry for the late response, I had a hectic weekend. Here is the HTML upon inspecting the code in the browser. `` and my code in the `edit.ctp` is `
    = $this->Form->input('date_of_purchase', array('type' => 'date', 'required' => 'required', 'class' => 'form-control date')) ?>
    `
    – Kovo_ Oct 19 '21 at 07:01
  • The value in there is in m/d/y format, but the spec says it's required to be in yyyy-mm-dd format. Do you have anything that would be changing the format of the date from the default? An accessor function masking the real property value, for example? I wouldn't think that changing the default "to string" format should affect this, but if you've got something like that maybe try without it to test? – Greg Schmidt Oct 19 '21 at 14:44
  • You are right. I just played around with the date format in `/vendor/cakephp/cakephp/src/Database/Type/DateType.php` and getting clues from this post [link](https://stackoverflow.com/questions/42438180/converting-array-to-string-for-date-format-in-cakephp-3-x). Its working now, a million thanks to you @GregSchmidt – Kovo_ Oct 20 '21 at 07:26
  • Sounds like it could be an error in Cake, as it should always format the data for the input according to the requirements of the input, regardless of the display format. Maybe open an issue on Github? – Greg Schmidt Oct 20 '21 at 12:25

0 Answers0