Add Custom Button next to Submit

Okay - let me do better splain'en.

On the form_x.js - I am updating a value if a certain submit button is pressed. The problem is that with this JS code - the elements will be updated even if the form validation fails. Wondering if there is a way to only update these values until after validation is successful?

JavaScript:
requirejs(['fab/fabrik'], function() {
   Fabrik.addEvent('fabrik.form.submit.start', function (form, e, btn) {
      if (btn.name === 'Route To Manager') {
         form.formElements.get('itr_2___routing_flag').update('1');
      }
      else if (btn.name === 'Route for App_Imp') {
          form.formElements.get('itr_2___routing_flag').update('2');
      }
   });
});
 
Well, no, because that's happening in the browser as you hit the Route To Manager button. Validation doesn't happen till after that, on the server side, when the submit request hits the server.

The alternative to doing it in Javsascript would be to do it in a PHP form submission plugin, running onBeforeProcess

Something like ...

Code:
if (array_key_exists('Route To Manager', $this->formData)) {
   $formModel->updateFormData('itr_2___routing_flag', '1', true);
}
else if (array_key_exists('Route for App_Imp', $this->formData)) {
   $formModel->updateFormData('itr_2___routing_flag', '2', true);
}

The onBeforeProcess hook runs during from submission, after validation (so doesn't run if validation fails). And the button name should only exist in the submitted form data if that button was pressed. So you can update the value for itr_2___routing_flag accordingly.

-- hugh
 
ok - unfortunately the if statements don't work...I try it without the if statement and it works fine:

Code:
$formModel->updateFormData('itr_2___routing_flag', '1', true);

Just a refresher - I am adding this buttons in default_actions.php on a custom form template:

Code:
                <button type="submit" class="btn btn-primary button save" name="Route To Manager">Route to Manager</button>
                <button type="submit" class="btn btn-primary button save" name="Route for App_Imp">Route for Approval & Implementation</button>
 
Ok - still no dice. Would you like me to set this scenario up on my test server so that you can test?

PHP:
if (array_key_exists('Route To Manager', $formModel->formData)) {
   $formModel->updateFormData('itr_2___routing_flag', '1', true);
}
else if (array_key_exists('Route for App_Imp', $formModel->formData)) {
   $formModel->updateFormData('itr_2___routing_flag', '2', true);
}
 
Nah, just put..

var_dump($formModel->formData); exit;

... as the first line, and paste the output here.



Sent from my HTC6545LVW using Tapatalk
 
Here it is...not seeing those button names show up though:

Attached:
 

Attachments

  • fabrikar_array.txt
    20.3 KB · Views: 54
Friendly bump again - I am really trying to get this to work so that I can continue working on this project....thanks.
 
Ah, you just need to replace spaces with _. If you look at that text file you attached the with var_dump() in it, there's "Route_To_Manager".

Code:
if(array_key_exists('Route_To_Manager',$formModel->formData)){
   $formModel->updateFormData('itr_2___routing_flag','1',true);
}
elseif(array_key_exists('Route_for_App_Imp',$formModel->formData)){
   $formModel->updateFormData('itr_2___routing_flag','2',true);
}

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

Thank you.

Members online

No members online now.
Back
Top