Validate when passing individual data using JSON.stringify - laravel

Before passing the data to Controller, the data is being added :
formData.push({"name":"channels","value":JSON.stringify(channels)});
Cause of this even when no data is present, its passed like
'channels' => '[]'
Now the issue is when I try to validate this in validator, I cannot use
'channels' =>'required',
'channels.*' =>'required|exists:channels,id',
How do validate the above data? Don't want to convert the format as its a working system. Any suggestions are appreciated. Thanks.
Updated for Request All Params:
'_token' => 'DjqgmNab0o3ifrVrSvHh6dM5vxLP7tZDc47pq05r',
'startdate' => '05 Sep 2018',
'years' => NULL,
'months' => NULL,
'enddate' => NULL,
'addChannel' => NULL,
'offerRuns' => 'UL',
'numberOfRuns' => NULL,
'limitPeriod' => 'FP',
'licenseAudioTrack' => '1',
'amount' => NULL,
'include_materials_costs' => '1',
'include_withholding_taxes' => '1',
'paymentTermsType' => 'US',
'termsAndConditionDescription' => NULL,
'document_s3_url' => NULL,
'file' => NULL,
'fileSize' => NULL,
'materialSpecificationDescription' => NULL,
'note' => NULL,
'countries' => '[]',
'platforms' => '["1","2","3","4","5","6","7","8","9"]',
'platforms-exclusive' => '[]',
'platforms-non-exclusive' => '[]',
'platforms-holdback' => '[]',
'channels' => '[]',
'languages' => '[["56","AL",1,"seller"]]',
'currencySelectedTerm' => 'EP',
'currencyId' => '1',
'paymentTerms' => '[]'

Check the present validation rule. It states:
present
The field under validation must be present in the input data but can
be empty.
Also look into sometimes rule:
In some situations, you may wish to run validation checks against a
field only if that field is present in the input array. To quickly
accomplish this, add the sometimes rule to your rule list
https://laravel.com/docs/5.7/validation#conditionally-adding-rules

As I understood channels is passed as JSON string & required validator is not working because it is not an empty string.
You can create a custom validator to validate empty JSON string & use it.
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class JsonRequired implements Rule
{
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
return ! empty(json_decode($value, true));
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return 'The :attribute is required.';
}
}
And use it as 'channels' =>'new JsonRequired'.
If you only need once throughout your application, you may use a Closure instead of a rule object.
Laravel custom validation

You can use json_decode for data first and then apply validations
public function store(Request $request)
{
$request_data = $request->all();
foreach($request_data as $key=>$value)
{
$request_data[$key] = json_decode($value);
}
// And then pass data in validator rules
$rules = [
// All rules here
];
$validator = Validator::make($request_data, $rules);
// other code
}

Related

Laravel FormRequest is modifying the input

I am having trouble using the Laravel Validator to validate some data. The validator is modifying properties of the input to null.
The data passed to be validated is a mix of array and objects (in this case, a model instance).
Just for clarification: I know how to use FormRequest in controllers, I am full aware that Laravel would inject the FormRequest in the methods, and FormRequest is primaly to be used to validate user data, etc, etc. The point is why the validator need to modify the data I sent to validation?
Here's an example that you can directly paste in a php artisan tinker session:
$rules = [
'users' => [
'required',
'array',
'min:1',
],
'users.*' => [
'required',
],
'users.*.name' => [
'required',
'string',
'max:255',
],
'users.*.age' => [
'required',
'integer',
],
'users.*.best_friend' => [
'required',
],
];
$data = [
'users' => [
(new \App\Models\User)->forceFill([
'name' => 'USER #1',
'age' => 30,
'best_friend' => (new \App\Models\User)->forceFill(['name' => 'User X'])
]),
],
];
echo 'BEFORE: ' . data_get($data, 'users.0.name'); // USER #1
$validator = Validator::make($data, $rules);
echo 'AFTER: ' . data_get($data, 'users.0.name'); // NULL
dd($data);
OK, the data PASSES. But the problem is that the validation modified the variable $data, setting null to the fields with these patterns: users.*.name, users.*.age and users.*.best_friend.
If I dare to validate any model attribute, it sets to null.
I debugged and I reached the source of the modification:
/vendor/laravel/framework/src/Illuminate/Validation/ValidationData.php:42:
/**
* Gather a copy of the attribute data filled with any missing attributes.
*
* #param string $attribute
* #param array $masterData
* #return array
*/
protected static function initializeAttributeOnData($attribute, $masterData)
{
$explicitPath = static::getLeadingExplicitAttributePath($attribute);
$data = static::extractDataFromPath($explicitPath, $masterData);
if (! str_contains($attribute, '*') || str_ends_with($attribute, '*')) {
return $data;
}
// here some debug info:
// $explicitPath="users"
// $attribute="users.*.name"
// $data=User
return data_set($data, $attribute, null, true);
}
I know data_set modifies by reference.
But I could not understand why the code modifies the data if there is data already there. Should not it check for data before setting to null?
The validator is making the validated properties of my model to be null. Why and how to fix?
Maybe a different approach? Maybe this could be considered an bug/improvement for the Illuminate lib?
Any help would be apreciated.
VERSIONS:
Laravel Framework 9.33.0
PHP 8.1.2
Laravel transforms the keys from your validation rules: name and age. But that didn't work as expected because users are objects. To solve that you need to call toArray() after forceFill
$data = [
'users' => [
(new \App\Models\User) -> forceFill([
'name' => 'USER #1',
'age' => 30,
'best_friend' => (new \App\Models\User) -> forceFill(['name' => 'User X'])
])->toArray(),
]
];
If you need validation for best_friend.name you need to call toArray() on that too. But without validation you will get the object as it is.

laravel endpoint hide field

How can i hide some fields ?
i want to hide the file field
Eloquent :
$reports = Report::select('id', 'file','company_id', 'title', 'year', 'created_at')
->orderBy('id', 'desc')
->paginate(10);
return ReportResource::collection($reports);
Model :
...
public function getFileSizeAttribute()
{
return Storage::disk('files')->size($this->attributes['file']);
}
....
ReportResource:
...
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'year' => $this->year,
'views' => $this->whenNotNull($this->views),
'file' => $this->whenNotNull($this->file), <-- i want to hide the file field
'file_size' => $this->fileSize, <-- but always show file_size
'created_at' => $this->created_at,
'company' => new CompanyResource($this->company),
];
}
to get file_size field i must select the file field , because it's depends on it to calculate the file size.
but i want to hide the file field before send the response.
i know i can use the protected $hidden = [] method in the model , but i don't want that, because file field it's required on others place. i just want to hide it on this endpoint only.
Since you are using API resources the best and clean way to do this is by using a Resource class for your collection.
Said that, you will have 3 Resources:
The first one, as it is, just for retrieving a single Model with file and file_size attributes. The one you already have ReportResource.php
...
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'year' => $this->year,
'views' => $this->whenNotNull($this->views),
'file' => $this->whenNotNull($this->file),
'file_size' => $this->fileSize,
'created_at' => $this->created_at,
'company' => new CompanyResource($this->company),
];
}
A new second resource to be used in your endpoint, without the file attribute. IE: ReportIndexResource.php
...
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'year' => $this->year,
'views' => $this->whenNotNull($this->views),
'file_size' => $this->fileSize,
'created_at' => $this->created_at,
'company' => new CompanyResource($this->company),
];
}
Now you need to create a Resource collection which explicitly defines the Model Resource to use. IE: ReportCollection.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class ReportCollection extends ResourceCollection
{
/**
* The resource that this resource collects.
*
* #var string
*/
public $collects = ReportIndexResource::class;
}
Finally, use this new resource collection in your endpoint
$reports = Report::select('id', 'file','company_id', 'title', 'year', 'created_at')
->orderBy('id', 'desc')
->paginate(10);
return new ReportCollection($reports);
Of course, you can make use of makeHidden() method, but IMO is better to write a little more code and avoid a non desired attribute in your response because you forgot to make it hidden.
Also, in case you make use of makeHidden() method and you want to show the attribute in a future, you will have to update all your queries instead of a silgle resource file.
If you want to make it Hide From All Returns , you can Do this in model
protected $hidden = ['file'];
and if you want to do it temporirly with this query , you can Use MakeHidden method
$users = $reports->makeHidden(['file']);
It's clear in laravel docs , take a look
https://laravel.com/docs/9.x/eloquent-collections#method-makeHidden

Validating not all fields in form - Laravel

In laravel, I have created a form. At the moment, I am working on the validation of the input fields of this form. I ran into a problem when I tried to validate some input fields and others not. For example, mail should be validated but catering_name not (it isn't necessary to fill in this field, its an option)
I have tried all validation methods I could find. I keep getting the same error.
Method Illuminate\Validation\Validator::validatePhone does not exist.
I guess I am missing something.
I have tried:
Validator::make($request->...
$this->validate(request(), [ ...
$request->validate([ ...
Bellow, you will find all the data that should be inputted in the database.
If I remove the validation part, the data got inserted into the database. I think the problem lays with how I try to validate. Thanks for any help.
$this->validate(request(), [
'add_name' => 'required|min:3',
'add_mail' => 'required|email',
'name' => 'required|min:3',
'email' => 'required|email',
'telefone' => 'numeric|phone',
'gsm' => 'numeric|phone',
'event' => 'required|min:3',
'date_start' => 'required|date|after:tomorrow',
'date_end' => 'required|date|after_or_equal:event_date_start',
'location' => 'required|min:3',
'number' => 'required',
]);
$event = new Event;
$event->add_name = request('add_name');
$event->add_mail = request('add_mail');
$event->name = request('name');
$event->email = request('email');
$event->telefone = request('telefone');
$event->gsm = request('gsm');
$event->name = request('name');
$event->date_start = request('date_start');
$event->date_end = request('date_end');
$event->location = request('location');
$event->number = request('number');
$event->catering = request('catering');
$event->catering_name = request('catering_name');
$event->remarks = request('remarks');
$event->status = Event::STATUS_0;
$event->save();
Unfortunately phone is not one of the default validation. You can try something like:
[
'telefone' => 'required|regex:/(01)[0-9]{9}/',
]
You can see the available list of validations given by Laravel here.
There are a wide variety of more complex options depending on how important it is to you.
There are packages for easy plug and play like Laravel-Phone.
You can create your own custom validation using php artisan make:rule phone_number and then editing the new rule made:
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class PhoneNumber implements Rule
{
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
// logic here, most likely some sort of regex.
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return 'The :attribute must be a valid phone number.';
}
}

Laravel Validation unique/exists with different database connection

In the documentation, I saw you could set a connection for the unique rule which is great. However, the exists doesn't seem to follow the same logic. Take this for example:
$rules = [
'username' => 'required|max:40|unique:user',
'name' => 'sometimes|required',
'email' => 'required|email|max:255|unique:int.user',
'password' => 'sometimes|required|confirmed|min:6',
'password_current' => 'sometimes|required'
];
The unique rule works GREAT in this instance. It uses my database connection called 'int' and calls the user table. HOWEVER, when the rules are reversed like so:
$rules['email'] = 'required|email|max:255|exists:int.user';
I got this error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'int.user'
doesn't exist (SQL: select count(*) as aggregate from int.user where
email = haleybuggs6#gmail.com)
It's trying to call an int.user table instead of using the int database connection.
Is there a reason exists doesn't act the same way as unique? Thanks.
instead of using connection name you can try with straight Database name which is defined in "int" connection. faced similar problem and these way worked for me. like
$rules['email'] = 'required|email|max:255|exists:DB_Name.user';
You can use
'email' => 'exists:mysql2.users|required'
Where mysql2 is second database settings array in the database.php file
Try it.
$rules = [
'username' => 'required|max:40|unique:connection_name.user',
'name' => 'sometimes|required',
'email' => 'required|email|max:255|unique:connection_name.user',
'password' => 'sometimes|required|confirmed|min:6',
'password_current' => 'sometimes|required'
];
Ultimately for Laravel 5.6.* you need to look at an existing instance of the model you are trying to validate, or specify ...
{db_connection_name}.{schema_name}.{table_name}
... to ensure that you are looking at the proper table.
Validation Example
validate it...
<?php
// for instance...
// maybe auth user is in a different db
// = so you cannot validate with your default db connection
$default_user = Auth::user();
// pass the instance in order to allow Validator to qualify the proper connection/name
\App\Validation\User::validate($_POST, $default_user);
User Validation class
<?php
namespace App\Validation;
class User extends Validator
{
/**
* #param \Illuminate\Database\Eloquent\Model|string $mixed
* #param string $default
* #return string
*/
public static function table($mixed,$default='default_connection.app_schema.users_table')
{
if($mixed instanceof \Illuminate\Database\Eloquent\Model){
$table = $mixed->getConnectionName().'.'.$mixed->getTable();
} else {
if (! empty($mixed)) {
$table = $mixed;
} else {
$table = $default;
}
}
return $table;
}
/**
* validation to create a new user
*
* #param array $data
* #param \App\User|string $mixed
* #return array
* #throws \Illuminate\Validation\ValidationException
*/
public static function validate(array $data, $mixed='default_connection.app_schema.users_table'){
return Validator::validate($data,[
'username' => 'required|max:40|unique:'.self::table($mixed),
'name' => 'sometimes|required',
'email' => 'required|email|max:255|unique:'.self::table($mixed),
'password' => 'sometimes|required|confirmed|min:6',
'password_current' => 'sometimes|required'
]);
}
}
$default_connection = 'db_name';
$rules = [
'username' => 'required|max:40|unique:{$default_connection}.user',
'name' => 'sometimes|required',
'email' => 'required|email|max:255|unique:int.user',
'password' => 'sometimes|required|confirmed|min:6',
'password_current' => 'sometimes|required'
];

Override / remove server-side validation in Symfony2.5

Say I have the following form builder buildForm method:
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'travelTime',
'datetime',
array(
'label' => 'Time',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'invalid_message' => 'Please enter a valid date and time',
)
)
->add(
'acmeEntity',
'entity',
array(
'label' => 'Acme Entity:',
'class' => 'AcmeBundle:MyEntity',
'expanded' => false,
'multiple' => false,
)
)
}
How can I override (or remove) validation for the 'acmeEntity' form field (and only that field) so that if I call:
$form->handleRequest($request);
$form->isValid();
in a Controller, then acmeEntity will not be included in the validation that determines whether $form->isValid() returns true? I've tried adding constraints => false, to the field options, but I'm receiving this error message:
Notice: Trying to get property of non-object in /var/www/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php line 67
Does anyone know the proper way to disable server-side validation for a Symfony form field?
EDIT:
Note that I am not looking for how to disable validation completely. This can be done by adding:
// Disable form validation
$builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) {
$event->stopPropagation();
}, 900); // Always set a higher priority than ValidationListener
to the bottom of a form builder.
Rather, I want to know how to completely disable validation for a single form field. Thanks everyone and good hunting!
You can define a custom form type for your entity and use the 'validation_groups' => false. This should disable the validation only for this field.
The custom form type may look like that:
// .../Your/OwnBundle/Form/Type/AcmeEntityType.php
namespace Acme\DemoBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class AcmeEntityType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => false
));
}
public function getParent()
{
return 'entity';
}
public function getName()
{
return 'acme_entity';
}
}
You can then use:
$builder
->add(
'travelTime',
'datetime',
array(
'label' => 'Time',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'invalid_message' => 'Please enter a valid date and time',
)
)
->add(
'acmeEntity',
'acme_entity',
array(
'label' => 'Acme Entity:',
'class' => 'AcmeBundle:MyEntity',
'expanded' => false,
'multiple' => false,
)
)
}
I assume that you call buildForm() from MyEntityType extending AbstractType so just use the options resolver by adding the function setDefaultOptions() in your type as said in the symphony doc here
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => false,
));
}
You can try this, I have used it in the past :
Create a validation group for your MainEntity
Add this validation group to your form
Do not add this validation group to AcmeEntity

Resources