How to pass external validation through model labels and error messages? - validation

In Kohana 3.2, passing external validation on Model_User upon save, why won't the correct message show?
I have user.php in application/messages/models which reads and translates fine for the "internal" data, while _external.php resides in application/messages/models/user.
When _external data is invalid, the default error message from Kohana is shown, and thus not correctly translated or given the correct labels from Model_User.
Edit, with code:
// We have $_POST, register a new user
$user = ORM::factory('user');
/*
* Here a bunch of variables are set
*/
$extra = Validation::factory($_POST)->
rule('email', 'email')-> // I run this check, because in my Model_User, email is filtered through Encrypt
rule('name', 'not_empty'); // Same goes for name
try {
$user->save($extra);
} catch (ORM_Validation_Exception $e) {
$this->template->errors = $e->errors('models', true);
}
So, when $extra variables don't match the rule, I would like to get nice error messages from application/messages/models/user/_external.php, which looks like:
return array(
'email' => array(
'email' => ':field must be a valid email address',
),
'name' => array(
'not_empty' => ':field must not be empty',
),
);
Also, it would be nice if :field was fetched from Model_User "labels".

You need to put _external.php next to your user.php in the messages/models directory, not in the messages/models/user directory. I had the same problem, it worked for me.

Related

FormRequest messages() function does not translate all rules [duplicate]

I'm working on a Laravel 5.8 project and trying to show custom validation messages for a validation which uses the requiredIf validation rule.
Here is how I have it set up:
$validation = Validator::make(
$request->all(),
[
...
'sum' => [
Rule::requiredIf(function() use ($request){
$model = Model::find($request->id);
return $model->is_special; //returns a boolean value
}),
'numeric'
],
...
],
[
...
'sum.required_if' => 'This cannot be blank',
'sum.numeric' => 'Must use a number here',
...
]
);
Now the validation is working correctly and the custom message for the numeric validation shows as should, but the message I get for the requiredIf() method is Laravel's default error message.
I also tried using 'sum.requiredIf' => '...' but that didn't work either and can't seem to find any documentation or example for this scenario.
I was tinkering with this for a while and noticed that for this to work I needed to define
'sum.required' => 'This cannot be blank'
and not 'sum.required_if' => 'This cannot be blank',.
Not sure if this is expected behavior or just a workaround but my deduction is that with the callback Rule::requiredIf(function() use ($request){...}) the parameters :other and :value are not passed so it falls back onto required messaging and I guess this makes sense since required_if and required would not be used on the same :attribute.
Hope this helps anyone who comes across this problem.
First, create a rule name isSpecial or whatever
php artisan make:rule isSpecial
Go to App\Rules\isSpecial.php
private $id;
public function __construct($id) // pass id or what you need
{
//
$this->id=$id;
}
public function passes($attribute, $value) // customize your rules here
{
//
return Model::find($request->id)->is_special;
}
public function message() // here is answer for your question
{
return 'The validation error message.'; // your message
}
in your controller
use App\Rules\isSpecial;
\Validator::make($request->all(), [
'sum' => new isSpecial() ,
])->validate();
another idea :
Specifying Custom Messages In Language Files
In most cases, you will probably specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file.
'custom' => [
'email' => [
'required' => 'We need to know your e-mail address!',
],
],
Simple notice:
- I suggest using HTTP Requests instead use validation in your controller and function direct
Looks like as of Laravel 8, using required_if works as expected, and alternatively will not fall back on required as mentioned previously:
'sum.required_if' => 'This cannot be blank',

Laravel validation messages contain "validation." instead of custom error message

I have a function that does some validation. Instead of $errors->get(key) returning the custom error messages I've defined, I'm getting the validation rule name. For example, if I use an email that's not unique:
$messages = [
'new_email.required' => 'Your new email address is required.',
'new_email:unique' => 'That email is already in use.',
'current_password|required' => 'Your current password must be provided.'
];
$rules = [
'new_email' => 'required|email|unique:users,email,' . $user->id,
'current_password' => 'required',
];
$validator = Validator::make($request->all(), $rules, $messages); // <-- custom error messages passed in here
if ($validator->fails()) {
$errors = $validator->errors();
if ($errors->has('new_email')) {
$msg = $errors->get('new_email'); // $msg contains ['validation.unique'] instead of ['That email is already in use.']
...
}
}
$errors->get('new_email') returns ['validation.unique'] instead of the custom error message in the array that's passed as the 3rd parameter to Validator::make. How can I get the custom error message instead of the validation rule that has been broken by the request?
There are some similar questions to this, but all the answers seem to focus on the resource/lang/xx/validation.php file missing or something like that. I'm not using those localization features at all.
Based on the documentation you should set your message with a string between property and validation rule.
$messages = [
'new_email.unique' => 'That email is already in use.',
];

Custom error message for `requiredIf` validation in laravel

I'm working on a Laravel 5.8 project and trying to show custom validation messages for a validation which uses the requiredIf validation rule.
Here is how I have it set up:
$validation = Validator::make(
$request->all(),
[
...
'sum' => [
Rule::requiredIf(function() use ($request){
$model = Model::find($request->id);
return $model->is_special; //returns a boolean value
}),
'numeric'
],
...
],
[
...
'sum.required_if' => 'This cannot be blank',
'sum.numeric' => 'Must use a number here',
...
]
);
Now the validation is working correctly and the custom message for the numeric validation shows as should, but the message I get for the requiredIf() method is Laravel's default error message.
I also tried using 'sum.requiredIf' => '...' but that didn't work either and can't seem to find any documentation or example for this scenario.
I was tinkering with this for a while and noticed that for this to work I needed to define
'sum.required' => 'This cannot be blank'
and not 'sum.required_if' => 'This cannot be blank',.
Not sure if this is expected behavior or just a workaround but my deduction is that with the callback Rule::requiredIf(function() use ($request){...}) the parameters :other and :value are not passed so it falls back onto required messaging and I guess this makes sense since required_if and required would not be used on the same :attribute.
Hope this helps anyone who comes across this problem.
First, create a rule name isSpecial or whatever
php artisan make:rule isSpecial
Go to App\Rules\isSpecial.php
private $id;
public function __construct($id) // pass id or what you need
{
//
$this->id=$id;
}
public function passes($attribute, $value) // customize your rules here
{
//
return Model::find($request->id)->is_special;
}
public function message() // here is answer for your question
{
return 'The validation error message.'; // your message
}
in your controller
use App\Rules\isSpecial;
\Validator::make($request->all(), [
'sum' => new isSpecial() ,
])->validate();
another idea :
Specifying Custom Messages In Language Files
In most cases, you will probably specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file.
'custom' => [
'email' => [
'required' => 'We need to know your e-mail address!',
],
],
Simple notice:
- I suggest using HTTP Requests instead use validation in your controller and function direct
Looks like as of Laravel 8, using required_if works as expected, and alternatively will not fall back on required as mentioned previously:
'sum.required_if' => 'This cannot be blank',

Email validation rule in Laravel?

I do email validation using the simple rule:
'email' => 'required|email|unique:users,email',
How do I modify the option unique so that it will work only if the entered email is different from the primordial?
A sample:
The field email contains the default value from table users: example#gmail.com
Then I push the button without making any changes in the form I should not check unique:users.
Otherwise, if I even changed one symbol in example#gmail.com1 I must validate the incoming value using: unique:users.
You can find an example here https://laracasts.com/discuss/channels/requests/laravel-5-validation-request-how-to-handle-validation-on-update
You will need to have multiple rules depending on the request method (update or create) and you can pass a third parameter to unique to ensure no fail if you know the user / email
'user.email' => 'required|email|unique:users,email,'.$user->id,
Switch for method
switch($this->method())
{
...
}
I did this using the conditional checks:
$validator = Validator::make($request->all(), []);
$validator->sometimes('email', 'unique:users,email', function ($input) {
return $input->email !== Auth::user()->email;
});
I think it is as loophole in laravel validation.
I update the code for email validation. This is working fine for me.
'email' => [
'required', 'email:rfc',
function($attribute, $value, $fail) {
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
$fail($attribute . ' is invalid.');
}
}],

cakephp 2.x include userExists in validating login

I'm new to CakePhp, I'm using CakePhp 2.x.
I am probably going about solving the problem below the wrong way. And I just know I'm overlooked something real simple but,.....
I'm validating login details based on 'Between 5 to 15 characters' they are retuning errors as expected.
[The MODEL]
public $validate = array(
'username' => array(
'between' => array(
'rule' => array('between', 5, 15),
'message' => 'Between 5 to 15 characters'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Minimum 8 characters long'
)
);
[The CONTROLLER]
public function login() {
if ($this->request->data) {
$this->User->set($this->request->data);
if ($this->User->validates() && $this->Auth->login()) {
if ($user = $this->Auth->user()) {
$this->render($this->Auth->redirect());
}else{
//??
}
}else{
$this->User->create();
pr($this->User->invalidFields());
$errors = $this->User->validationErrors;
$data = compact('errors');
$this->set('errors', $data);
$this->set('_serialize', array('errors'));
$this->Session->setFlash('Your username/password combination was incorrect');
}
}
}
So, the problem is, if the fields follow the rules in the model above even if the login details (the user) doesn't exist, no errors will be returned (no good). Would it be correct to add an other validation for this, adding another rule to check if that user actually exists? If so how!?
Or, do I work this into the controllers login function checking if the user exists? I'm a little confused now. Maybe I've been looking at the screen for too long.
Thanks.
Would it be correct to add an other validation for this, adding
another rule to check if that user actually exists? If so how!?
You can add as many rules as you want. In this case you want the rule "unique". Read this section of the book about data validation.
Or, do I work this into the controllers login function checking if the
user exists?
All data manipulation and validation should happen in the model layer of the MVC stack. So put everything into a model method and pass the post data to it and validate it there. You can put all logic into the controller to but that's stupid in terms of not following the MVC pattern. Models can be shared between shells and controllers for example, a controller not. Again you could instantiate a controller in a shell but doing all of this negates any benefit and idea the MVC pattern has. Also a model is competitively easy to test. And yes, you should unit test your code. Check how our users plugin is doing it for example.
You can specify multiple rules per field...
Follow this link to learn more about it...
http://book.cakephp.org/2.0/en/models/data-validation.html#multiple-rules-per-field
a sample code is given below
<?php
[IN The MODEL]
//the following code checks if the username is notempty, is a valid email and is it already taken or not...
public $validate = array(
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Please enter a valid email.',
),
'email' => array(
'rule' => array('email'),
'message' => 'Please enter a valid email.',
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken.'
)
)
);
?>

Resources