Jump to:

1976 Posts in 1306 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » [SOLVED] SS3 FORMS: Pass variable to form

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 307 Views
  • Optic Blaze
    Avatar
    Community Member
    155 Posts

    [SOLVED] SS3 FORMS: Pass variable to form Link to this post

    Hi there,

    I am building some front end forms on of which needs to load in the current record based on the url id.
    So if the url is: www.example.com/customer/edit/15 i want the form to load in record number 15.
    Been struggling with it for days, i know i am close, i just dont know what i am doing wrong. I get the following error:

    [Warning] Missing argument 1 for CustomerPage_Controller::CustomerEditForm()

    This is what i have so far:

    -------------------------------------------------------------------------------------------
    <?php

    class CustomerPage_Controller extends Page_Controller {
       
    public static $allowed_actions = array (
          'CustomerEditForm',
          'edit',   
        );
       
       private static $url_handlers = array(
    '//$Action//$ID' => 'edit'
    );
       
       
        //CUSTOMER FORM -> EDIT CUSTOMER
    public function CustomerEditForm($request) {
        return BootstrapForm::create(
    $this,
    "CustomerEditForm",
    FieldList::create(
          TextField::create("Name","Name")
    ->addExtraClass("required"),
       TextField::create("Surname","Surname")
    ->addExtraClass('required'),
       NumericField::create("IDNumber","ID Number")
    ->setMaxLength('13'),
       NumericField::create("Tel1","Phone number 1")
    ->addExtraClass("required")
    ->setMaxLength('10')
             ),
             
          FieldList::create(
           FormAction::create("EditCustomer","Edit Customer")
    ->setStyle("primary"),
    FormAction::create("DeleteCustomer","Delete Customer")
    ->setStyle("warning")
             )
    ->setLayout("horizontal")
        ->loadDataFrom(CustomerData::get()->byID($rquest->param('ID')))
        ;
    }

       // EDIT A CUSTOMER
    public static function EditCustomer($data, $form) {
    $submission = CustomerData::get()->byID($data["ID"]);
    $form->saveInto($submission);
    $submission->write();
    Controller::curr()->redirectBack();
    }

    // DELETE A CUSTOMER
    public static function DeleteCustomer($data, $form) {
    CustomerData::get()->byID($data["ID"])->delete();
    Controller::curr()->redirect("home/");
    }
       
    }

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: [SOLVED] SS3 FORMS: Pass variable to form Link to this post

    You should probably first remove the $request param from the CustomerEditForm() function...

  • Optic Blaze
    Avatar
    Community Member
    155 Posts

    Re: [SOLVED] SS3 FORMS: Pass variable to form Link to this post

    Hi there,

    Got it sorted. In the end i found the best way to call the variable was to wrap it in a function.

    So if i wanted to call 'customer/edit/4' which would correspond to 'customer/$Action/$ID' i created like this:
    ----------------------------------------------------------------------------

    // For any other readers the ID in '$this->urlParams['ID']' corresponds to the either the default $ID or if you set a custom
    // name for it with public static $url_handlers. eg if i set url_handlers to '$Action/$CusID/$ClaimID' => 'handleAction' i would
    // call $this->urlParams['CusID']

    // GETS THE CURRENT CUSTOMER ID --- did not need to white list in $allowed_actions
    public function customer() {
    // Notice the isset()....if you dont do this you get an 'undefined variable' error
    if(isset($this->urlParams['ID']))
    {$customer = Customer::get()->byID($this->urlParams['ID']);}
    else
    {$customer = $this->request->postVars();}
        return $customer;
    }

    -------------------------------------------------------------------------------

    that function i then call from my CustomerEditForm without having to pass the $request variable. So like this:

    --------------------------------------------------------------------------------

    .......
    ->loadDataFrom($this->customer()
    ) ;
    }

    307 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.