Force Validation error in Laravel 5.1 - php

Is there a way to force an error when using Validator Class in Laravel 5.1?
For now, I've done the traditional way calling $validator = Validator::make($request->all(), $rules), which is working fine. Also, I'm attempting to make another validation (manual one) and push it into $validator, but I can't get true when I call $validator->fails().
Even using $validator->errors()->add('field','message') I couldn't force it. Is there another way?

In order to make validation fail, you need to define a validation rule that won't be met. Easiest way is to require some non-existent field.
This will do the trick:
$rules['some_non_existent_field'] = 'required';
$validator = Validator::make($request->all(), $rules);
dd($validator->fails());

I had the same problem too
adding error to Validator doesn't make It to fail, so you need to make a trap for It that forces It to, maybe a required input that doesn't even exist...
but that will make a confusing error for user (example: unreal_input is required), you'd easily change the message to explain what actually made you to fail the process...
$req = Request::all();
$rules = [/*...*/];
if(/*something's wrong*/){
$rules['unreal_input'] = 'required'; // a confusing error in your errors list...
$messages['unreal_input.required'] = '/* explain the real problem in here*/';
}
$validator = Validator::make($req, $rules,$messages);
if ($validator->fails()) return redirect()->back()->withErrors($validator)->withInput();
so in the code above you've manually checked if something's wrong, then you've made a trap for Validator, then you've changed the message to what the real problem was.
NOTE: I suggest you to use a random value instead of something like unreal_input , because If you do this, the user may(?) guess the input's name, and easily using F12 he/she would pass the validation and give some invalid data to you

Related

How can I require all fields matching a regular expression with laravel?

I am making an application where users can upload questions, and questions can have multiple correct answers. The correct answers have names of the form correctAnswer1 correctAnswer2 etc.
I want to know how to require all submitted fields matching this pattern; I was thinking of using something analogous to
/correctAnswer[0-9]/ => 'required'
I'm not sure of the logic behind your requirement, kinda seems you should do things in a different manner, but again I do not know how your app works so I can't be a judge of that. So if the user can add new correct answers fields on the form, and you wan't them to not be empty it makes some sense.
You can't have a regex in the rule name but you can do the following:
$rules = [
// your other rules
];
$correctAnswers = preg_grep( '/^correctAnswer[1-9]{1}$/', array_keys($this->all()));
// use $this->all() when in Http\Requests\YourRequest
// if you are not using the request method of validation (you validate in controller)
// simply replace $this->all() with $request->all() or Input::all().
foreach ($correctAnswers as $correctAnswer) {
$rules[$correctAnswer] = 'required';
}
return $rules;
This assumes you are using the Laravel 5, Http\Requests to validate your input. If you are doin'g the validation elsewhere (in controller for example), just replace $this->all() with $request->all() or Input::all(). I can't give the exact choice as I do not know exactly how you do the validation and what version of laravel you use.
PS: This will match only correctAnswer1 to correctAnswer9. If you want more just play with the [0-9]{1} part of the regex.

Laravel Form Validation, fail if not empty

I have an input field which needs to be empty, otherwise I want the validation to fail. This is an attempt at stopping spam through a contact form.
I've looked at the documentation for the validation but there's nothing to do this, other than the "max" rule, but this doesn't work.
Any other options?
Here's a clean and (probably) bullet-proof solution:
'mustBeEmpty' => 'present|max:0',
In the method where you are validation, extend/add custom rule:
Validator::extend('mustBeEmpty', function($attr, $value, $params){
if(!empty($attr)) return false;
return true;
});
Then you may use this rule like:
protected $rules = array(
'emptyInputField' => 'mustBeEmpty'
);
Then everything is as usual, just use:
$v = Validator::make(Input::except('_token'), $rules);
if($v->passes()) {
// Passed, means that the emptyInputField is empty
}
There are other ways to do it without extending it like this or extending the Validator class but it's an easy Laravelish way to do it. Btw, there is a package available on Github as Honeypot spam prevention for Laravel applications, you may check that.
For Laravel 8.x and above, you may use the prohibited validation rule:
return [
'emptyInputField' => 'prohibited',
];
In laravel 5.8 you can use sometimes for conditional rules adding. 'email' => 'sometimes|email' . This rules will be applied if there is something present in input field.
You can use the empty rule. Details can be seen here: https://laravel.com/docs/5.2/validation#conditionally-adding-rules

Laravel - Custom validation for file doesn't work

I am trying to use custom validation rules for file inputs in Laravel. The file input name is "photo". I am trying to apply a rule called "validate_art", to this file input. I know there's "image" validation rule, but I want to apply my own customized rule to this file input.
Here's the code :
$rules = array('name'=>'required|unique:user,name','startyear'=>'numeric','endyear'=>'numeric'
,'photo'=>'validate_art');
Validator::extend('validate_art',function($attribute,$value,$parameters){
// returning false just for testing purpose
return false;
});
What happens is, it doesn't apply the rule to the field at all. When I try to use this rule to some other field for testing purposes, it works without failure. Am I doing something wrong? Or custom validation cannot be applied for file inputs? Please someone, throw some light at this. I would really appreciate that.
first you should modify your input data
foreach ($inputs as $k=>$input){
if($input instanceOf \Symfony\Component\HttpFoundation\File\UploadedFile)
$inputs[$k]=$_FILES[$k];
}
then send $input to validator $validator=Validator::make($input,$rules)

Laravel Session Array Values

I've just started to test out Laravel. I'm using a form with some fields and trying to validate the inputs using Laravel's built-in validator class.
$input = Input::all();
$rules = array(
'fname' => 'required|max:100',
'lname' => 'required|max:100',
'email' => 'required|email',
);
$validation = Validator::make($input, $rules);
if ($validation->fails()){
return Redirect::to('inputform')
->with('input_errors', $validation->errors);
}
Everything goes well, and the validation check works. When validation fails, I put the errors in a session variable called input_errors and pass it to the view. My problem is that I can't seem to display the errors. I tried a foreach loop using the blade templating engine as given below:
#foreach (Session::get('input_errors') as $message)
{{ What Should I put here? }}
#endforeach
How can I display the errors that are being returned as an array. I tried referencing it as $message[0][0] but it didn't work.
Thanks.
EDIT: Sorry, forgot to mention that I'm using Laravel 3
The correct syntax for getting the errors is...
$messages= $validation->messages();
That alone, unfortunately, is not going to return you the messages. It's going to return a MessageBag instance. This allows you to pull out any specific messages you want or all.
If you want to get all the messages, now you can do do...
$errors = $messages->all();
That will return an array you could loop through in your view to display errors. There are also methods for getting errors on a specific field such as...
$firstNameError = $messages->first('fname');
or
$firstNameErrors = $messages->get('fname');
I also suggest when sending error messages to the view, to use...
->with_errors($validation);
That will flash the errors to session and automatically assume you are sending them as an $errors variable. Then you may display the errors in your view with.
{{ $errors->first('fname') }} // Blade approach
<?php echo $errors->first('email'); ?> // Non-blade approach
This way, you don't have to add logic to your views trying to determine if the variable exists before you should try and echo it.
http://four.laravel.com/docs/validation

Laravel custom validation issue

I am trying to register a custom validation rule but it does not seem to work. I need either of 2 fields to be filled in. One is a URL(link) field and other is a File input(file_upload).
Here is my custom validation:
Validator::register('file_check', function($attribute, $value, $parameters) {
if (!trim($value) == "" || array_get(Input::file($parameters[0]), 'tmp_name')) {
return true;
}
return false;
});
$messages = array(
'file_check' => 'Please upload a file or provide a link to files.',
);
$rules = array(
'link' => 'url|file_check:file_upload',
'file_upload' => 'mimes:jpg,jpeg,gif,png,psd,ai,bmp,xls,xlsx,doc,docx,zip,rar,7z,txt,pdf'
);
$validation = Validator::make(Input::all(), $rules, $messages);
if ($validation - > fails()) {
return Redirect::to('page') - > with_errors($validation - > errors) - > with_input();
}
Need help :)
EDITED
Also, I just noticed that the validation rule should accept "PSD" files but when I try to upload a PSD file it redirects with the error "Invalid file type".
I am maybe late in party but may be somebody will find it useful, in case you need to create implicit rule which will be called even if field is not present in Input (like required,required_if....) use
Validator::extendImplicit( 'validator_name', function($attribute, $value, $parameters)
{
});
Check this out
I was just struggling with this myself! It turns out that except when a few specific rules are applied to them, Laravel doesn't pass empty fields through the Validator at all. So a custom either-this-or-that rule can't work, since at least one of the two fields is likely to not be visible to it.
You can get around this by moving from the registering-a-new-rule approach to the alternate extend-the-Validator-class approach. Your new class will inherit all the methods of the standard Validator, including a method called "implicit" (you can find the original on line 215 of the standard Validator class), which specifies a whitelist of rules that Laravel should pass fields along to even if they are empty. Add your new rule to that list and you should be good to go.
Jason is right, but there is one thing that can be confusing.
Laravel's 'registering a new rule' approach uses the syntax 'Validator::extend(...'. As described elsewhere, this is convenient when you want to customize in a special situation. However, if you want to add a number of reusable rules, then you probably want to use the extend-the-Validator-class approach. In that case, IF you have a rule conditionally requires something, you need to override the existing implicitRules array with a new one adding your rule.
If the first rules you add don't conditionally require, you will think you have it nailed, then you will spend hours trying to figure out why your new 'RequireWhenBlaBla...' rule is invisible.

Categories