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
Related
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.
I want to send reset link to user email address, I have tree type of users and tree tables for each of them.
Also I create tree guard that I seperate them by this guards.
I can send reset email for each of them, but the route for each one is not same and have just a prefix for each users.
for example for student the reset link shoule be this :
http://localhost:8000/student/password/reset/e39f6ef2a2cc4d88c1b7be6afc9ecfe876f9dc194b98740347e8c842aa17554b
and for admin this:
http://localhost:8000/admin/password/reset/e39f6ef2a2cc4d88c1b7be6afc9ecfe876f9dc194b98740347e8c842aa17554b
How I can send email by correct link?
You may want to do like this.
At first use one common form to select user type.
Then use 3 different type of forms for 3 different types of users to
give their email or whatever information to reset password. Only 1
particular user form will show up based on selected user type from
first form. You can use jquery/JavaScript to show up the second
form. Then from the second form action, you can call 3 different type
of routes.
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
I have a form in Symfony2 where an Admin have to write the username of another user.
Is it possible to check before to send the form, if the username exist in my database? I use doctrine.
Thanks!
You could write a custom constraint, which is injected with your Entity Manager and checks if the username exists. See the documentation for creating a custom constraint here.
Another option would be to write a data transformer which converts a username string into a User object, which should ensure that user existed throwing a TransformationFailedException if she doesn't. This might be a bit easier because the example in the docs already creates the transformer as a service and shows you how to inject the entity manager.
Also, depending on how many users you have in your app, you could make the username option an entity choice type, and allow symfony to create a drop down with all of your users already in there.
EDIT:
Sorry, I missed the part about checking before you submit the form. The ideas above are still how you could implement on the server, but for the first two options you would need to use an Ajax request to test if the username was valid.
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