1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace WizardSampleSite
.Controllers
18 using System
.Collections
;
20 using Castle
.MonoRail
.Framework
;
22 using WizardSampleSite
.Model
;
25 [DynamicActionProvider( typeof(WizardActionProvider
) )]
26 public class SimpleWizardController
: Controller
, IWizardController
28 public WizardStepPage
[] GetSteps(IRailsEngineContext context
)
30 return new WizardStepPage
[]
32 new IntroductionStep(),
35 new ConfirmationStep(),
40 public void OnWizardStart()
44 public bool OnBeforeStep(string wizardName
, string stepName
, WizardStepPage step
)
49 public void OnAfterStep(string wizardName
, string stepName
, WizardStepPage step
)
54 // Please note that we put the steps on the same file
58 /// Presents a small introduction
60 class IntroductionStep
: WizardStepPage
64 class MainInfoStep
: WizardStepPage
66 protected override void Reset()
68 Session
.Remove("account");
71 public void Save([DataBind("account")] Account accountFromForm
)
73 Account account
= WizSessionUtil
.GetAccountFromSession(Session
);
75 // Update the account on session with the data from the form
77 account
.Name
= accountFromForm
.Name
;
78 account
.Username
= accountFromForm
.Username
;
79 account
.Email
= accountFromForm
.Email
;
80 account
.Pwd
= accountFromForm
.Pwd
;
81 account
.PwdConfirmation
= accountFromForm
.PwdConfirmation
;
83 // Some naive validation
85 IList errors
= ValidateAccount(account
);
87 if (errors
.Count
!= 0)
91 Flash
["errors"] = errors
;
93 // User can't go to the next step yet
95 RedirectToStep("MainInfoStep");
103 private IList
ValidateAccount(Account account
)
105 IList errors
= new ArrayList();
107 if (account
.Name
== null || account
.Name
.Length
== 0)
109 errors
.Add("Full name field must be filled");
111 if (account
.Username
== null || account
.Username
.Length
== 0)
113 errors
.Add("User name field must be filled");
115 if (account
.Email
== null || account
.Email
.Length
== 0)
117 errors
.Add("E-mail field must be filled");
119 if (account
.Pwd
!= account
.PwdConfirmation
)
121 errors
.Add("Password don't match with confirmation");
128 /// Note that you can override
129 /// this method and render a different
130 /// view (the default behavior is to render
131 /// the step name, i.e. MainInfoStep in this step)
133 protected override void RenderWizardView()
135 PropertyBag
.Add("account", WizSessionUtil
.GetAccountFromSession(Session
));
137 base.RenderWizardView();
141 class SubscribeStep
: WizardStepPage
144 /// You can also use this to give the view some data.
145 /// Here we send the presaved (if any) selections
147 protected override void RenderWizardView()
149 PropertyBag
.Add("source", new String
[] { "Sports", "Science", "Nature", "History" }
);
151 PropertyBag
.Add("account", WizSessionUtil
.GetAccountFromSession(Session
));
153 base.RenderWizardView();
156 public void Save([DataBind("account")] Account accountFromForm
)
158 Account account
= WizSessionUtil
.GetAccountFromSession(Session
);
160 account
.Interests
= accountFromForm
.Interests
;
166 class ConfirmationStep
: WizardStepPage
169 /// This is an example of a pre-condition check.
171 protected override bool IsPreConditionSatisfied(IRailsEngineContext context
)
173 Account account
= WizSessionUtil
.GetAccountFromSession(Session
);
177 Flash
.Add("error", "No account information found. Please enter the data");
178 RedirectToStep("MainInfoStep");
185 protected override void RenderWizardView()
187 PropertyBag
.Add("account", WizSessionUtil
.GetAccountFromSession(Session
));
189 base.RenderWizardView();
193 class ResultStep
: WizardStepPage
195 protected override void RenderWizardView()
197 Account account
= WizSessionUtil
.GetAccountFromSession(Session
);
199 // Here you would take the proper actions like creating
200 // the account in the database.
202 // AccountService.Create(account);
203 // Session.Remove("account");
205 base.RenderWizardView();
211 internal static Account
GetAccountFromSession(IDictionary session
)
213 Account account
= session
["stored.account"] as Account
;
217 account
= new Account();
219 session
["stored.account"] = account
;