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.

Archive /

Our old forums are still available as a read-only archive.

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

Registration Form - User already exists Check?


Reply


5 Posts   4372 Views

Avatar
SalvaStripe

Community Member, 89 Posts

30 October 2008 at 2:14am

Hey Boys, Hi Girls,

In Bottom you can see my RegistrationForm.php. Its simple but it works. When I have a group called "UserGruppe", then visitors can register and there were added to the DataBase in "member".

BUT, when there is someone, who registers with an email adress that allread exists in "member" (there is a registered member with this email adress), then the new FirstName und Surname were UPDATED at that member, and there is no new member.

I want to add an "email check", so a email adress can only have one user.
i realy dont know how to do this in SS. i think i have to make a Query, if the email already exists, but dont know how.

HERE THE FILE:

#####################################

<?

class RegistrationForm extends Page {
}

class RegistrationForm_Controller extends Page_Controller {
   function Form() {
      return new Form($this, "Form", new FieldSet(
         // List the your fields here
         new TextField("FirstName", "Vorname"),
         new TextField("Surname", "Nachname"),
         new EmailField("Email", "Email Addresse")
         
      ), new FieldSet(
         // List the action buttons here
         new FormAction("doform", "Registrieren")
         
      ), new RequiredFields(
         // List the required fields here: "Email", "FirstName"
         "Email", "FirstName", "Surname"
      ));
   }
   function doform($data, $form) {
      // Create a new Member object and load the form data into it
      $member = new Member();
      $form->saveInto($member);
      
      // Write it to the database. This needs to happen before we add it to a group
      $member->write();
      
      // Add the member to group. (Check if it exists first)
      if($group = DataObject::get_one('Group', "Code = 'UserGruppe'")) {
         $member->Groups()->add($group);
      }
      
      // Redirect to a page thanking people for registering
      Director::redirect('jobangebote/');
   }
}

?>

##########################################

p.s.
maybe someone can help me to tell me, whats the best way to protect pages, i mean certain pages should be only visible from logged in users and how to make a form für the users to UPDATE THEIR MEMBER DATA???

TY TY TY if there is any help :D

Avatar
Willr

Forum Moderator, 5513 Posts

30 October 2008 at 12:18pm

$member = new Member();
$form->saveInto($member);

Before you 'create' a new member all you need to do is do a simple query on the db to see if it exists.

$email = Convert::raw2sql($data['Email']); // prevent sql injection
$existingUser = DataObject::get_one("Member", "Email = '$email'");
if($existingUser) {
// user already exists. you might want to add an error msg and redirectBack()
return;
}
$member = new Member();
...

Avatar
SalvaStripe

Community Member, 89 Posts

4 November 2008 at 12:37am

Edited: 04/11/2008 12:39am

hey,
thank you for help. it works realy fine. i have a promlem with adding the error message when i use redirectBack() or redirect('site'), but I'll try a bit more and i think i can find out what to do.

here is my new question:

I created a page type "Kundenbereich", its like MemberArea in english. this pagetype should just make a form, where the users can EDIT their own informations.

This is the Form function for my Edit Page:

   function Form() {
      return new Form($this, "Form", new FieldSet(
         // List the your fields here
         new TextField("edit_FirstName", "Vorname", Member::currentUser()->FirstName),
         new TextField("edit_Surname", "Nachname", Member::currentUser()->Surname),
         new EmailField("edit_Email", "Email Addresse", Member::currentUser()->Email),
         new PasswordField("edit_Password", "Passwort"),
         new PasswordField("edit_Password_two", "Passwort wiederholen")
      ), new FieldSet(
         // List the action buttons here
         new FormAction("doform", "Ändern")
                  
      ));
   }

it works fine too to show the FirstName and Surname in the Textfields. This part is okay, i just want to know how i can set the "Email" field "disabled" (the user should not be able to edit his email/login name)...

Okay, this was Part1 of my Topic here.. now Part2:

THIS IS THE "doForm" function, when the submit button was clicked.

   function doform($data, $form) {
      
      // my email field is not disabled, so i did this IF :D
      if((Member::currentUser()->Email) != (Convert::raw2sql($data['Email'])))
         Director::redirect('kundenbereich/');
      return;
      }

      // Here is the part i dont know to what to do, to edit the member
      $member = new Member();
      $form->saveInto($member);
      
      // Write it to the database. This needs to happen before we add it to a group
      $member->write();
      
      // Ok, you are back at kundenbereich and data was changed
      Director::redirect('kundenbereich/');
   }

i hope you understand me and you can help me.

greetings vom SalvaStripe

Avatar
SalvaStripe

Community Member, 89 Posts

5 November 2008 at 2:19am

i found this function in member.php

getMemberFormFields()

but i dont know how to use / if this could make a form i am searching for.

i want still have the code, what makes that user can edit his own Name, Surname, Password.

here is my file:

<?

class Kundenbereich extends Page {
}

class Kundenbereich_Controller extends Page_Controller {
   function Form() {
      return new Form($this, "Form", new FieldSet(
         // List the your fields here
         new TextField("edit_FirstName", "Vorname", Member::currentUser()->FirstName),
         new TextField("edit_Surname", "Nachname", Member::currentUser()->Surname),
         new EmailField("edit_Email", "Email Addresse", Member::currentUser()->Email),
         new PasswordField("edit_Password", "Passwort"),
         new PasswordField("edit_Password_two", "Passwort wiederholen")
      ), new FieldSet(
         // List the action buttons here
         new FormAction("doform", "Ändern")

      ));
   }
   function doform($data, $form) {

//The User should not change his mail!!!      
      if((Member::currentUser()->Email) != (Convert::raw2sql($data['edit_Email']))) {
         $edit_error = 1;
      }
//-> See my text on bottom <-
      if(($data['edit_Password'] == "") && ($data['edit_Password_two'] == "")) {
         $new_pw = Member::currentUser()->Password;
      } else {
         if($data['edit_Password'] != $data['edit_Password_two']) {
            $edit_error = 1;
         } else {
            $new_pw = Member::currentUser()->Password;
         }
      }
      
      if(!$edit_error) {
         $change = "UPDATE Member Set FirstName = '".$data['edit_FirstName']."', Surname = '".$data['edit_Surname']."', Password = '".$new_pw."' WHERE ID = '1'";
         $update = mysql_query($change);
      }
      
      Director::redirectBack();
   }
}
?>

// If pw is not empty and pw2 is the same then pw is current pw (because i dont know how to convert the pw, what the user submitted.. with the Salt string and so.. at the login check :S)

but i can change the FirstName, the Email, the Surname.. but i think thats not the right method for such an edit data code.

hmm

Avatar
SalvaStripe

Community Member, 89 Posts

5 November 2008 at 5:43am

okay
for the update i use this code now..

      $myMember = DataObject::get_by_id('Member',Member::currentUser()->ID);
      if($myMember) {
       $myMember->FirstName = $data['edit_FirstName']; // sets property on object
       $myMember->Surname = $data['edit_Surname']; // sets property on object
       $myMember->write(); // writes row to database
      }

but i just want to show the email too in the form, but the user should not have the choose to edit its email.
and: how to convert the pw, that i can save it in the password field?