preCreate/preEdit - extract string from field
preCreate/preEdit - extract string from field
I am wanting to use preCreate and preEdit to automatically create an insert into database column based on two fields in the Editor form.
The created insert needs to be in the format week - unit
where unit is eight characters.
This constructed entry is to be inserted into column week.week_full_name
This is constructed from fields:
Field::inst( 'week.week' ),
and
Field::inst( 'unit_group.unit_group' )
The latter field I just want to use the last eight characters of the record (selected by Select list). Not sure how to extract that part of the string from that record - that is I only want the last eight characters of Field::inst( 'unit_group.unit_group' )
The code so far:
->on( 'preCreate', function ( $editor, &$values ) {
$editor
->field( 'week.week_full_name' )
->setValue( $values['week']['week'] . ' - ' . $values['unit_group']['unit_group'] );
} )
->on('preEdit', function ($editor, $id, $values) {
$editor->field('week.week_full_name')->setValue(
$values['week']['week'] . ' ' . $values['unit_group']['unit_group']);
} )
This question has accepted answers - jump to:
Answers
Full code, and I'm getting system error on create:
Notice: Undefined index: unit_group in /var/www/html/curriculum_mapper/programs/cm_mjd_plmed/program_data/week_data.php on line 38 {"data":[{"DT_RowId":"row_125","week":{"week":"testing","week_full_name":"testing - ","modified":"2022-05-05 13:50:27","modified_by":"00082563","unit_group_fk":"10"},"unit_group":{"unit_group":"Learning Events - PATH3309"}}]}
Are you inline editing possibly? It will only submit the changed field value, not the whole row. That can be changed using the
form-options
submit
parameters as shown in this example.If it is not that, I think I'd need a link to the page showing the issue so I can inspect the data being sent to the server.
Allan
Alan
No inline editing. I have another almost identical page that uses preCreate/preEdit and no issues. That code is:
The only difference I can see is the second field to be joined in the preCreate is part of a left join in the problematic code in the first post. In this code that works, the right most join field in the preCreate is further up in the Fields list.
This is the client side code for the problematic server side data code:
Alan, I sent a PM regarding access to a page which shows the issue. Peter
Many thanks for the link by PM - I see the issue and understand what is causing it now.
$values
contains the submitted data, andunit,unit_group
is not being sent to the server. Include it in your form as ahidden
field and that should resolve the issue.Allan
Alan, I don't quite follow , sorry.
In the preCreate/preEdit part, the following combines two fields: week and unit group from their respective tables.
this combined value is for insertion to column
week.week_full_name
.There is not meant to be any table for
unit
in this code.The field
unit_group.unit_group
is referenced further up in the server side script, week_data.php:Field::inst( 'unit_group.unit_group' )
I still don't understand why it would be causing an error in this code:
As I indicated earlier, I just want to grab the last eight characters from ```unit_group''' instead of the whole value of that field. Can you possibly give me a more detailed explanation?
Here is the code from the form/editor client side:
In the error:
Re no index for
unit_group
, the data is actually there inLearning Events - PATH3309
, so I don't know why there is that error.As I said I have another page that uses preEdit/preCreate with no problems. The difference is that page both combined fields are columns of the main table.
With this problematic page with the above error, the column that is problematic is coming from a left join as you can see. Perhaps that is where the issue is?
Add:
To your Javascript Editor fields. That should resolve it.
Allan
Thanks Allan. That resolved the error.
So going back to my original question, how do I extract the last 8 characters from:
$values['unit_group']['unit_group']);
to use only those 8 characters in combined preEdit/preCreate insert?
Tried:
slice()
is JS, as you're in PHP you'll need to use something like substr(),Colin
What was I thinking...of course. I blame the brain fog. Works perfectly with:
Thanks Colin!
Ok there is a problem with using the hidden field:
When creating or updating a record, records in the column in table
unit_group.unit_group
are set to empty WHERE the value equals the value in the Editor form for that field.Alan, you can test this with the previous link sent via PM a few days ago. Create a new record, note the unit group you selected, then refresh the page. You will see all records on that page that should have the same value for the unit group as the one created, have lost the value for Unit Group column.
Add
->set(false)
to the PHP field if you don't want editor to write to it.Allan
Tried that but now the
unit_group
is missing from the fieldweek_full_name
with:Full PHP code:
and the client-side:
Strange, it looks like the
unit_group
part of the combined fields in preCreate/preEdit is missing along with theunit_group
column as per my previous post on Create. If I Update that record theweek_full_name
is complete with theunit_code
. I'm not sure what is happening...Alan, could you check the previous sent link via PM to have a look at this?
The PHP code:
Client side:
Strange, it looks like the
unit_group
part of the combined fields in preCreate/preEdit is missing along with theunit_group
column as per my previous post on Create. That is, ALL **values in tableunit_group.unit_group
are now empty where the value equals the selected unit_group in the Editor on **Create.This is with:
If I Update that record, after manually repopulating that
unit_group
column in the DB table, theweek_full_name
is complete along with theunit_code
. I'm not sure what is happening...Alan, could you check the previous sent link via PM to have a look at this?
The PHP code:
Client side:
Many thanks - I think I understand the issue a little better now.
week.week_full_name
andunit_group.unit_group
are being submitted as empty strings, since their values are not being set when selecting a value from the _Unit Group` dropdown.When a value is selected from that field, you'll want to set the value for the other dependent fields as well. That can be done with
dependent()
.Something like:
In all honesty though, I would recommend against this approach. Consider the case whereby if someone updated a unit group name, that wouldn't then be reflected in this field and couldn't be unless its value to was updated.
What I would strongly recommend in this case is that you store only the raw data components in the database, and then on the client-side use a rendering function to combine the data. That way you side step the issue discussed here and also integrity issue I mentioned above.
Allan
Thanks Allan, the dependant does the trick nicely. I will use that as the unit_groups are created by the application and aren't editable.