PHP code on form submit

churari

New Member
I am trying to set an "Approved By" and "Approved Date" field on my form when the user changes the status to "Approved".

I am doing this in the form's PHP code on form submission page. I selected "Start of form submission (onBeforeProcess)".

My code is:

$user =& JFactory::getUser();
$username = $user->get('username');
$approved = $formModel->formData['access_mgmt___approval'];
/*if ($approved == 'Approved' && $formModel->formData['access_mgmt___approved_by'] == '') {*/
if ($approved == 'Approved')
$date = date('m/d/Y');
/*$formModel->updateFormData('access_mgmt___approved_by', $username, true);*/
$formModel->updateFormData('access_mgmt___approved_by', $approved, true);
$formModel->updateFormData('access_mgmt___approve_date', $date, true);
}

The code comparison $approved == 'Approved' won't return true, however when I remove that comparison and always execute the code, I set the field to $approved and it does indeed equal "Approved".

Is the $approved value not recognized yet when this code is executed? That is all I can think of, any help is appreciated.
 
You'll probably find that $approved is an array, so do something like ...

PHP:
$approved = is_array($approved) ? $approved[0] : $approved;

... after you get it from the formData array.

Also, be sure that you are testing for the "value" of the checkbox, not the "label".

The other thing you may or may not be taking in to account is that you aren't testing to see if 'approved' has been changed, you are only checking that it is set. So if they edit and save the form again, your code will run again, and set the approved_by / approved_date again.

If that is a concern, eg. if you expect the form to get saved again, you'll need to also check against $formModel->origData[], which when editing a form (as opposed to submitting a new one) will contain the original values of the elements, prior to editing.

If I recall correctly, $formModel->origData[] is an array of objects, so you'd need $formModel->origData[0]->access_mgmt___approval.

I'm pretty sure we will already have loaded origData before your plugin runs, but if not, you can load it with

$origData = $formModel->getOrigData();

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top