Making "edit" / "create" views looking the same as "view" one?

Is there a fairly easy way of making “create” or “edit” views in a model’s CRUD to look nearly exactly the same as “view” view does?

I want to make a feeling of a kind-of in-place edit. User switches from “view” to “edit” and see nearly exactly the same (banded table), but with edit fields instead of non-editable values.

There are significant differences between these two kinds of views:

  • The “view” view is using yii\widgets\DetailView widget and an underlying table
  • The “edit” or “create” (actually “_form”) view “throws” fields as is

If this is possible then which aproach I should use?

Options considered by me:

  • Force DetailView to display form parts instead of non-editable strings
  • Iterate through model attributes in a loop and generate HTML table of my own
  • Use Bootstrap 5’s Columns styling set to mimic table while actually using simple divs

Any comment or feedback is highly encouraged. Thank you.

Should be possible, yes. There’s no easy way so your plan makes sense.

Thanks samdark and good to know that you are still active from time to time in the ol’ good 2.0 branch.

Is there any easy way to reuse the mechanism that DetailView uses to generate its underlying set of table-related methods? Like some kind of internal renderer, etc.

Ad 1. Consider use exact the same view for the ‘edit’ and ‘view’ scenarios without DetailView at all but then with some extra params causing render form fields in disabled state, without submit button etc. This will effectively works as a form rendered as a read-only view.

Ad 2. Ugly :slight_smile:

Ad 3. Good way but require extra work with some fancy CSS.

3 Likes

No idea :frowning:

Don’t know if you want to use a third party library, but Kartik has a DetailView widget for Yii2 with the possibility to edit.
https://demos.krajee.com/bs4/detail-view

1 Like

Kartik is absolutely fantastic and his DetailView is just smashing my head.

Thank you for point me out to this.