I am building a REST based API, where one of the API is having the following request
{
"categories_id" :"1",
"product_name" : "Pen",
"product_description" : "this is pen",
"tags" : "pen,write",
"image_count" : "4",
"skus":
{
"is_shippable":"n",
"actual_price":"100.55",
"selling_price":"200.45",
"quantity_type":"bucket",
"quantity_total":"10",
"bucket_value":"instock",
"sort_order":"1"
}
}
These are my validation rules
protected $rules = [
ValidatorInterface::RULE_CREATE => [
'users_id' => 'required',
'user_profiles_id' => 'required',
'categories_id' => 'required',
'product_name' => 'required|max:100',
'product_description' => 'required|max:1000',
'tags' => 'required',
'image_count'=>'required|integer',
'creation_mode'=>'required|integer',
'skus.is_shippable'=>'in:y,n',
'skus.actual_price'=>'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
'skus.selling_price' => 'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
'skus.quantity_type' => 'sometimes|required|in:finite,infinite,bucket',
'skus.quantity_total' => 'integer|required_if:skus.quantity_type,finite',
'skus.bucket_value'=>'in:instock,soldout,limited|required_if:skus.quantity_type,bucket',
'skus.sort_order'=> 'required|integer'
],
ValidatorInterface::RULE_UPDATE => [
]
];
The above request is properly getting validated. But The skus can have multiple entities inside like below request
{
"categories_id" :"1",
"product_name" : "Pen",
"product_description" : "this is pen",
"tags" : "pen,write",
"image_count" : "4",
"skus":
[{
"is_shippable":"n",
"actual_price":"100.55",
"selling_price":"200.45",
"quantity_type":"bucket",
"quantity_total":"10",
"bucket_value":"instock",
"sort_order":"1"
},
{
"is_shippable":"n",
"actual_price":"100.55",
"selling_price":"200.45",
"quantity_type":"bucket",
"quantity_total":"10",
"bucket_value":"instock",
"sort_order":"1"
}]
}
How do I validate if there are multiple nested entities?
What version of Laravel are you using? If you are using Laravel 5.2 or if you don't mind updating to it, there's a solution out of the box.
Array Validation
Validating array form input fields is much easier in Laravel 5.2. For example, to validate that each e-mail in a given array input field is unique, you may do the following:
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users'
]);
Likewise, you may use the * character when specifying your validation messages in your language files, making it a breeze to use a single validation message for array based fields:
'custom' => [
'person.*.email' => [
'unique' => 'Each person must have a unique e-mail address',
]
],
Another example from Laravel news:
Pretend you have a form with an array of input fields like this:
<p>
<input type="text" name="person[1][id]">
<input type="text" name="person[1][name]">
</p>
<p>
<input type="text" name="person[2][id]">
<input type="text" name="person[2][name]">
</p>
In Laravel 5.1 to add validation rules it required looping through and adding the rules individually. Instead of having to do all that it’s been “Laravelized” into this:
$v = Validator::make($request->all(), [
'person.*.id' => 'exists:users.id',
'person.*.name' => 'required:string',
]);
So if you don't want to use Laravel 5.2 you will have to do it manually, if you do update to Laravel 5.2 you can use the new array validation and it will be some like this:
protected $rules = [
ValidatorInterface::RULE_CREATE => [
'users_id' => 'required',
'user_profiles_id' => 'required',
'categories_id' => 'required',
'product_name' => 'required|max:100',
'product_description' => 'required|max:1000',
'tags' => 'required',
'image_count'=>'required|integer',
'creation_mode'=>'required|integer',
'skus.*.is_shippable'=>'in:y,n',
'skus.*.actual_price'=>'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
'skus.*.selling_price' => 'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
'skus.*.quantity_type' => 'sometimes|required|in:finite,infinite,bucket',
'skus.*.quantity_total' => 'integer|required_if:skus.quantity_type,finite',
'skus.*.bucket_value'=>'in:instock,soldout,limited|required_if:skus.quantity_type,bucket',
'skus.*.sort_order'=> 'required|integer'
],
ValidatorInterface::RULE_UPDATE => [
]
];
Ihmo the best way to add this extra validation logic is extending the Validator class creating your CustomValidator class, it may be a bit overkill, but when Laravel 5.2 gets released you can remove your CustomValidator and keep on using Laravel's 5.2 Validator without making any changes to your code.
How? Well first we create a folder under our app/
I decided to name this folder Validator you can name it whatever you want, just remember to update the namespace of the following classes. Next we are going to create 3 .php files inside this folder CustomValidator.php, CustomValidatorServiceProvider.php and Factory.php.
<?php
namespace App\Validator;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Validation\Validator;
use Symfony\Component\Translation\TranslatorInterface;
class CustomValidator extends Validator
{
/**
* Create a new Validator instance.
*
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* @param array $data
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return void
*/
public function __construct(TranslatorInterface $translator, array $data, array $rules, array $messages = [], array $customAttributes = [])
{
$this->translator = $translator;
$this->customMessages = $messages;
$this->data = $this->parseData($data);
$this->customAttributes = $customAttributes;
// Explode the rules first so that the implicit ->each calls are made...
$rules = $this->explodeRules($rules);
$this->rules = array_merge((array) $this->rules, $rules);
}
/**
* Explode the rules into an array of rules.
*
* @param string|array $rules
* @return array
*/
protected function explodeRules($rules)
{
foreach ($rules as $key => $rule) {
if (Str::contains($key, '*')) {
$this->each($key, $rule);
unset($rules[$key]);
} else {
$rules[$key] = (is_string($rule)) ? explode('|', $rule) : $rule;
}
}
return $rules;
}
/**
* Define a set of rules that apply to each element in an array attribute.
*
* @param string $attribute
* @param string|array $rules
* @return void
*
* @throws \InvalidArgumentException
*/
public function each($attribute, $rules)
{
$data = Arr::dot($this->data);
foreach ($data as $key => $value) {
if (Str::startsWith($key, $attribute) || Str::is($attribute, $key)) {
foreach ((array) $rules as $ruleKey => $ruleValue) {
if (! is_string($ruleKey) || Str::endsWith($key, $ruleKey)) {
$this->mergeRules($key, $ruleValue);
}
}
}
}
}
/**
* Get the inline message for a rule if it exists.
*
* @param string $attribute
* @param string $lowerRule
* @param array $source
* @return string|null
*/
protected function getInlineMessage($attribute, $lowerRule, $source = null)
{
$source = $source ?: $this->customMessages;
$keys = ["{$attribute}.{$lowerRule}", $lowerRule];
// First we will check for a custom message for an attribute specific rule
// message for the fields, then we will check for a general custom line
// that is not attribute specific. If we find either we'll return it.
foreach ($keys as $key) {
foreach (array_keys($source) as $sourceKey) {
if (Str::is($sourceKey, $key)) {
return $source[$sourceKey];
}
}
}
}
/**
* Get the custom error message from translator.
*
* @param string $customKey
* @return string
*/
protected function getCustomMessageFromTranslator($customKey)
{
$shortKey = str_replace('validation.custom.', '', $customKey);
$customMessages = Arr::dot(
(array) $this->translator->trans('validation.custom')
);
foreach ($customMessages as $key => $message) {
if ($key === $shortKey || (Str::contains($key, ['*']) && Str::is($key, $shortKey))) {
return $message;
}
}
return $customKey;
}
}
This custom validator has all the changes that were made on Laravel 5.2, you can check them in here
Now since we have a new CustomValidator class we have to find a way of using it, for that we have to extend the ValidatorServiceProvider and the Validator factory.
<?php
namespace App\Validator;
class CustomValidatorServiceProvider extends \Illuminate\Validation\ValidationServiceProvider
{
/**
* Register the validation factory.
*
* @return void
*/
protected function registerValidationFactory()
{
$this->app->singleton('validator', function ($app) {
$validator = new Factory($app['translator'], $app);
// The validation presence verifier is responsible for determining the existence
// of values in a given data collection, typically a relational database or
// other persistent data stores. And it is used to check for uniqueness.
if (isset($app['validation.presence'])) {
$validator->setPresenceVerifier($app['validation.presence']);
}
return $validator;
});
}
}
<?php
namespace App\Validator;
use App\Validator\CustomValidator as Validator;
class Factory extends \Illuminate\Validation\Factory
{
/**
* Resolve a new Validator instance.
*
* @param array $data
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return App\Test\CustomValidator
*/
protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
{
if (is_null($this->resolver)) {
return new Validator($this->translator, $data, $rules, $messages, $customAttributes);
}
return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes);
}
}
Now that we have extended our validation to support the nested syntax sku.*.id
We just have to swap the Validator to our CustomValidator, and the last step is changing the file config/app.php and inside the ServiceProviders array look for the ValidatorServiceProvider, just comment that line and add our extended service provider, like this:
....
// Illuminate\Validation\ValidationServiceProvider::class,
App\Validator\CustomValidatorServiceProvider::class,
....
The reason we are commenting it out is because whenever you update your Laravel 5.1 to 5.2 you just want to uncomment it, remove our CustomValidatorServiceProvider from the list and then you delete our app/Validator folder because we don't need it anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With