Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Passing Data to Form

edited May 2012 in Framework
Form Object ( [failed] => Array ( ) [method] => post [rules] => Array ( ) [_rules:Form:private] => transpoManager/add_signature [view] => transpoManager/add_signature [data] => [controller] => Controller Object ( [params] => Array ( ) [internal] => [data] => Array ( [item_id] => 1 ) [cli] => [put_data] => [app] => transpoManager [uri] => transpoManager/add_signature [chunked] => [cache] => ) [verify_referrer] => 1 [verify_csrf] => 1 [csrf_token] => [csrf_field_name] => _token_ [error] => [js_validation] => 1 )

I am passing this into my form, obviously the Controller Object "Data" is populated, how do I access this within the view? Or am I passing data into the wrong place?

Okay I lied...

stdClass Object ( [_form] => transpoManager-add_signature-form [_failed] => Array ( ) [_rules] => transpoManager/add_signature [is_being_rendered] => 1 )

That is what my $data contains in the view... so how do I pass custom values with the standard form handler?


  • Figured it out. (Should have just read the core Form library class to begin with.

    Solution: In controller

    $form->data = array('item_id' => 1);

    In view

    {{ item_id }}
  • If you're using Form's handle() method, then you can set default values for the form in the $form->data property, which accepts an object or array. These will be merged with submitted values via the merge_values() method (if the form is being re-rendered), and available in the view template via {{ foo }} where that resolves to $form->data->foo.

    For example:

    $form = new Form ('post', $this);
    $form->data = array (
        'field1' => 'default',
        'field2' => 'default'
    echo $form->handle (function ($form) {
        info ($_POST);

    And the view:

    <form method="post" id="{{ _form }}">
    <p>Field 1:<br />
    <input type="text" name="field1" value="{{ field1|quotes }}" /></p>
    <p>Field 2:<br />
    <input type="text" name="field2" value="{{ field2|quotes }}" /></p>
    <p><input type="submit" value="{" Submit "}" /></p>

    Let me know if that's what you're looking for.

  • Haha! Yup! :) I must have beat you to it by milliseconds because your post showed up after my submit, but it is ordered before yours.

Sign In or Register to comment.