Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

[SOLVED] SS3 FORMS: Pass variable to form


Go to End


3 Posts   2403 Views

Avatar
Optic Blaze

Community Member, 190 Posts

25 December 2013 at 12:48pm

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/");
}

}

Avatar
martimiz

Forum Moderator, 1391 Posts

31 December 2013 at 4:16am

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

Avatar
Optic Blaze

Community Member, 190 Posts

1 January 2014 at 1:26am

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()
) ;
}