Welcome Guest, Not a member yet? Register   Sign In
CI4: create single view (modal / non-modal) for both Add/Edit
#1

(This post was last modified: 04-07-2022, 10:35 PM by Ahmed Haroon.)

as seniors know about i have just started learning using CI4 as recommended by @InsiteFX, so may be the question is worthless. anyway, i am trying following different guides, today i started seeing this Guide , here he created a modal form with method="post" at around 01:50 , so question arise that is there a way i can create a Single View for both methods Add & Edit "post" & "put" ? if so, please have a link to the guide from where i can create a single modal form for both operations.
regards.

edit: because appearance and functionalities are same in both imo. main difference is in Add form should be blank and Insert will call, in Edit form should contain existing data for selected row/record and Update will call. if there are calculations involved, they will work same in both cases etc.
Reply
#2

This should point you in the right direction, if not let me know.

Makitweb - CRUD (Create Read Update Delete) in a CodeIgniter 4

He's using one view but you will need to look at the code to see what he is doing.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(04-08-2022, 12:59 AM)InsiteFX Wrote: This should point you in the right direction, if not let me know.

Makitweb - CRUD (Create Read Update Delete) in a CodeIgniter 4

He's using one view but you will need to look at the code to see what he is doing.

thanks @InsiteFX for quick response and link to the guide. will check it and give feedback here.
regards
Reply
#4

(This post was last modified: 04-08-2022, 05:04 AM by Ahmed Haroon.)

hi @InsiteFX
to check how it works, first i just created files and copy / pasted given code as mentioned, after completion, started spark server and in browser enter http://localhost:8080/SubjectsController and it runs fine, added 2 records, but when pressing List button, it redirects me to default CI 4 home page (welcome message) , what i have to modify to overcome this my side issue. i didn't yet touched anything else but only mentioned in that guide.
i have project folder => d:\xampp\htdocs\testci4\
regards

EDIT: problem resolved, added /SubjectsController/index in site_url('/SubjectsController/index') and now List button works fine ( added in both files  create.php and edit.php )  site_url('/SubjectsController/index')
Note: there are 3 view files and separate for Add (create.php) and Edit (edit.php) and a separate file named layout.php which inherit in all 3 view files.
site_url('/SubjectsController/index')
 SubjectsController/index[size=1]SubjectsController/[/size]
Reply
#5

You really should create routes for the urls.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 04-10-2022, 10:56 PM by Ahmed Haroon.)

(04-09-2022, 12:45 AM)InsiteFX Wrote: You really should create routes for the urls.
@InsiteFX
as mentioned in Edit portion, the problem is resolved.
also mentioned in Edit that he is using 2 views.
please inform about he is using 2 views (not one) create.php and edit.php and also a layout.php , in both views he used Post method, this is not the thing i have asked for ( we are creating a single Form in Oracle Forms and Oracle Apex for both operations Add & Edit  or like a single method/function to perform multiple tasks as per parameters passed). is it possible in CI4? if yes, how?
regards
Reply
#7

(This post was last modified: 04-10-2022, 11:22 PM by ignitedcms.)

At the very least you will need a create and edit view.

Typically.

Create:
-Open form, validate input data, do a round trip on server validate and post back errors / repopulate form (if you use ajax you avoid issues with repopulating form - i.e no page refresh useful with modals)

Edit:
-Do a lookup from db, gather data, populate existing form input data, on validate post back errors / repopulate form ( use ajax)

I use a combination of this with vue for my management system to dynamically build and store the data and repopulate, which makes it super convenient.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#8

(This post was last modified: 04-10-2022, 11:55 PM by Ahmed Haroon.)

@ignitedcms
you said :
Quote:I use a combination of this with vue for my management system to dynamically build and store the data and repopulate, which makes it super convenient.

yes, experienced people like you and others here can achieve this, i asked to have knowldge for a work around to have an easy, reusable code like a Single function/method which can perform multiple tasks just depending on parameters passed. almost all Add forms contains same fields/columns in Edit forms and the calculations / validations are too same most of the time as per data entered. that's why i asked it here. may be after some time, i can have this feature from CI developers in CI itself.
regards
Reply
#9

(This post was last modified: 04-11-2022, 12:03 AM by ignitedcms.)

I would say it is possible to have one view for create and edit, but you would just be passing a series of if/ else conditions to achieve the same thing if you were creating two controllers / two views.

Personally, I would prefer these as being separated for ease of maintenance. I've devised a way to store form data structure and it's hierarchy with simply JSON and vue, IMO this is the best way to process forms but there is nothing to stop you from doing it manually.

Also, to add to this make sure you're generating a CSRF token for all form input requests.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#10

(04-11-2022, 12:01 AM)ignitedcms Wrote: I would say it is possible to have one view for create and edit, but you would just be passing a series of if/ else conditions to achieve the same thing if you were creating two controllers / two views.

Personally, I would prefer these as being separated for ease of maintenance. I've devised a way to store form data structure and it's hierarchy with simply JSON and vue, IMO this is the best way to process forms but there is nothing to stop you from doing it manually.

Also, to add to this make sure you're generating a CSRF token for all form input requests.
thanks for your passions to help @ignitedcms 
ok, this approach will be more beneficial in CI4 if i have 2 separate views, first create for Add, test the functionalities it required, when things confirm ( comfort level ) then copy this page for Edit and make necessary changes to fetch data from db to update.
if anything else necessary, please guide.
regards
Reply




Theme © iAndrew 2016 - Forum software by © MyBB