Email and username validation not working in yii2 - php

I have a form with lot of fields including username and email. I also put some validation rules into the relevant model. When I submit the form I want to show the result "username already exist" if that email address is already present in the system.
I'm using:
[['email'], 'unique']
But when I submit with already existing record it doesn't show the validation error.
When I comment
[['user_name', 'full_name', 'password', 'email', 'address1', 'address2', 'city', 'state', 'country', 'zip', 'phone', 'card_number'], 'required']
rule everything works fine.

finally got it.actually their is no problem.ajax validation didn't happen for username and email .when we fill all other field and submit leaving username and email . validation work and alert us "credentials already exist "

Related

How to check if a value already exists in the database with codeigniter

I found this line : $this->form_validation->set_rules();
But I have no idea where to use it. it seems complicated and everywhere its mentioned its about 'username' and 'password' fields.
I have a variable called 'utr' which I am POSTing to the same page with ajax submit, now I want to to check if it is a unique value that is submitted on form submit and not already in my database.
The table name is 'orders' and the column name is 'utr'.
Thank you.
You can check any value which is already exist in database try this code :
utr is the name which is send by form and is_unique validate table.coulmn in database.
$this->form_validation->set_rules(
'utr', 'UTR Title',
'required|is_unique[orders.utr]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
);

e-mail validation rule in Laravel

I am doing validation this way.
$rules = [
'email'=> 'required|regex:/^.+#.+$/i|unique:tab_example,email,'.$this>get('example_id').',example_id'
];
return $rules;
However, I am not getting success.
The error informs that
the email already exists
What I want is that if the email already exists and is from the same user does not need to inform that the email already exists.
I do not know what the problem is in my code.
You can use
'email' => "required|email|unique:users,email,{$id},id",
The id should be replaced with the primary key column name of the table you use for the unique check. The {$id} should be defined before $rules array like:
$id = $request->route('user')
Sometimes, you may wish to ignore a given ID during the unique check.
For example, consider an "update profile" screen that includes the user 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 can use like:
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
Try this
'email' => Rule::unique('users')->ignore($user->id, 'user_id');
Read Under the Section Forcing A Unique Rule To ignore A given Field
Try This way
$rules = [
'email'=> ['required', 'email', \Illuminate\Validation\Rule::unique('tab_example', 'email')->whereNot('example_id',$this->get('example_id'))]
];
Just use
$this->route('example_id')
instead of
$this>get('example_id')
And if you use resource route then use $this->route('user').
$rules = [
'email'=> 'required|regex:/^.+#.+$/i|unique:tab_example,email,'.$this->route('example_id').',example_id'
];
return $rules;

CodeIgniter Form Validation matches got too many error messages

I have an error message problem about form validation matches.
Here is what I set:
$this->load->library('form_validation');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
and of course, I have a form with two password input field.
form_password('password');
form_password('cpassword');
I am dealing with error messages using validation_errors() function.
if I let two password fields blank, i got:
The Password field is required.
The Confirm Password field is required.
And if I type something in password and let the Confirm Password field blank, I got:
The Confirm Password field is required.
so far so good until:
I type something in Confirm Password field and let the Password field blank, I got:
The Password field is required.
The Confirm Password field does not match the Password field.
I got two messages instead of one.
I just need the "The Password field is required." only.
What can I do for this? Please help, thanks.
In my own applications, I set up my validation rules like this
$this->form_validation->set_rules('password', 'Password', 'required|trim|matches[cpassword]');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim');
The password is required, and since it has to match cpassword then you are validating cpassword by default.
As James Lalor said above. here is the working code.
$this->load->library('form_validation');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim');
if (!empty($this->input->post('password'))) {
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
}

CodeIgniter set_message not working

public function doAssignerSU() {
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric');
$this->form_validation->set_rules('password', 'Password', 'required|alpha_numeric|min_length[4]');
$this->form_validation->set_message('min_length','Minimum of 4 characters');
When I display my validation error in my view, it still displays the preset 'The {field} must be minimum of ..'
what's my mistake? Thanks in advance
Set your custom message like this..
$this->form_validation->set_rules('password', 'Password', 'required|alpha_numeric|min_length[4]',array('min_length'=>'Minimum of 4 characters'));
Hint:You can directly set your custom messages when setting rules.
For more see Codeigniter Form Validation

Display single message in all validation in codeigniter

I user validation method in my codeigniter project.
In that i set rules for validation like.
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
Here display three difference errors when validation failed.
I want to display only one error like 'All fields are required' instead of follwing three.
You can add the attribute required in your inputs, so you don't need to send data to the server to get it checked.
<input type="text" name="fieldname" required />
and you'll get on every input a message saying that it's required.
add this line in controller
$this->form_validation->set_message('required', 'All fields are required');
$this->form_validation->set_message('valid_email', 'All fields are required'); // using this we overrideerror message found in the language file

Categories