We saw how to create a simple form. However, we didn't have a full interface to administrate the data: verification of the content, display of errors, list of the news and links to modify, delete... Jelix facilitate the development of such features with a powerful form system and a controller to do CRUD (Create/Read/Update/Delete). ===== Creating a form with jForms ===== The first thing to do is to create a form using jForms. It is an XML file in which you declare all controls of the form, and their labels, their constraints etc. It exists a command in jelix-scripts to create quickly a jForms form from a dao file. We will use of course the dao we created in the previous chapter. Go to the jelix-scripts directory and execute this command: php dev.php module:create-form news newsform news The createform command takes three arguments : - the name of the module, - the name of the futur form, - the name of the dao. Then you have a new file @@news.org/modules/news/forms/newsform.form.xml@@ :
The generated form is very basic and most of time you have to modify it because the script cannot guess what you want exactly. For example, here, you have to modify the labels to have better labels. Perhaps you want other controls.. And here, we want also to add some constraints and help.
The format is aaaa-mm-jj
Now the form is ready. We could use the jForms API to manipulate the form, but the controller we're going to use do it for us. ===== Creating a CRUD controller ===== Create the file @@F@modules/news/controllers/admin.classic.php@@, and start to write the controller: Here the controller doesn't inherits from @@C@jController@@, but from @@C@jControllerDaoCrud@@. This is a controller which contains predefined actions to list, create, modify, save and delete records. So you just have to fill some properties to have a full management of the news. A minimal setup is to indicate the dao and the form to use in the controller: Now try to display the main action of the controller: http://localhost:8080/index.php/news/admin/ You should see a list of records, links to edit/remove them or to add new ones. ===== Customizing the CRUD controller ===== If we want to modify some properties of the main HTML response, which is in our case @@C@myHtmlResponse@@, we can modify the method @@M@_getResponse@@ of @@C@jControllerDaoCrud@@. getResponse('html'); $rep->title = "News manager"; return $rep; } } ?> We can also redefine template for each page of the CRUD controller. By default, it uses some templates provided by the jelix module. So let's create a new template to display the list of news. We start by copying the original one, @@F@vendor/jelix/jelix/lib/jelix/core-modules/jelix/templates/crud_list.tpl@@, to @@F@news.org/modules/news/templates/@@. And then we modify it, just the title for the moment:

News management

...
You indicate then this new template to @@C@jControllerDaoCrud@@ in the property @@$listTemplate@@: Now, if you relaunch http://localhost:8080/index.php/news/admin/, you should see your new list of news. You can do the same thing for templates used to display the form etc. See the [[https://docs.jelix.org/en/manual-1.7/modules/controllers/crud|documentation of the CRUD controller]]. === Note === We created the form, the dao, and the controller separately to learn the framework. But we could also create all of them with a single command: php dev.php module:create-dao-crud [] # so here: php dev.php module:create-dao-crud news news admin It creates the controller, the dao and the form file. Then you can see directly the manager: http://localhost:8080/index.php/news/admin/