Set a non-standard validation error message in Codeigniter 3 - codeigniter

I am working on a basic blog application in Codeigniter 3.1.8.
The application has a registration form. You have to accept the Terms and Conditions to register.
I have this valdation rule:
$this->form_validation->set_rules('terms', 'Terms and Conditions', 'required');
The problem with the above line is that it outputs the standard error message "The Terms and Conditions field is required", while i want "You have to accept out Terms and Conditions".
How do I achieve this?

You can find the answer in the documentation:
$this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3',
array('rule2' => 'Error Message on rule2 for this field_name')
);
Then on your code you should have something like this:
$this->form_validation->set_rules('terms', 'Terms and Conditions', 'required', array ('required' => 'You must accept the Terms and Conditions'));

Related

Laravel 5.5 form validation numeric/integer accepts non-digital characters like "+"

I want to validate the form data if it is just numeric/integer (as in just numbers). Based on Laravel's documentation there are two specific validators for that. But the problem I'm facing is that both the validators accept non-numeric characters such as "+" or "-".
numeric
The field under validation must have a numeric value.
integer
The field under validation must be numeric.
How can I make the validation to only accept numbers and not other non-numeric characters?
'main_telephone' => 'numeric',
'main_fax' => 'integer',
'direct_telephone' => 'integer',
'mobile' => 'integer',
Below is the screenshot
if anyone has come across this problem the best possible solution is to use regex. I do not know why I didn't think of this before.
'main_telephone' => 'integer|regex:/^[0-9]*$/',
'main_fax' => 'integer|regex:/^[0-9]*$/',
'direct_telephone' => 'integer|regex:/^[0-9]*$/',
'mobile' => 'integer|regex:/^[0-9]*$/',

validation and duplicate update() in laravel

In my form Voiture, I want to do a validation system for the fields immatriculation and num_vehicule
Here i an overview:
If I edit the first recording ie the value of the field num_vehicule 000001 per 0000032and that I validate, I have an error message because the value of the field immatriculation already exists.
I don't understand the problem... Do you have an idea please?
'immatriculation' => 'required|string|max:15|min:6|unique:voitures,immatriculation',
'num_vehicule' => 'required|string|max:6|min:6|unique:voitures,num_vehicule',
'fk_modele' => 'required'
Thank you
Use
'immatriculation' => 'string|max:255|unique:voitures,immatriculation,' . $this->voitures->id,
The syntax is
'input_field' => 'unique:<table name>,<column name for this input field>, <unique id>, <unique id column in table>';

Laravel: validate format when at least one of two input fields required

Suppose, in the feedback form we want to ask at least one of two contacts: email and/or telephone number. So, in the validation rules will be required_without for each other:
$this->validate($request, [
'email' => 'required_without:tel|email',
'tel' => 'required_without:email|regex:/(01)[0-9]{9}/'
], $messages);
We need to validate the contact data format only when it has been inputted, but in the code above the format of both fields will be checked. Because we need at least one contact, sometimes rule is no use.

Laravel validation rule `confirmed` message show on wrong field

For example you have 2 inputs: password and password_confirmed.
model
$rule = array (
'password' => 'min:4|confirmed',
'password_confirmed' => 'min:4',
);
If the user inputs the wrong password in the password_confirmed input, the validator sends the message to password so that the error message gets displayed with the password errors and not the password_confirmation errors.
How do I make the confirmation error go to the password_confirmation messages?
Well, this may not be a direct answer to your question, but will still give desired result.
Look into custom messages for your validation:
$messages = [
'password.confirmed' => 'Your passwords were mismatched',
'password_confirmation.min' => 'Your password must be at least 4 characters'
];
You should be able to get the desired message for any of your fields by utilizing this feature. Unless I am misunderstanding your issue?

Laravel - email uniqueness validation fails

I have the following rules:
$rules = array(
'name' => 'required|alpha_num|unique:users,username',
'password' => 'required|min:6|confirmed',
'email' => 'required|email|unique:users'
);
unique checks for existing emails properly, HOWEVER, if I input an invalid email address (something like kolÄ™#ddd.com) I get a "Woops, something went wrong" laravel error. It seems that the validation doesn't stop at the email rule and the wrong email address is being passed to the unique rule. If I remove the unique rule it works properly, but doesn't check if a certain email exists. How do I solve this?
I've found out what was causing the validator to fail: mysql table row collation.
I've set it to utf8-general-ci and it's all working now.

Resources