Thanks for the clarifications. In between, I have added some code to my program. Now when the flow of control is going on the Home Page then the text of label is being updated from "Home Page" to "Welcome Hello". But I also want to display the name, email and password submitted through the registration form on the home page. And I am facing problem in this. When I using the following code:
this.label.text = "Welcome Hello "+this.registeredRegistration.name;
then I am getting following error:
Uncaught TypeError: Cannot read properties of null (reading 'name')
This implies that the value of this.registeredRegistration is null. But why it is null?
Here is my code:
import objects.Registration;
import events.RegistrationEvent;
import feathers.controls.Application;
import feathers.controls.Label;
import feathers.controls.navigators.StackNavigator;
import feathers.controls.LayoutGroup;
import feathers.controls.navigators.StackItem;
import openfl.events.Event;
import feathers.events.TriggerEvent;
import feathers.controls.Button;
import feathers.layout.VerticalLayout;
import feathers.controls.Form;
import feathers.controls.FormItem;
import feathers.controls.TextInput;
import feathers.events.FormEvent;
import feathers.skins.RectangleSkin;
import feathers.core.InvalidationFlag;
using StringTools;
class RegistrationForm extends Application
{
public function new()
{
super();
var navigator = new StackNavigator();
/*
var registrationPage = StackItem.withClass(RegistrationPage.ID, RegistrationPage, [
Event.CHANGE => Push(HomePage.ID)]);
*/
var registrationPage = StackItem.withClass(RegistrationPage.ID, RegistrationPage, [
RegistrationEvent.REGISTER => NewAction((event:RegistrationEvent) -> {
var registration = event.registration;
return Push(HomePage.ID, (target: HomePage) -> {
target.registeredRegistration = registration;
});
})
]);
navigator.addItem(registrationPage);
var homePage = StackItem.withClass(HomePage.ID, HomePage, [
//Event.COMPLETE => Pop()
//Event.COMPLETE => Push(ViewA.ID)
Event.CHANGE => Push(RegistrationPage.ID)]);
navigator.addItem(homePage);
navigator.rootItemID = RegistrationPage.ID;
addChild(navigator);
}
}
class RegistrationPage extends LayoutGroup
{
public static final ID = "registration-page";
public var nameInput:TextInput;
public var emailInput:TextInput;
public var passwordInput:TextInput;
public var registeredRegistration(default, set):Registration = null;
private function set_registeredRegistration(registration:Registration):Registration {
if (this.registeredRegistration == registration) {
return this.registeredRegistration;
}
this.registeredRegistration = registration;
this.setInvalid(InvalidationFlag.DATA);
return this.registeredRegistration;
}
public function new()
{
super();
layout = new VerticalLayout();
var form = new Form();
var nameItem = new FormItem();
nameItem.text = "Name:";
nameInput = new TextInput();
nameInput.prompt = "Your name:";
nameInput.autoSizeWidth = true;
nameItem.content = nameInput;
nameItem.required = true;
form.addChild(nameItem);
nameItem.textPosition = LEFT;
nameItem.paddingTop = 5.0;
nameItem.paddingRight = 8.0;
nameItem.paddingBottom = 5.0;
nameItem.paddingLeft = 8.0;
nameItem.gap = 10.0;
var emailItem = new FormItem();
emailItem.text = "Email:";
emailInput = new TextInput();
emailInput.prompt = "Your email:";
emailInput.autoSizeWidth = true;
emailItem.content = emailInput;
emailItem.required = true;
form.addChild(emailItem);
emailItem.textPosition = LEFT;
emailItem.paddingTop = 5.0;
emailItem.paddingRight = 8.0;
emailItem.paddingBottom = 5.0;
emailItem.paddingLeft = 8.0;
emailItem.gap = 10.0;
var passwordItem = new FormItem();
passwordItem.text = "Password:";
passwordInput = new TextInput();
passwordInput.prompt = "Your password:";
passwordInput.autoSizeWidth = true;
passwordInput.displayAsPassword = true;
passwordItem.content = passwordInput;
passwordItem.required = true;
form.addChild(passwordItem);
passwordItem.textPosition = LEFT;
passwordItem.paddingTop = 5.0;
passwordItem.paddingRight = 8.0;
passwordItem.paddingBottom = 5.0;
passwordItem.paddingLeft = 8.0;
passwordItem.gap = 10.0;
var sendButton = new Button();
sendButton.text = "Register";
form.addChild(sendButton);
form.submitButton = sendButton;
var skin = new RectangleSkin();
skin.border = SolidColor(1.0, 0x999999);
skin.fill = SolidColor(0xcccccc);
skin.width = 16.0;
skin.height = 16.0;
form.backgroundSkin = skin;
form.addEventListener(FormEvent.SUBMIT, form_submitHandler);
addChild(form);
}
private function form_submitHandler(event:FormEvent):Void
{
if(nameInput.text.trim()=="" || emailInput.text.trim()=="" || passwordInput.text.trim()=="")
{
if(nameInput.text.trim()=="") nameInput.errorString="Empty";
if(emailInput.text.trim()=="") emailInput.errorString="Empty";
if(passwordInput.text.trim()=="") passwordInput.errorString="Empty";
}
else dispatchEvent(new RegistrationEvent(RegistrationEvent.REGISTER, this.registeredRegistration));
}
}
class HomePage extends LayoutGroup
{
public static final ID = "home-page";
public var registeredRegistration(default, set):Registration = null;
private function set_registeredRegistration(registration:Registration):Registration {
if (this.registeredRegistration == registration) {
return this.registeredRegistration;
}
this.registeredRegistration = registration;
this.setInvalid(InvalidationFlag.DATA);
return this.registeredRegistration;
}
private var label:Label;
public function new()
{
super();
}
override private function initialize():Void
{
super.initialize();
layout = new VerticalLayout();
this.label = new Label();
this.label.text = "Home Page";
this.addChild(this.label);
var button = new Button();
button.text = "Go To Registration Page";
button.addEventListener(TriggerEvent.TRIGGER, button_triggerHandler);
this.addChild(button);
}
override private function update():Void {
var dataInvalid = this.isInvalid(InvalidationFlag.DATA);
trace(dataInvalid);
if (dataInvalid) {
this.label.text = "Welcome Hello "+this.registeredRegistration.name;
//this.label.text = "Welcome Hello ";
}
super.update();
}
private function button_triggerHandler(event:TriggerEvent):Void
{
//dispatchEvent(new Event(Event.COMPLETE));
dispatchEvent(new Event(Event.CHANGE));
}
}