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