hi i have project on laravel 5.6
and this is my role for validation
'voucher_debt' => 'required|array|min:1',
'voucher_debt.*' => 'nullable|numeric|min:0.001',
'voucher_credit' => 'required|array|min:1',
'voucher_credit.*' => 'nullable|numeric|min:0.001',
my problem thats i need it to check if the
array_sum($voucher_credit) - array_sum($voucher_debt) == 0
i tried many thing nothing works out with me
is that possible on laravel
You may do this in your request:
public function rules()
{
$rules = [
'voucher_debt' => ['required', 'array', 'min:1'],
'voucher_debt.*' => ['nullable', 'numeric', 'min:0.001'],
'voucher_credit' => ['required', 'array', 'min:1'],
'voucher_credit.*' => ['nullable', 'numeric' ,'min:0.001'],
];
if (array_sum($this->get('voucher_debt')) - array_sum($this->get('voucher_debt')) == 0) {
throw new ConflictHttpException('YOUR_MESSAGE');
}
}
Or you can make your custom validation rule and add to voucher_debt key
https://laravel.com/docs/5.6/validation#custom-validation-rules
Related
I have a form on my .blade that has a filed "type_id" and it must a number between 0 and 4
is it possible to create a multiple size? for example 'size:0 || size:1 || size:2 || size:3 || size 4' ???
that's my function store: (but the most important part is the type_id field)
public function store(Request $request)
{
$toCreate = $request->validate([
'type_id' => ['required','integer','size:4'],
'name' => ['required', 'string', 'max:255'],
'partitaIva' => ['max:255'],
'codiceFiscale' => ['max:255'],
'sedeAmministrativa' => ['max:255'],
'indirizzoNotifica' => ['max:255'],
'referente' => ['max:255'],
'responsabile' => ['max:255'],
'telefono' => ['max:255'],
'fax' => ['max:255'],
'email' => ['max:255'],
'pec' => ['max:255'],
'capitale' => ['max:255'],
'nDipendenti' => ['max:255'],
'convenzioniDeleghe' => [],
'note' => []
]);
Administration::create($toCreate);
return redirect()->route('administrations.index')->with('success','Amministratore aggiunto con successo.');
}
You can use "between" validator. eg.
'type_id' => ['required', 'integer', 'between:1,4']
or
'type_id' => 'required|integer|between:1,4',
size is used to validate the length of array, string or a file.
For numbers "between", two approches:
Like #ericmp says, you have the "between" rule in form validation :
('type_id' => ['required', 'integer', 'size:4', 'between:0,4'])
But if you needs more rules, or custom rules in one, you can create a custom validation rule file:
php artisan make:rule MyRule
There is an example on this Laracast topic: Best way to validate range in Laravel?
There is laravel's documentation about Validation rules: Validation: Laravel Doc
I have the following request validation rules in an controller.
public function __invoke(Request $request)
{
$this->validate($request, [
'name' => ['required', 'unique:games,name', 'string'],
'variant' => ['required', 'string'],
'rules' => ['string'],
'is_anonymous' => ['boolean', 'required'],
'message_mode' => ['required', new EnumRule(MessageModeEnum::class)],
'powers' => ['required', 'array'],
'powers.*' => ['distinct:ignore_case', 'required', 'string'],
]);
}
Below is the test I'm running against the controller. I would expect the powers.* validation to throw an error as the two powers are equal and not distinct.
/**
* #test
*
*/
public function endpoint_validation()
{
$user = User::factory()->create(['rank' => RankEnum::A()]);
$response = $this->actingAs($user)->post(route('game.create'), [
'name' => '::name::',
'variant' => '::variant::',
'rules' => '::rules::',
'is_anonymous' => false,
'message_mode' => 'none',
'powers' => [
'::power::', '::power::'
],);
$response->assertSessionHasErrors('powers');
}
However, this test is failing. What am I doing wrong? How can I fix this?
I'm trying to validate a unique entry in my laravel app
following is my validation array,
$website = $websiteModel->find($id);
$this->validate($request, [
'subDomainName' => ['required','regex:/^[A-Za-z0-9 ]+$/'],
'subDomainSuffix' => ['required'],
'packageType' => ['required'],
'themeid' => ['required'],
'lang' => ['required'],
'user' => ['required'],
'domain' => [
'required',
'string',
'min:2',
'max:255',
Rule::unique('apps')->ignore($website)
],
], $request->all());
My validation working properly BUT,
When i tried to enter a duplicate value for my domain field, It get validated properly but not showing the error message, saying sorry the name is already exists...
<input type="text" id="domain" class="form-control" name="domain" >
{!! $errors->first('domain', '<span class="help-block" role="alert">:message</span>') !!}
Here in this span it shows nothing but in the common error message area it shows sorry the form cannot be updated... So how can I validate the field properly and display the relevant error message
Do something like this:
On insert request use
'domain' => [
...
'unique:websites,domain'
]
On update request use
'domain' => [
...
"unique:websites,domain,{$this->website->id}"
]
Or
'domain' => [
...
Rule::unique('websites', 'domain')->ignore($this->website)
]
You passed $request->all() as validation messages.
Please Try:
$website = $websiteModel->find($id);
$request->validate([
'subDomainName' => ['required','regex:/^[A-Za-z0-9 ]+$/'],
'subDomainSuffix' => ['required'],
'packageType' => ['required'],
'themeid' => ['required'],
'lang' => ['required'],
'user' => ['required'],
'domain' => [
'required',
'string',
'min:2',
'max:255',
Rule::unique('apps')->ignore($website)
],
]);
don't you need to pass duplicate column in ignore Rule To instruct the validator to ignore the website domain, except for it self ? for example like
Rule::unique('apps')->ignore($website->id)
please try this one . it helps to solve your problem
use exception and validator in top of the file
use Exception;
use Validator;
$rules = [
'subDomainName' => 'required|unique:sub_domain_name',
];
$validator = Validator::make($request->all(), $rules, $message);
if ($validator->fails()) {
throw new Exception(implode('\n', $validator->errors()->all()));
}
sub_domain_name : this is database column name
I have inputs, some of them are array inputs (e.g. <input name="test[]">) and some are normal ones.
Fields have some specific rules, but all of them share 2 rules together.
I want that same validation rule to be applied to all fields instead of copying the same thing again and again for like 10+ fields.
I tried:
$rules = [
'*.*' => ['max:100', new CustomRule], // Apply to all
'name1' => ['array'],
'name1.*' => ['specific_rule'],
]
but it didn't work, then I moved that *.* to bottom like this:
$rules = [
'name1' => ['array'],
'name1.*' => ['specific_rule'],
'*.*' => ['max:100', new CustomRule], // Apply to all
]
and then suddenly it works!
A little confused about why it doesn't work on top, and I'm not sure if this is the right way to do this.
Please advise.
Edit:
The whole thing (removed some of the rules to simplify):
$data = json_decode($request->getContent(), true);
$rules = [
'contact' => ['required', 'array'],
'contact.name' => ['required', 'string', 'max:255'],
'contact.email' => ['required', 'email', 'unique:users,email', 'max:255'],
'subscription' => ['required', 'array'],
'subscription.type' => ['nullable', 'integer'],
'*.*' => [new CustomRule], // Works when it's here, doesn't work on top, this is a Regex rule, looking for some invalid characters in all inputs.
];
$validator = Validator::make($data, $rules);
if($validator->fails())
{
return response()->json($validator->messages(), 400);
}
I have a set of validation rules in laravel. there are two date fields that one of them have to be greater than other. but when I use gt operator, an error apears:
$validation = Validator::make($request->all(), [
'description' => 'required|string',
'started_at' => 'required|date',
'finished_at' => 'required|date|gt:started_at',
]);
error is: Method Illuminate\Validation\Validator::validateGt does not exist.
gt:started_at is wrong gt rule does not exist in laravel use after
$validation = Validator::make($request->all(), [
'description' => 'required|string',
'started_at' => 'required|date',
'finished_at' => 'required|date|after:started_at',
]);