1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AntonyThorpe\Knockout\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\TestOnly; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use AntonyThorpe\Knockout\KnockoutForm; |
8
|
|
|
use SilverStripe\View\SSViewer; |
9
|
|
|
use SilverStripe\Control\HTTPRequest; |
10
|
|
|
use SilverStripe\Forms\Form; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use AntonyThorpe\Knockout\KnockoutTextField; |
13
|
|
|
use AntonyThorpe\Knockout\KnockoutDropdownField; |
14
|
|
|
use AntonyThorpe\Knockout\KnockoutNumericField; |
15
|
|
|
use AntonyThorpe\Knockout\KnockoutEmailField; |
16
|
|
|
use AntonyThorpe\Knockout\KnockoutTextareaField; |
17
|
|
|
use AntonyThorpe\Knockout\KnockoutOptionsetField; |
18
|
|
|
use AntonyThorpe\Knockout\KnockoutConfirmedPasswordField; |
19
|
|
|
use AntonyThorpe\Knockout\KnockoutCheckboxField; |
20
|
|
|
use AntonyThorpe\Knockout\KnockoutSwitchField; |
21
|
|
|
use AntonyThorpe\Knockout\KnockoutToggleCompositeButtonField; |
22
|
|
|
use AntonyThorpe\Knockout\KnockoutFormAction; |
23
|
|
|
|
24
|
|
|
class KnockoutFormTestController extends Controller implements TestOnly |
25
|
|
|
{ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
parent::__construct(); |
29
|
|
|
if (Controller::has_curr()) { |
30
|
|
|
$this->setRequest(Controller::curr()->getRequest()); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private static $allowed_actions = array('Form'); |
|
|
|
|
35
|
|
|
|
36
|
|
|
private static $url_handlers = array( |
|
|
|
|
37
|
|
|
'$Action//$ID/$OtherID' => "handleAction", |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
protected $template = 'BlankPage'; |
41
|
|
|
|
42
|
|
|
public function Link($action = null) |
43
|
|
|
{ |
44
|
|
|
return Controller::join_links( |
45
|
|
|
'KnockoutFormTestController', |
46
|
|
|
$this->getRequest()->latestParam('Action'), |
47
|
|
|
$this->getRequest()->latestParam('ID'), |
48
|
|
|
$action |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function Form() |
53
|
|
|
{ |
54
|
|
|
$form = KnockoutForm::create( |
55
|
|
|
$this, |
56
|
|
|
'Form', |
57
|
|
|
FieldList::create( |
58
|
|
|
KnockoutTextField::create('Spaceship', 'Spaceship') |
59
|
|
|
->setObservable('spaceship2') |
60
|
|
|
->setHasFocus(true), |
61
|
|
|
KnockoutTextField::create('FieldWithComma', 'FieldWithComma') |
62
|
|
|
->setObservable('fieldwithcomma') |
63
|
|
|
->setValue("Enterprise's Voyage"), |
64
|
|
|
KnockoutDropdownField::create( |
65
|
|
|
'Menu', |
66
|
|
|
'Space Menu', |
67
|
|
|
array('1'=>'Light Speed Salad','2'=>'Comet Custard') |
68
|
|
|
)->setObservable('menu'), |
69
|
|
|
KnockoutNumericField::create('SeatNumber', 'Seat Number', 4) |
70
|
|
|
->setObservable('seatNumber'), |
71
|
|
|
KnockoutEmailField::create('Email', 'Email') |
72
|
|
|
->setObservable('email') |
73
|
|
|
->setValue('[email protected]'), |
74
|
|
|
KnockoutTextareaField::create('Comments', 'Comments') |
75
|
|
|
->setObservable('comments'), |
76
|
|
|
KnockoutOptionsetField::create( |
77
|
|
|
'Accessories', |
78
|
|
|
'Accessories', |
79
|
|
|
array( |
80
|
|
|
'Flying High DVD' => 'Flying High DVD', |
81
|
|
|
'Zero Gravity Pillow' => 'Zero Gravity Pillow', |
82
|
|
|
'Rocket Replica' => 'Rocket Replica' |
83
|
|
|
), |
84
|
|
|
'Zero Gravity Pillow' |
85
|
|
|
)->setObservable('accessories') |
86
|
|
|
->setOtherBindings("blah: someOtherFunction"), |
87
|
|
|
KnockoutConfirmedPasswordField::create('Password', 'Password'), |
88
|
|
|
KnockoutCheckboxField::create('CheckboxFieldExample', 'Checkbox Field Example') |
89
|
|
|
->setObservable('checkboxField'), |
90
|
|
|
KnockoutSwitchField::create('SwitchFieldExample', 'Switch Field Example') |
91
|
|
|
->setObservable('switchField'), |
92
|
|
|
KnockoutToggleCompositeButtonField::create( |
93
|
|
|
"MyToggleCompositeButtonField", |
94
|
|
|
"This is a knockout composite button field", |
95
|
|
|
[ |
96
|
|
|
KnockoutTextField::create('Test1', 'Test1')->setObservable('test1'), |
97
|
|
|
KnockoutTextField::create('Test2', 'Test2')->setObservable('test2') |
98
|
|
|
] |
99
|
|
|
)->setObservable('compositeButtonField') |
100
|
|
|
// add any new knockout fields here and assert above |
101
|
|
|
), |
102
|
|
|
FieldList::create( |
103
|
|
|
KnockoutFormAction::create('doSubmit', 'Submit') |
104
|
|
|
->setObservable('canSaveInterGalacticAction') |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
$form->setSubmit('addToCart2'); |
108
|
|
|
|
109
|
|
|
return $form; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function doSubmit($data, Form $form, HTTPRequest $request) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$form->sessionMessage('Test save was successful', 'good'); |
115
|
|
|
return $this->redirectBack(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getViewer($action = null) |
119
|
|
|
{ |
120
|
|
|
return new SSViewer('BlankPage'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|