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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

The default Login Form - add default value's


Go to End
Reply


19 Posts   3514 Views

Avatar
borriej

Community Member, 267 Posts

6 August 2010 at 8:03am

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?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

6 August 2010 at 8:15am

Not sure what you mean?

Cool avatar!

Avatar
borriej

Community Member, 267 Posts

6 August 2010 at 7:50pm

Edited: 06/08/2010 7:50pm

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..

Avatar
UncleCheese

Forum Moderator, 4096 Posts

7 August 2010 at 1:52am

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");

Avatar
borriej

Community Member, 267 Posts

9 August 2010 at 10:37pm

Edited: 09/08/2010 10:37pm

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;
            
   }
               
   
}   

?>

Avatar
borriej

Community Member, 267 Posts

9 August 2010 at 11:08pm

Edited: 10/08/2010 7:50am

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>

Avatar
UncleCheese

Forum Moderator, 4096 Posts

10 August 2010 at 1:35am

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

Avatar
borriej

Community Member, 267 Posts

10 August 2010 at 2:42am

Edited: 11/08/2010 9:11am

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 (?)

Go to Top