Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Showing errors on a form or in a .ss view


Go to End


2 Posts   2457 Views

Avatar
defunct

Community Member, 3 Posts

12 March 2009 at 5:20pm

Hey everyone,

I'm new here. I have a unique problem:

1) I have a form that is part of a controller called Players.php (this controller extends just controller, I don't want it within the actual backend CMS)

2) The form posts to a controller called LoginAction() in Players controller.

3) The Players controller then makes a remote curl call to a java servlet which returns XML to me, either containing success or and error.

Option a: I test for the error and I want to either redirect back to the form and force a custom error within the form framework,

Option b: Redirect back to say domain.com/players/loginfailed and then test for that url string and display an error in the Players.ss view.

With good old fashioned PHP I would do something like this domain.com/players/?error=1 and then if(isset($_GET['error']) { $this->showError('Login Failed'); } or something along those lines.

Can anyone please let me know how to do this. This issue is I'm not really interacting directly with the cms to test the login, so I'm finding it very hard to display custom errors.

Thanks for your time

Avatar
defunct

Community Member, 3 Posts

13 March 2009 at 8:04am

Ok thanks to stuckinrealtime for helping me with this:

My form looks like this:


class Players_Controller extends Page_Controller {
 
	/**
	* This function lets you put a form on your page, using $Login
	*/
	function Login() {
		
		// get captcha id
		$c = new PlConnect();
		$result = $c->PlCaptchaId();	
		
		
		// Show form using $Login within an .ss template
		return new Form($this, "Login", new FieldSet(

		// List your fields here
		new TextField("username", "username"),
		new PasswordField("password", "password"),
		new HiddenField("captchaId", "captchaId", $result['captchaId'])

	), new FieldSet(

		// List the action buttons here
		new FormAction("LoginAction", "Log in")

	), new RequiredFields(

		// List the required fields here: "Email", "FirstName"
		'username', 'password'

	));

	/**
	* This function is called when the user submits the form.
	*/
	function LoginAction($data, $form) {
	
		// Log the user into the login servlet
		$c = new PlConnect();
		$result = $c->PlLogin($data);
		
                // this error is given from my servlet
		if(!isset($result['error'])) {
		
		// TODO: Setup session information
		
		
		// Redirect to myaccount
		Director::redirect('players/myaccount/');
		
		} else {
		
			// Login failed, redirect and show an error above the form and redirect back to the controller, login is my form name
			$form->addErrorMessage('Login','Invalid username or password','required');			
			Director::redirect('players/');
			

		}

	}

and my template for now is simple, it loads that within the Page.ss template (I'm not sure why):

Players.ss

$Login