Forums

New/Edit record form

adop 17 Jun, 2012
Hi Bob,
I am trying to understand how a form should be set-up in order to enable both, new record insert and editing of stored records.
I have setup a simple form with 1 dropdown field, 1 text box and 1 hidden field named cf_id. There are also 2 actions: 1 DB Multi Record Loader to feed the dropdown and 1 DB Record Loader for edit/insert.

The DB Multi Record Loader picks up fields id,title from #__content under modelID content
The DB Record Loader:
DB field used to query the table record: cf_id
Request param: token
Table: cs_TestContent

Editing a record works fine but if I do not provide a token=1 param I get an Error 500 message complaining about a missing WHERE clause:

JDatabaseMySQL::query: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 SQL=SELECT * FROM `cs_testContent` AS `CsTestContent` WHERE

but I don't see why there should be a WHERE clause on the table feeding the dropdown field.
Please give me a hint where to look for a solution.

Thanks,
adop
GreyHead 18 Jun, 2012
Hi adop,

What do you want the DB Multi-Record Loader to do if this is a new record and there is no id provided?

Bob

PS It's often simpler to use two separate forms for editing and creation. I usually build the 'create' form first then take a copy and alter that to become the 'edit' form.
adop 18 Jun, 2012
Hi Bob,
your PS makes things clearer. So for each form you would have 2 versions, 1 for insertion of new records and 1 for editing. I was trying to get both cases into 1 form only.
Thats enough,

Thanks Bob
GreyHead 18 Jun, 2012
Hi adop,

Yes that it. I found too often that there were little differences (like your Record Loader) that made it cleaner to have two forms.

Bob
adop 18 Jun, 2012
Hi Bob,
in the meantime I have changed some simple forms and keeping 2 forms, 1 for editing and 1 for new records, is not a big deal.
But I also have some looong forms with many dropdown fields and in this case keeping 2 forms in line by applying changes to both of them could required a great effort. Also because these forms are loaded from custom code that retrieves all the necessary arrays to feed the dropdowns.
Is there a way to use custom code in order to convert a form that already enables me to insert new records to a form that can also manage record editing?
I have seen that what seems to make that difference between the 2 types of forms is the DB Record Loader. So I think I could:
1) test for the existance of a 'token' parameter in URL;
2) if present I create a new DB Record Load action
3) fill the action with correct params
4) run the action.

Does this make some sense or it is too simplistic?

Thanks,
adop
GreyHead 18 Jun, 2012
Hi adop,

I understand, your testing approach should work OK. That's why I asked what you wanted to happen if there was no id found.

Bob
adop 21 Jun, 2012
Hi Bob,
I am back after doing some tests.
To answer your question I would like insert a new record if no id is provided.If token=some number than I would like the form to edit that record.
I have setup a basic address form with 2 dropdowns and some text fields.
In the action tab I have 2 DB Multi Record Loaders linked to the dropdowns with the Dynamic Data option and 1 DB Record Loader with:
[list]DB Field=cf_id
Request Param=token
Load Fields=Yes
Curly Replacer=No
No model ID[/list]
When a url without a 'token' parameter is received I have included a piece of custom code that empties DB field and Request param. This code is run as first action in the Load event :
  $token = JRequest::getVar('token');

  if (!isset($token)) {
    foreach ($form->form_actions_data as $fad) {
      if ($fad->type == 'db_record_loader') {
        $myParam = json_decode($fad->params);
        $myParam->dbfield = '';
        $myParam->request_param = '';
        $fad->params = json_encode($myParam);
      }
    }
  }

What is still left to arrange, but I am not sure how to proceed, is to load the dropdowns with the options.

However I have noticed a couple of issues that relate to edit forms in general and hence I have tested separately an edit form with the same structure as the one before but without the custom code above. My setup is J 2.5.6, CF V4 RC3.3.
I have come across 2 issues that have been reported before in other threads:
1) setting Load fields Should any form fields be loaded with data ? your field name should match the table's column name.] to Yes in DB Record Loader breaks the code with the following message:

Fatal error: Call to a member function get_array_value() on a non-object in ...components\com_chronoforms\libraries\includes\data_republish.php(24) : eval()'d code on line 16

. See attached image.
2) the Multi Record Loader requires a WHERE clause in any case, even though I want to pick upp all the records. So to make it work I add a fictitious clause that is always true like
country_id < 9999

For one of the 2 issues above I remember reading a post from Max saying that there was a fix.
Please advise on any fix that you may have already developed and any idea you may have to get at least the dropdowns to work in an edit form.

Thanks,
adop
This topic is locked and no more replies can be posted.