Skip to content

Instantly share code, notes, and snippets.

/page.php Secret

Created October 14, 2013 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b8b7cfc014ec335d6fd3 to your computer and use it in GitHub Desktop.
Save anonymous/b8b7cfc014ec335d6fd3 to your computer and use it in GitHub Desktop.
So I have a purchase flow including several forms. Each form is sent via Ajax to be validated, and if it is, the next form is returned and client-side code displays it. I have simplified the code (removed fields and some unrelevant control code) to the snippet below. My problem is: when Form1 validates through Ajax, Form2's template is returned …
public function form1() {
// render template including Form1 form
}
public function Form1Form() {
$f = BootstrapForm::create($this, "Form1",
FieldList::create()
->text('Name', _t('I18n.NAME', 'Name'))
->text('Address', _t('I18n.ADDRESS','Address'))
->text('PostalCode', _t('I18n.POSTALCODE','Postal code'))
->text('City', _t('I18n.CITY','City'))
, FieldList::create(
FormAction::create("confirmForm1", _t('I18n.YES','Confirm'))
, RequiredFields::create("Name", "Address", "City", "PostalCode"));
$data = Session::get("FormData.ConfirmForm");
if(!$data) {
$data = Member::currentUser();
}
$data->ID = $this->getOfferFromRequest()->ID;
$f->loadDataFrom($data);
return $f;
}
public function confirmForm1($data, $form)
{
Session::set("FormData.ConfirmForm", $data);
// use form data
// redirect to next form
return $this->redirect($this->Link("form2"));
}
public function form2(SS_HTTPRequest $r) {
// This function renders the second form, Form2, that shows the issue
$order = $this->getOrder();
i18n::set_locale($order->Language);
$data = array(
'Order' => $order,
'Offer' => $order->Offer()
);
if(Director::is_ajax())
{
// custom header added for initialization purposes on the javascript side
$this->getResponse()->addHeader('X-UpgradeOffer-Step', 'dealers');
// Render the template for Form2 without any layout in case of Ajax request
return $this->customise($data)->renderWith('PurchasePage_form2');
}
else
{
// Let the framework render everything
return $data;
}
}
public function Form2Form() {
$fields = FieldList::create();
$fields->push(HeaderField::create(_t('I18n.STEP2', 'Order step 2')));
$fields
// FromDealer option with default to "N"
->optionset('FromDealer', '', array('Y' => _t('I18n.BUY_FROM_DEALER', 'I want to order from a dealer'), 'N' => 'No'), 'N')
// BuyDirectly option with default to "N"
->optionset('BuyDirectly', '', array('Y' => _t('I18n.IWANTTOBUYDIRECTLY','I want to order online'), 'N' => 'No'), 'N')
// Agree option with default to "N"
->optionset('Agree',_t('I18n.DO_YOU_AGREE','I agree with sales terms'),
array(
'Y' => _t('I18n.YES','Yes'),
'N' => _t('I18n.NO','No')
),
'N'
);
$submit = FormAction::create("confirmForm2", _t('I18n.NEXT','Next'));
$submit->setDisabled(true);
$f = BootstrapForm::create($this, "Form2Form", $fields, FieldList::create($submit));
// In both cases (ajax/direct load), $data is null when rendering the form for the first time
if($data = Session::get("FormData.Form2Form")) {
$f->loadDataFrom($data);
}
// When DirectAllowed is false, the FromDealer optionsets always displays correctly, when
// the BuyDirectly one stays incorrect (both radios checked when Ajax request)
if(!$order->DirectAllowed)
{
$f->Fields()->dataFieldByName('FromDealer')->setValue('Y');
$f->Fields()->dataFieldByName('BuyDirectly')->addExtraClass('hide');
}
return $f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment