Jump to content

Access repeater field values in form processing


gebeer
 Share

Recommended Posts

Hello,

I'm building a form through the API. In that form I am using a repeater field that is setup with 5 Ready-To-Edit New Repeater items. In the repeater is only one text input field named "servername".

Now I'm having trouble accessing the repeater field values when I process the form.

I read through the docs at https://processwire.com/api/fieldtypes/repeaters/. But when processing form input post values things seem to be different.

I add the field to the form with:

$registrationField = $fields->get("reg_servers")->getInputfield($pages->get("/registration"));
$registrationField->attr("class","form-control");
$registrationForm->append($registrationField);

The field is there in the form and working fine.

Now when processing the form, I need to access the values of my repeater.

Here's the relevant code:

if($input->post->submitregistration) {
    $registrationForm->processInput($input->post);
    $servers = $registrationForm->get("reg_servers")->value;
    var_dump(count($servers)); // this gives int 5, which is fine

    foreach ($servers as $server) {
        echo "<pre>";
        print_r($server->fields->servername); //servername is my text input field in the repeater
        echo "</pre>";
    }
    exit();
}

In the print_r output I can't find the value of my fields.

I also tried var_dump($server->servername) and var_dump($server->servername->value) inside the foreach which gives an empty string.

var_dump($server->fields->servername->value) gives null.

How can I access my field values?

Link to comment
Share on other sites

Read this quickly. Just guessing here; are you by any chance dealing with a multi-dimensional array? If yes, then $input will not help. It is only set to deal with simple arrays. An option would be to use PHP's $_POST instead or there was some other nice code in the forums yesterday but I can't find it now :-) here you go if it is relevant. Anyway, just guessing for now but you will get better answers am sure :-)

Edited by kongondo
Updated post with a link
  • Like 3
Link to comment
Share on other sites

Thanks for that link kongondo. It really helped.

When I do print_r($GLOBALS), I get:

    [_POST] => Array
        (
            [name] => Test
            [uname] => test1
            [regemail] => my@email.net
            [pass] => 
            [_pass] => 
            [sort_repeater1708] => 0
            [publish_repeater1708] => 0
            [_disable_repeater1708] => 1708
            [servername_repeater1708] => 
            [sort_repeater1709] => 1
            [publish_repeater1709] => 0
            [_disable_repeater1709] => 1709
            [servername_repeater1709] => 
            [sort_repeater1710] => 2
            [publish_repeater1710] => 0
            [_disable_repeater1710] => 1710
            [servername_repeater1710] => 
            [sort_repeater1711] => 3
            [publish_repeater1711] => 0
            [_disable_repeater1711] => 1711
            [servername_repeater1711] => 
            [sort_repeater1712] => 4
            [publish_repeater1712] => 0
            [_disable_repeater1712] => 1712
            [servername_repeater1712] => 
            [_reg_servers_add_items] => 0
            [timezone] => Europe/Berlin
            [submitregistration] => Register Account
            [TOKEN609710988X1412858909] => VehDxck9zIEvN191SQ5G.1HKmG0EeQG41
        )

Now I process $_POST with the function from the linked post.

$servers = getMultiDimensional($_POST, "servername_repeater");

And get back my array :)

Array
(
    [1798] => A Server
    [1799] => Another Server
    [1800] => And one more Server
    [1801] => 
    [1802] => 
)

Now I can go on from there and process my values.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...