PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Dynamically Populating from Previous Form Page with Editing Issue

  1. theslink2000
    Member

    Hi, I've been trying to get this working correctly for a while now and the question I have is really two fold.

    Firstly here's what I'm trying to achieve:

    Users register using your registration plugin, and part of the form asks for the postcode.
    The second page pulls the postcode from the first page, but before it is populated back into the form it undergoes editing to produce a username that users will have to use to register on the site.

    The logic is as follows:

    Get postcode,
    Make sure it's all upper case and remove spaces,
    Add -001 to the end of the postcode,
    Check to see if this username is already in use,
    If it is increase to 002 and try again,
    If it's not then continue,
    Post unique username back to the form to allow the user to register,

    So the final format should look like this - AB12CD-002

    I'm having problems getting the formatting and check working so if anyone can help me with the over all problem that'd be great (I'll include what I've got so far at the end).

    The main problem that has caused me to post here is that when I try and concatenate the two halves of the username (join the postcode and number) the post back to GForms seems output the second variable to every field that allows numbers! I have no idea why but I think it has something to do with the last line: $field['defaultValue'] = $user_id;

    Any and all help with either the specific issue or the whole problem would be greatly received!

    Here's what I've got so far (the population code comes from other posts on here):

    add_filter("gform_pre_render_1", "populate_previous_page_data");
    function populate_previous_page_data($form){

    $page_number = rgpost("gform_target_page_number_{$form["id"]}"); //Set $page_number to the form id ??
    $page_number = !is_numeric($page_number) ? 1 : $page_number; //Make the form id a usable number ??

    foreach($form['fields'] as &$field) { //Loop through each form field in the variable $field

    if($field['id'] != 9) // If field is not equal to 9 (or target for dynamically populating)

    $field_page = rgar($field, 'pageNumber'); //$field_page equals the form page the currently looked at field is on

    if($page_number > $field_page) //If $page_number is greater than $field_page then
    continue; //Exit loop and restart

    $user_id = rgpost('input_2_5');
    $user_id = preg_replace('/\s+/', '', $user_id);
    $user_id = strtoupper($user_id);

    $count = 1;
    $user_id_no = 001;
    //$user_id_no = printf("%'03", $user_id_no);
    $user_id = $user_id.$user_id_no;
    $field['defaultValue'] = $user_id;

    }

    return $form;
    }

    Posted 12 years ago on Wednesday May 16, 2012 | Permalink
  2. theslink2000
    Member

    Has no one got any ideas?

    Posted 12 years ago on Wednesday May 23, 2012 | Permalink
  3. theslink2000
    Member

    Well, I've had no reply from anyone but I have managed to find a workable solution so I'll post it here in case anyone stumbles across this post and wants the answer.

    Ultimately this post has provided the majority of the solution:

    http://www.gravityhelp.com/forums/topic/dynamically-populate-age

    The reason I had loads of boxes with bits of data is that the -001 section was set and the postcode wasn't populated until after the first page submission. Using $field[default_Value'] as a return was a mistake. Here is how I've modified the code to work:

    add_filter('gform_validation_1', 'find_age');
    function find_age($validation_result) {

    // 2 - Get the form object from the validation result
    $form = $validation_result["form"];

    // 3 - Get the current page being validated
    $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;

    // 4 - Loop through the form fields
    foreach($form['fields'] as &$field){

    // 5 - If the field does not have our designated CSS class, skip it
    if(strpos($field['cssClass'], 'postcode') === false)
    continue;

    // 6 - Get the field's page number
    $field_page = $field['pageNumber'];

    // 7 - Check if the field is hidden by GF conditional logic
    $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());

    // 8 - If the field is not on the current page OR if the field is hidden, skip it
    if($field_page != $current_page || $is_hidden)
    continue;

    // 9 - Get the submitted value from the $_POST
    $field_value = rgpost("input_2_5");
    $field_value = preg_replace('/\s+/', '', $field_value); //Remove spaces and any character that isn't a number or letter
    $field_value = strtoupper($field_value); //Convert to uppercase

    $user_id = build_id($field_value, 1);

    foreach($form['fields'] as &$field2){
    if(strpos($field2['cssClass'], 'userid') === false) //loop through fields again to find the "Age" field
    continue;

    $field2["defaultValue"] = $user_id; //add calculated age as default value to "Age" field
    }}
    $validation_result['form'] = $form; //manipulate $validation_result with modified $form

    // 15 - Return the validation result
    return $validation_result;

    }

    function build_id ($pcode_id, $assigned_id_no) { //Builds complete user id

    do {
    $cmplt_id = $pcode_id . "-" . number_pad($assigned_id_no, 3); //Puts postcode string and calls the number padding function to add the leading 0's
    $assigned_id_no++;
    }
    while(username_exists($cmplt_id));
    return $cmplt_id;

    return $cmplt_id;
    }

    function number_pad($number,$n) { //Takes a string and add's however many 0's (or other character placed there) to the left of the string
    return str_pad((int) $number,$n,"0",STR_PAD_LEFT);
    }

    Posted 12 years ago on Wednesday May 30, 2012 | Permalink