I’ve done some preliminary work on this, and it looks fairly promising. I’ve hit two issues so far.
1. Karl mentioned that saving values to $_LW->_POST
should be carried over to the actual save step somewhere after OnBeforeValidate
. This doesn’t seem to be the case.
I’ve been using $_LW->REGISTERED_MESSAGES['failure']
as a kind of output for testing, and through that I was able was able to isolate fields to change, implement my REGEX check, and save back to $_LW->_POST
. Had some trouble with different field types (text, textarea), but it appears to work for either now.
The following is some utlity output in failure
message that shows when attempting to save:
Original POST Data:
"title" => "Test Years: Nick Mischler’14"
"description" => "John Castor’87"
"contact_info" => "Mokona Aigablai’01"
"custom_testing_custom_field" => "“This is a bit redundant in a profiles context, but can it be generalized?” - Mick Nischler’41"
"custom_331" => "Jane Smith’96"
"custom_332" => "Alyssa Luna’24 is a Geology Major working with thesis advisor Jim Rougvie. She is studying Dissolved Organic Matter (DOM) Properties During Wet and Dry Years in Spanish Fork Canyon, Central Utah."
"custom_333" => "Kelsey Engelke’25 is a Geology Major working with thesis advisor Jay Zambito. She is examining the Chronostratigraphy of Late Devonian Sandstones from Southwest Illinois and East-central Missouri."
Edited POST Data:
"title" => "Test Years: Nick Mischler ’14"
"description" => "John Castor ’87"
"contact_info" => "Mokona Aigablai ’01"
"custom_testing_custom_field" => "“This is a bit redundant in a profiles context, but can it be generalized?” - Mick Nischler ’41"
"custom_331" => "Jane Smith ’96"
"custom_332" => "Alyssa Luna ’24 is a Geology Major working with thesis advisor Jim Rougvie. She is studying Dissolved Organic Matter (DOM) Properties During Wet and Dry Years in Spanish Fork Canyon, Central Utah."
"custom_333" => "Kelsey Engelke ’25 is a Geology Major working with thesis advisor Jay Zambito. She is examining the Chronostratigraphy of Late Devonian Sandstones from Southwest Illinois and East-central Missouri."
If I disable all $_LW->REGISTERED_MESSAGES['failure']
statements and try to let LiveWhale save the profile, none of these changes are made or saved. This makes me believe that updating the values of $_LW->_POST
in OnBeforeValidate
isn’t enough to make these edits stick. Grr.
2. When the page reloads after triggering $_LW->REGISTERED_MESSAGES['failure']
, some of the fields in the editor update with the changes made. There is a key exception: custom profiles fields.
This approach has its benefits: it puts the correct format into the fields to be saved normally (avoiding the above no-change), and it notifies (or annoys) the editor of the changes and improves awareness of style rules. Thinking this, I tried to trigger $_LW->REGISTERED_MESSAGES['failure']
for each field, similar to required field checks.
Modified to use class year format (First Last ’00): title
Modified to use class year format (First Last ’00): description
Modified to use class year format (First Last ’00): contact_info
Modified to use class year format (First Last ’00): custom_testing_custom_field
Modified to use class year format (First Last ’00): custom_331
Modified to use class year format (First Last ’00): custom_332
Modified to use class year format (First Last ’00): custom_333
When the page reloads, title
, description
, contact_info
, and custom_testing_custom_field
have all been updated with the correct style rule. However, the custom_xyz
fields are unchanged. Thus, I get this set of failures indefinitely for every subsequent save.
Modified to use class year format (First Last ’00): custom_331
Modified to use class year format (First Last ’00): custom_332
Modified to use class year format (First Last ’00): custom_333
So it appears that trying to save updates for custom profiles fields to $_LW->_POST
doesn’t persist when a page is reloaded for a failure. So close!
Thus, what might be wrong here? LiveWhale? Me?
I’m assuming LiveWhale isn’t handling this in the simple way we were hoping, but I could be missing something. Not sure what either would be; I doubt I should be calling $_LW->save_data
“before validation” or anything like that.
Here’s my current custom module code for review.
public function onBeforeValidate($data_type, $id) {
global $_LW;
// if saving an testing profile from a backend editor
if ($data_type=='profiles' && $_LW->page=='profiles_edit' && $_LW->_GET['tid']==72) { // testing profiles
// REAL: Get default fields, append custom fields for this type/subtype.
$fields = ['firstname', 'middlename', 'lastname', 'title', 'description', 'contact_info'];
foreach ($_LW->_POST as $p_id => $p_value) { // universal approach for both profile and config custom fields (key only alternative?)
if (str_starts_with($p_id, 'custom_') && ($p_id !== 'custom_fields')) {
$fields[] = $p_id;
}
}
// UTILITY: List all POST fields we're checking.
$display = '';
foreach ($fields as $field_name) {
$display .= ' "'.$field_name.'" => "'.$_LW->_POST[$field_name].'"<br/>'; // isset or array_has_key
}
$_LW->REGISTERED_MESSAGES['failure'][]='<strong>Original POST Data</strong>:<br/>'.$display.'<strong>Individual Changes</strong>:';
// REAL: Check each field, try to match REGEX, and if match then replace REGEX.
foreach ($fields as $field_name) {
// check isset or array_has_key to ensure key exists; likely isset to also check if empty
$field_content = html_entity_decode($_LW->_POST[$field_name], ENT_QUOTES, 'UTF-8'); // decode so REGEX works
$regex = '/(\w+ \w+)(?!\s{1}’\d{2})\s*[\'‘’´`]?\s*(\d{2})/u'; // enforce First Last ’00
$substitution = '$1 ’$2';
if ( preg_match($regex, $field_content) ) {
$_LW->_POST[$field_name] = preg_replace($regex, $substitution, $field_content);
$_LW->REGISTERED_MESSAGES['failure'][]='Field modified to use correct class year format (First Last ’00): '.$field_name;
}
}
// UTILITY: List all POST fields we're checking, after edits made.
$display = '';
foreach ($fields as $field_name) {
$display .= ' "'.$field_name.'" => "'.$_LW->_POST[$field_name].'"<br/>'; // isset or array_has_key
}
$_LW->REGISTERED_MESSAGES['failure'][]='<strong>Edited POST Data</strong>:<br/>'.$display;
};
} // end onBeforeValidate
Any input, ideas, and direction welcome.
Thanks,
Nick
Summary of input from Alex during 2025 Conference: $_LW->save_data
is a variable, not a function. Try to use that.