Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » Showing errors on a form or in a .ss view

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

Page: 1
Go to End
Author Topic: 1378 Views
  • defunct
    Avatar
    Community Member
    3 Posts

    Showing errors on a form or in a .ss view Link to this post

    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

  • defunct
    Avatar
    Community Member
    3 Posts

    Re: Showing errors on a form or in a .ss view Link to this post

    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

    1378 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.