Special validation of email field in Laravel - php

I have simple settings section in my app where people can change various account settings including email. Once they have posted the form, I'm using FormRequest to validate all the fields agains some rules, easy. But how do I validate email field? here is what I mean. In case user wanna update the email, we need to make sure it hasn't been taken by someone else. But we can't define rules as
'email' => 'email|unique:users',
because, if they don't want to update the email, they would never be able to submit the form (by default, when page loads, I display current email prefilled in the field). So, we need this email to be unique to the table except for the current user's email. how to correctly validate the email field?

It's simple you have ti just define that which user email address is ignore,Yes you can ignore multiple email address in validation.
For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address. You only want to throw a validation error if the user provides an e-mail address that is already used by a different user. To tell the unique rule to ignore the user's ID, you may pass the ID as the third parameter:
'email' => 'unique:users,email_address,current_user_id'
Form more detailed information click here.

you are close, you just have to add in more variable to your rules as the following:
'unique:users,email,'.Auth::id().',id'
the statement above will build unique rules and ignore the email which own by the current authenticated user

Related

Wordpress remove user registration for custom implementation

I am integrating a user verification feature into a plugin I am developing whereby a user must verify their email address by clicking a link sent to them.
It is based on code provided on Github
At the moment I create a 'temporary' user, then delete the user from the users table. Only after verification is the user added back into the users table.
Is there a way to disable the core user registration in Wordpress so that I don't have to delete the user, therefore it is never stored in the database until it is created by the verification code?
I am finding, quite naturally, the user IDs are skipping every one digit so that for example, a verified user has an id '1' then the next is '3'.
Thanks,
Leon
The standar way to do that is through a field in the table users that is set to true for example whenever the user have validated his email via your link.
And with this field you control that if the field is not validated you dont let them sign in on you website.
So in order to apply this you need to find the sections in your wordpress that control de sign in to put the restriction with this new field
I'm going to be more specific so you can remove the downvote...
Lets think for example that you have a field named email_verify which will just contain a 1 or a 0 if the email is already validated or not.
Then you have another field for example session_token with a sha1 or random token that must be unique for the link that will validate the email when clicking on it.
You need to have a php function that catches when someone enters that link and you do it by extracting the sha1 from the link as an url parameter and searching in your table for whoever have that session_token, when you find a record with this session_token then you turn your email_verify value to 1 meaning the email is already verified and then you turn null the session_token field so the link expires.

Laravel 4 validation email unique constraint

I'm writing a Laravel 4 app that has a users table with the usual contact info. In my users model, my validation for the email specifies 'email'=>'required|email|unique:users', which works fine when registering new users.
My question is how to handle the user edit form-- when the form is submitted, I'd like for the unique email constraint to only be fired if it's not the same as the old email-- otherwise you can't save your profile, since the email (your email) is already inuse.
Thanks
The third parameter to the unique rule allows you to specify an id of a record to ignore. When you're editing a user, you want your unique validation rule to ignore the value contained by the id of the user you are editing.
'email'=>'required|email|unique:users,email,'.$userId
You can see the docs on the validation rule here.
The trickiest part you'll run into is figuring out how to edit your rule to provide the id of the user you're editing. That all depends on how you have your rules set up and where you're doing your validation.
Unique Validation Syntax
unique:table,column,except,idColumn
Try this to exclude the current email
unique:'users', 'email', Auth::user()->email

How to create this type of form in PHP

I have to put a sort of "double opt-in newsletter registration form" in a website.
Since I don't know much about php, I thought about how to limit the code I have to write, and I thought this:
I want to create a registration form (with fields: name, email address and an OBLIGATORY checkbox); when the user clicks Submit:
Of course it checks that all the fields are filled in.
An email is sent to that email address.
User RESPONDS to that email, I see the reply on my email and I manually add him to my mailing list.
Is this a reasonable thing? If so, how do I do that?
Thanks.
I wanted to add some steps to make it automatic, you don't have to do it manually
Of course it checks that all the fields are filled in.
An email is sent to that email address. with a unique link with some random key
link http://domain.com/confirm.php?regId=4&key=DTSRROymc90JDklrTu2wi64Nny0
User RESPONDS to that email by clicking on confirm link in email,
You get the response from confirm.php?regId=4&key=DTSRROymc90JDklrTu2wi64Nny0 and update its status to confirmif its found in db using regId=4 and key=DTSRROymc90JDklrTu2wi64Nny0
in confirm.php
if(isset($_GET['regId']) && isset($_GET['key'])) {
//Get the reg details and update the status if row found in db
....
}
Here is an script, give it a try

CodeIgniter | How to solve this logical issue?

I have a function that lets you update your information, like email and username. The email field has a validation is_unique[table.email]. When lets say the user doesn't want to change the email, but just the country or other info, the is_unique is going to display the message. But if I remove it in update page, the user might put an email address that is already in database.
What would be best logical method of solving this issue?
Assuming you are using a database query and counting the number of rows it returns (0 meaning that the address is free), you'll want to pass the user's unique ID along:
$result = $this->db->where('email_address',$email)->where('id !=',$userid)->get('users')->num_rows();
What I just did above will check if anyone other than our current user is signed up with the requested email. If this user wants to just update his/her country, it won't trigger an error.
edit: This would be in your custom form validation rule __unique_email() or something similar. Which needs to be in the controller.
Compare the email that's being submitted to the email in the database. If it's different then use the email validation. If not, don't.
you can add a hidden field in your form containing the value of the current email address then if the submitted value is different run the validation

Form with email validation with Drupal

I need to create this using Drupal, but I have no idea how to simply implement this.
Form with an input to insert an email and a submit button.
When the form is submit, send a validation email to make sure the email addr is valid
When the user validate the email (clicking the link in his email), the email is stored in the database.
Thanks a lot for your help!
I guess there are better solutions, but here is a rather simple way. I assume you do know how to basically do stuff in Drupal like creating forms, storing information in the database, sending mails and so on. If not, there is a link for each...
Define a table with email (varchar), key (varchar), status (int) (hook_schema)
Create a form where the user can enter the email (how to create a form
Store the mail in the database together with a random key (like md5($mail . uniqueid()) (db_insert for D7, db_query for D6), set status to 0
Send a mail to the user with a link that contains the key like yourmodule/verify/$key (drupal_mail
Register that path (yourmodule/verifiy/%) in hook_menu
When the user clicks on the link, look for that key in the database and set status to 1 (db_query + db_update (D7 only))
Done, all mails with status 1 are now confirmed.
And as others have mentioned, drupal core user registration is already doing this.

Categories