How can I validate one input with multiple values? I'm using bootstrap tagsinput plugin. It returns all tags in one field. I need to validate this tags - unique.
First I'm trying to place this tags into array and then validate it in request but still no luck.
Here is my code in request:
public function all()
{
$postData = parent::all();
// checkbox status
if(array_key_exists('keywords', $postData)) {
// put keywords into array
$keywords = explode(',', $postData['keywords']);
$test = [];
$i = 0;
foreach($keywords as $keyword)
{
$test[$i] = $keyword;
$i++;
}
$postData['keywords'] = $test;
}
return $postData;
}
public function rules()
{
$rules = [
'title' => 'required|min:3|unique:subdomain_categories,title|unique:subdomain_keywords,keyword',
'description' => '',
'image' => 'required|image',
'keywords.*' => 'min:3'
];
return $rules;
}
But as soon as keyword becomes invalid I get this error:
ErrorException in helpers.php line 531:
htmlentities() expects parameter 1 to be string, array given.
Any ideas what's wrong?
I was running into a similar problem with comma separated emails on 5.4. Here's how I solved it:
In your request class, override the prepareForValidation() method (which does nothing by default, btw), and explode your comma separated string.
/**
* #inheritDoc
*/
protected function prepareForValidation()
{
$this->replace(['keywords' => explode(',', $this->keywords)]);
}
Now you can just use Laravel's normal array validation!
Since my case needed to be a bit more explicit on the messages, too, I added in some attribute and message customizations as well. I made a gist, if that's of interest to you as well
Instead of mutating your input, for Laravel 6+ there is a package to apply these validations to a comma separated string of values:
https://github.com/spatie/laravel-validation-rules#delimited
You need to install it:
composer require spatie/laravel-validation-rules
Then, you can use it as a rule (using a FormRequest is recommended).
For example, to validate all items are emails, not long of 20 characters and there are at least 3 of them, you can use:
use Spatie\ValidationRules\Rules\Delimited;
// ...
public function rules()
{
return [
'emails' => [(new Delimited('email|max:20'))->min(3)],
];
}
The constructor of the validator accepts a validation rule string, a validation instance, or an array. That rules are used to validate all separate values; in this case, 'email|max:20'.
It's not a good practice to override the all() method to validate a field.
If you receive a comma separated String and not an array that you want to validate, but there is no common Method available, write your own.
in your App/Providers/ValidatorServiceProvider just add a new Validation:
public function boot()
{
Validator::extend('keywords', function ($attribute, $value, $parameters, $validator)
{
// put keywords into array
$keywords = explode(',', $value);
foreach($keywords as $keyword)
{
// do validation logic
if(strlen($keyword) < 3)
{
return false;
}
}
return true;
}
}
Now you can use this Validation Rule on any request if needed, it is reusable.
public function rules()
{
$rules = [
'title' => 'required|min:3|unique:subdomain_categories,title|unique:subdomain_keywords,keyword',
'description' => 'nullable',
'image' => 'required|image',
'keywords' => 'keywords',
];
return $rules;
}
If the validation pass and you want to make the keywords be accessible in your request as array, write your own method:
public function keywords ()
{
return explode(',', $this->get('keywords'));
}
Related
I am using the Lumen Framework, which utilizes the Laravel Validation
I wanted to create a Validator Rule to make the Request->input() json only contain specific keys at the root like "domain" and "nameservers". Not more and not less.
Example passing the rule:
{
"domain":"domain.tld",
"nameservers":
{...}
}
Example not passing the rule:
{
"domain":"domain.tld",
"nameservers":
{...},
"Hack":"executeSomething()"
}
I tried to use to use several default validation rules to achieve this but wasnt successful.
My approach was now to put the request in another array like this
$checkInput['input'] = $request->all();
to make the validator validate the "root" keys.
Now this is my Approach:
create the validator
$checkInput['input'] = $request->all();
$validator = Validator::make($checkInput, [
'input' => [
'onlyContains:domain,nameservers'
],
]);
creating the rule
Validator::extend('onlyContains', function($attribute, $value, $parameters, $validator){
$input = $validator->getData();
$ok = 0;
foreach ($parameters as $key => $value) {
if (Arr::has($input, $attribute . '.' . $value)) {
$ok++;
}
}
if (sizeof(Arr::get($input, $attribute)) - $ok > 0) {
return false;
}
return true;
});
It seems i got the desired result, but i am asking if there is maybe smarter solution to this with the default rules provided by Laravel/Lumen.
You are trying to do a blacklisting approach blocking out fields that are not intended. A simple approach, that is utilized a lot, is to only fetch out the validated. Also you are trying to do logic, that goes against normal validation logic, to do it a field at a time.
This is also a good time, to learn about FormRequest and how you can get that logic, into a place where it makes more sense.
public function route(MyRequest $request) {
$input = $request->validated();
}
With this approach, you will only ever have the validated fields in the $input variable. As an extra bonus, this approach will make your code way easier to pick up by other Laravel developers. Example form request below.
public class MyRequest extends FormRequest
{
public function rules()
{
return [
'domain' => ['required', 'string'],
'nameservers' => ['required', 'array'],
];
}
}
You should use prohibited rule.
For eg:
$allowedKeys = ['domain', 'nameservers'];
$inputData = $request->all();
$inputKeys = array_keys($inputData);
$diffKeys = array_diff($inputKeys, $allowedKeys);
$rules = [];
foreach($diffKeys as $value) {
$rules[$value] = ['prohibited'];
}
Currently in lumen when you use the $this->validate($request, $rules) function inside of a controller it will throw a ValidationException with error for your validation rules(if any fail of course).
However, I need to have a code for every validation rule. We can set custom messages for rules, but I need to add a unique code.
I know there's a "formatErrorsUsing" function, where you can pass a formatter. But the data returned by the passed argument to it, has already dropped the names of the rules that failed, and replaced them with their messages. I of course don't want to string check the message to determine the code that should go there.
I considered setting the message of all rules to be "CODE|This is the message" and parsing out the code, but this feels like a very hacked solution. There has to be a cleaner way right?
I've solved this for now with the following solution:
private function ruleToCode($rule) {
$map = [
'Required' => 1001,
];
if(isset($map[$rule])) {
return $map[$rule];
}
return $rule;
}
public function formatValidationErrors(Validator $validator) {
$errors = [];
foreach($validator->failed() as $field => $failed) {
foreach($failed as $rule => $params) {
$errors[] = [
'code' => $this->ruleToCode($rule),
'field' => $field,
];
}
}
return $errors;
}
I want to get the parameter passed in the validation rule.
For certain validation rules that I have created, I'm able to get the parameter from the validation rule, but for few rules it's not getting the parameters.
In model I'm using the following code:
public static $rules_sponsor_event_check = array(
'sponsor_id' => 'required',
'event_id' => 'required|event_sponsor:sponsor_id'
);
In ValidatorServiceProvider I'm using the following code:
Validator::extend('event_sponsor', function ($attribute, $value, $parameters) {
$sponsor_id = Input::get($parameters[0]);
$event_sponsor = EventSponsor::whereIdAndEventId($sponsor_id, $value)->count();
if ($event_sponsor == 0) {
return false;
} else {
return true;
}
});
But here I'm not able to get the sponsor id using the following:
$sponsor_id = Input::get($parameters[0]);
As a 4th the whole validator is passed to the closure you define with extends. You can use that to get the all data which is validated:
Validator::extend('event_sponsor', function ($attribute, $value, $parameters, $validator) {
$sponsor_id = array_get($validator->getData(), $parameters[0], null);
// ...
});
By the way I'm using array_get here to avoid any errors if the passed input name doesn't exist.
http://laravel.com/docs/5.0/validation#custom-validation-rules
The custom validator Closure receives three arguments: the name of the
$attribute being validated, the $value of the attribute, and an array
of $parameters passed to the rule.
Why Input::get( $parameters ); then? you should check $parameters contents.
Edit.
Ok I figured out what are you trying to do. You are not going to read anything from input if the value you are trying to get is not being submitted. Take a look to
dd(Input::all());
You then will find that
sponsor_id=Input::get($parameters[0]);
is working in places where sponsor_id was submited.
I'm searching for a cleaner way to validate tags when storing a Post.
All of the input validation takes place within my custom request StorePostRequest. The problem is that I need to check whether the given tags exist in the database, only existing tags are allowed. The function $request->input('tags') returns a string with comma seperated values, for example: Tag1,Tag2,Tag3.
Here is the code:
/**
* Store a newly created resource in storage.
*
* #param StorePostRequest $request
* #return Response
*/
public function store(StorePostRequest $request)
{
//THIS PIECE OF CODE
$tags = explode(',', $request->input('tags'));
$tags = Tag::whereIn('title', $tags)->lists('id');
if(count($tags) < 1)
{
return redirect()->back()->withInput()->withErrors([ trans('tags.min') ]);
}
else if(count($tags) > 5)
{
return redirect()->back()->withInput()->withErrors([ trans('tags.max') ]);
}
//TILL HERE
$post = $request->user()->posts()->create([
'slug' => unique_slug('Post', $request->input('title')),
'title' => $request->input('title'),
'description' => $request->input('description'),
'summary' => $request->input('summary'),
]);
$post->tags()->attach($tags);
return redirect(route('theme.post.show', [$theme->slug, $post->slug]))->with(['success', trans('messages.post.store')]);
}
The code is a little sloppy and redundant when using it in multiple controllers.
In order to solve this, I've created a ValidationServiceProvider to extend the core validator rules. Something like this:
$this->app['validator']->extend('tags', function ($attribute, $value, $parameters)
{
$tags = explode(',', $value);
$tags = Tag::whereIn('title', $tags)->lists('id');
if(count($tags) < 1 || count($tags) > 5))
{
return false;
}
});
Pretty neat. The thing is I still need to be able to access the $tags variable within the controller (because of ->attach($tags)).
Is there a better way of tackling this problem? Or should I stop thinking and just use (and repeat) the code I have?
Thanks in advance, hope it makes some sence.
I am assuming that you understand the use of this class because I have seen that you have defined StorePostRequest class. So, just for clarify, the rules method could looks like:
public function rules()
{
return [
'tags' => ['required', 'tags'] //kb
];
}
Finally, with all the tools in correct place, you only make manipulate the data in your controllers like this:
public function store(StorePostRequest $request)
{
// at this point, the tags are already validated, so we, proceed get them:
$tags = explode(',', $$request->get('tags'));
$post = $request->user()->posts()->create([
'slug' => unique_slug('Post', $request->input('title')),
'title' => $request->input('title'),
'description' => $request->input('description'),
'summary' => $request->input('summary'),
]);
$post->tags()->attach($tags);
return redirect(route('theme.post.show', [$theme->slug, $post->slug]))->with(['success', trans('messages.post.store')]);
}
Keep in mind that inyecting StorePostRequeststore in the controller's function it is already validating and running the rules.
That is enough if you really has defined the StorePostRequest's rules correctly.
foreach($request->tags as $k=>$tags){
$this->validate($request, [
'tags.'.$k => 'required|string|max:20'
]);
}
I've some areas in my form something like:
<ul>
<li>
<input type="checkbox" name="post_categories[]" value="16">English First Main Category<br>
<ul>
<li><input type="checkbox" name="post_categories[]" value="17">English First Subcategory<br></li>
</ul>
</li>
</ul>
When I try to validate them as required fields or something else, Laravel did not validate rules. My rules something like below (In /application/models/posts.php):
public static $rules = array(
'post_title' => 'required',
'post_body' => 'required',
'content_language'=>'required|alpha',
'post_categories'=>'array_numeric',
'post_sequence_number'=>'numeric'
);
public static function validate($data){
return Validator::make($data, static::$rules);
}
In /application/library/validate.php I've a function that validates the array is numeric or not:
Class Validator extends Laravel\Validator {
public function validate_array_numeric($attribute, $value, $parameters){
$numeric_values = array_filter($value, create_function('$item', 'return (is_numeric($item));'));
return count($numeric_values) == count($value);
}
}
Rules works perfectly, except post_categories[]. I get the error:
Method [array_numeric] does not exist.
Cheers.
I had to solve the same problem. Here's what I did:
I created a custom class that extends the default Laravel\Validator class. I wanted to be able to tell the validator when I'm dealing with multiple values (like in your case). In my implementation this could be done by appending '_array' to every validation rule for a certain field name. What my class does is to check if the rule name has this suffix and if it does the value parameter (which is an array in this case) is broken down to its contained items and passed to the default validation functions in Laravel.
<?php
class Validator extends Laravel\Validator {
public function __call($method, $parameters)
{
if (substr($method, -6) === '_array')
{
$method = substr($method, 0, -6);
$values = $parameters[1];
$success = true;
foreach ($values as $value) {
$parameters[1] = $value;
$success &= call_user_func_array(array($this, $method), $parameters);
}
return $success;
}
else
{
return parent::__call($method, $parameters);
}
}
protected function getMessage($attribute, $rule)
{
if (substr($rule, -6) === '_array')
{
$rule = substr($rule, 0, -6);
}
return parent::getMessage($attribute, $rule);
}
}
As stated in the posts above you will have to make the following change so that your custom Validator class can be found by the Autoloader:
Then you need to remove the following line from
application/config/application.php file:
'Validator' => 'Laravel\Validator'
When this is done you'll be able to use all of Laravel's validation rules with the '_array' suffix. Here's an example:
public static $rules = array(
'post_categories'=>'required_array|alpha_array'
);
I don't know if this issue has been solved in Laravel 4. Maybe you can try it.
But what I'm doing right now is extending the validation class.
You can create a new library that extends the validation class.
To validate if all the items in the array has numeric values. This is in application/libraries:
class Validator extends Laravel\Validator {
public function validate_arraynumeric($attribute, $value, $parameters){
$numeric_values = array_filter($value, create_function('$item', 'return (is_numeric($item));'));
return count($numeric_values) == count($value);
}
}
To change the default error message when the validation fails. Go to application/language/en/validation.php. Just use the name of the function as the key for the new array item:
"arraynumeric" => "The :attribute contains non-numeric values",
update
Then you need to remove the following line from application/config/application.php file:
'Validator' => 'Laravel\\Validator'
To use the new validation:
public static $rules = array(
'post_categories'=>'array_numeric'
);
Now for the required checkbox. I assume you're just requiring one checkbox to be checked. You can just check in the function for the new validation the count of the checked checkboxes if it is at least 1.
You're doing something strange here.
Using post_categories[] as form names generates an array. This means you cannot validate it with 'post_categories[]'=>'required|numeric'. Instead you have to loop through the array and validate each field on it's own.