I try to modify the “decrease an available count in another form” for use in a class enrollment site as in example
http://formidablepro.com/knowledgebase/action-hooks/frm_after_create_entry/
add_filter(‘frm_after_create_entry’, ‘after_entry_created’, 30, 2);
function after_entry_created($entry_id, $form_id){
if($form_id == 68){ //change 5 to the ID of your event form
global $wpdb, $frmdb;
$event_id = $_POST['item_meta'][517]; //change 25 to the ID of your drop-down data from entries field
$seat_required = $_POST['item_meta'][518];//change 50 to the ID of seat required
$seat_count_field = 516; //change 15 to the ID of the available seats field in the event form
$available = FrmEntryMeta::get_entry_meta_by_field($event_id, $seat_count_field, true);
if (((int)$available-$seat_required) >=0)
$wpdb->update($frmdb->entry_metas, array(‘meta_value’ => ((int)$available-$seat_required)), array(‘item_id’ => $event_id, ‘field_id’ => $seat_count_field));
else
echo “The class if full !!!!”;
}
}
Above code works correctly when there are more seats available that I request. But when I try to reserve seats (e.g. 3 seats) that is more than available. (e.g. before submission, no. of seats equals 2 seats), the enrollment form can still be accepted and the error message is not shownZ. I would like the enrollment form to be rejected with an error message. Are there other hooks that I can use to check and throw an error message if no. of available seats greater than no. of reserving seats before processing the enrollment form like this case?
Many thanks indeed.




September 27, 2012 at 2:44 pm
This hook is fired after the entry is created. If you'd like to show an error message and prevent the form from being submitted, you'll need to use this hook:
http://formidablepro.com/knowledgebase/frm_validate_field_entry/
September 29, 2012 at 3:21 am
Based on your advice, I now tried to do a field validation before submitting the form to decrement the no. of seats available in the following two functions. I guess this should work but the helper that I think can retrieve value from a field in another form is not working. Can you advise other helper function that can do this?
1. validate that there are more available seats then requested
add_filter('frm_validate_field_entry', 'validation_remaining_seat', 10, 3);
function validation_remaining_seat($errors, $field, $value){
if($field->id == 518){ // ID of the seat_requested that is to be validated
global $wpdb, $frmdb;
$event_id = $_POST['item_meta'][517]; //ID of your drop-down data from entries field, local form
$seat_availabe_currently = 516; //change 516 to the ID of the seat_available_currently field in the event form, global
$available = FrmEntryMeta::get_entry_meta_by_field($event_id, $seat_available_currently, true);// I must have used the wrong helper as this is not returning any value at all at all times
$new_seat_available = ((int)$available-$value);
if ($new_seat_available >= 0){ // required seat is available,Booking is OK, output no error and continues form submission
return;
} else {
$errors['field'.$field->id]= 'event_id'.$event_id.' seatreq'.$value.' newseatavailable'.$new_seat_available.' seatavailablecurrently'.$available;// Booking is NOT OK, keep seat_available_currently unchanged and return an error
}
}
return $errors;
}
2. allow submitting of form and decrement no. of seat available accordingly
add_filter('frm_after_create_entry', 'after_entry_created', 30, 2);
function after_entry_created($entry_id, $form_id){
if($form_id == 68){ //change 5 to the ID of your reserve a seat form
global $wpdb, $frmdb;
$event_id = $_POST['item_meta'][517]; //change 25 to the ID of your drop-down data from entries field
$seat_required = $_POST['item_meta'][518];//change 50 to the ID of seat required
$seat_available_currently = 516; //change 15 to the ID of the seat_available_currently field in the event form
$available = FrmEntryMeta::get_entry_meta_by_field($event_id, $seat_available_currently, true);
$wpdb->update($frmdb->entry_metas, array('meta_value' => ((int)$available-$seat_required)), array('item_id' => $event_id, 'field_id' => $seat_available_currently));
}
}
October 1, 2012 at 11:04 am
You said that line was working in the frm_after_create_entry function, correct? If so, please troubleshoot on your own or with help from a developer. If not, is field 516 set as a post field?
October 3, 2012 at 7:55 am
Stephanie,
What do you meant by "If not, is field 516 set as a post field?"? Is it correct that the code "$seat_available_currently = 516;" will retrieve the value of field with ID 516 which reside on other form (I guess this is possible with the help of "global $wpdb, $frmdb;", or am I wrong?) . If this is not the right code, what is the code which will retrieve the value of field with ID 516 which reside on other form?
October 3, 2012 at 1:40 pm
Is the form with field 516 in it set up to create posts? If so, is that field selected somewhere in the post settings?
Topic closed.