Laravel validation rule `confirmed` message show on wrong field - laravel

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?

Related

Laravel Custom Validation: Show field data of failing record

I need a way to place data of failing record along with count of records that failed because of the same reason. I hope the explanation is enough to get the requirement.
eg:
$rules['inventories.*.activity_id']= [ 'required', 'exists:activities,id ];
$messages['inventories.*.activity_id.required'] = 'Activity id can not be blank.';
$messages['inventories.*.activity_id.exists'] = "Activity id <<< [FAILING RECORD -> ACTIVITY_ID] >>> does not exist in the system. <<< NUMBER OF RECORDS FAILED FOR THIS REASON >>> entries skipped.";
Anyone have an idea about this?
I found I can place :attributes but this does't not what I want.
You could try using the :input attribute to display the value you are checking the existence of in your custom validation message:
'Activity id :input does not exist in the system.'
Though this will not get you to your failure count part.
this shows you number of validation errors:
count($errors)
if you want to show the invalid value that passed among request in the error message, you can do this:
public function messages()
{
return [
'fieldname.numeric' =>
'The :attribute must be numeric. Your value is '.$request->input('fieldname')
];
}

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>';

Set a non-standard validation error message in Codeigniter 3

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'));

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.

CakePHP: How can I read in controller the string value of a flash message if set?

I need to know this so I can append messages (flashes) if needed.
This way I can give the user the full feedback and avoid one flash being overwritten (in a redirect, for ex, where the last controller, usualy, can do that).
I read the documentation and I did't find any option to be given in setFlash() in order to require this appending.
I know there is a Session::read(), but I do not know what key to search for..
Thank you!
What you are looking for is:
$this->Session->read('Message');
Message is the key that hold the session messages for the current user, be it flash messages or auth messages. A simple pr($this->Session->read()) will give you output similar to:
Array(
['Auth'] => array(
... your auth keys and values here
),
['Message'] => array(
['flash'] => ... your current flash message array (if any)
['auth'] => ... your current auth message array (if any)
)
)
Although I am not sure why you are worried. When you do
$this->Session->setFlash('your message');
$this->redirect('/');
Even if you do have a redirect, the session message will persist and will display on the redirected page. You just need to make sure you are outputting the flash messages.
The flash message could be retrieved by using this :
$message = $this->Session->read('Message.flash.message');
echo $message;

Resources