Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » The default Login Form - add default value's

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2 3
Go to End
Author Topic: 3121 Views
  • borriej
    Avatar
    Community Member
    267 Posts

    The default Login Form - add default value's Link to this post

    Hello,

    in the page.ss I use the default $Form to log-in.

    I want the fields to display default value's.

    For example:value="'username"

    at the moment this is not possible.

    How can I do this?

    Or how can i modify the template/php?

  • UncleCheese
    Avatar
    4085 Posts

    Re: The default Login Form - add default value's Link to this post

    Not sure what you mean?

    Cool avatar!

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: The default Login Form - add default value's Link to this post

    When you have a <input type="text .... you can always (in normal html editing) define a default value, this value will be printed inside the textField. Like:

    <form>
    <input type="text" name="name" value="Enter your name here" />
    </form>

    Silverstripe doesn't give the loginform (to log-in to the cms or customer login) value tags And I do want this.

    The idea is that my layout/design doesn't show the 'username label' above the field, but is says 'username' inside the input textField. When you click the input textField the value should disappear.

    So how can i add these in the code of SilverStripe? At first I added them with javascript, but my other script (which clears the field value when you click in it) won't work then..

  • UncleCheese
    Avatar
    4085 Posts

    Re: The default Login Form - add default value's Link to this post

    The easiest thing to do, and the most common way to solve this problem is use the jQuery inline label plugin. That way you don't create a hassle for non-JS users. You shouldn't have to change the markup of your form just for a behavioral element.

    But if you really want to hard code a value in your form field, just overload the loginform.

    class MyLoginForm extends MemberLoginForm {
    public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) {
    parent::_contstruct($controller, $name, $fields, $actions, $checkCurrentUser);
    $this->Fields()->fieldByName('Email')->setValue('Some default value');
    }
    }

    _config.php

    Object::useCustomClass("MemberLoginForm", "MyLoginForm");

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: The default Login Form - add default value's Link to this post

    Uncle Cheese, thank you for your reply ;)

    I am working with the custom login form for administrators and normal user accounts. (With the GroupDecorator)
    Tried to paste the function inside the customloginform, but that gave website error.

    I really want it hard coded, how to do this?

    php code

    <?php

    class CustomLoginForm extends MemberLoginForm {

       // this function is overloaded on our sublcass (this) to do something different
       public function dologin($data) {
          if($this->performLogin($data)) {
           if(!$this->redirectByGroup($data))
                   Director::redirect(Director::baseURL());
                echo 'done';
          } else {
             if($badLoginURL = Session::get("BadLoginURL")) {
                Director::redirect($badLoginURL);
             } else {
                Director::redirectBack();
             }
          }
       }

       public function redirectByGroup($data) {    

          // gets the current member that is logging in.
          $member = Member::currentUser();
          // gets all the groups.
          $Groups = DataObject::get("Group");
          
          //cycle through each group   
          foreach($Groups as $Group){
             //if the member is in the group and that group has GoToAdmin checked
             if($member->inGroup($Group->ID) && $Group->GoToAdmin == 1)
             {   
                //redirect to the admin page
                 Director::redirect(Director::baseURL() . 'admin' );
                return true;
             }
             //otherwise if the member is in the group and that group has a page link defined
             elseif($member->inGroup($Group->ID) && $Group->LinkPageID != 0)
             {   
                //Get the page that is referenced in the group      
                $Link = DataObject::get_by_id("SiteTree", "{$Group->LinkPageID}")->URLSegment;
                //direct to that page
                Director::redirect(Director::baseURL() . $Link);
                return true;
             }
             
          }
          //otherwise if none of the above worked return fase
          return false;
                
       }
                   
       
    }   

    ?>

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: The default Login Form - add default value's Link to this post

    btw, i tried with jQuery:

    Added this code to the template, which does work on static html pages.
    But it doesn't work on a silverstripe template strange enough

    <script type="text/javascript" src="http://www.url.nl/js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
       $('input[title]').each(function() {
          if($(this).val() === '') {
             $(this).val($(this).attr('title'));   
          }
          
          $(this).focus(function() {
             if($(this).val() == $(this).attr('title')) {
                $(this).val('').addClass('focused');   
             }
          });
          $(this).blur(function() {
             if($(this).val() === '') {
                $(this).val($(this).attr('title')).removeClass('focused');   
             }
          });
       });
    });
    </script>

  • UncleCheese
    Avatar
    4085 Posts

    Re: The default Login Form - add default value's Link to this post

    The code I gave you didn't work? Why did you copy over those two functions? All you should need is a constructor.

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: The default Login Form - add default value's Link to this post

    I was already using CustomLoginForm

    _config:
    Object::useCustomClass('MemberLoginForm', 'CustomLoginForm');
    Object::add_extension('Group', 'GroupDecorator');
    Object::useCustomClass("MemberLoginForm", "MyLoginForm");

    When I add this line in the config, and the function in the page.php i get this error:

    Parse error: syntax error, unexpected T_CLASS, expecting T_FUNCTION in /public/sites/www.url.nl/mysite/code/Page.php on line 37

    So i tought, maybe we hade to add the function in the CustomLoginForm (?)

    3121 Views
Page: 1 2 3
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.