Laravel 5.5 / Validator / Custom rules - laravel

(edited to clearer purpose)
I discovered Laravel some months ago and I followed some Laracast videos. Now, I'm stuck with a problem of custom Validator.
I've got normal and permanent rules (date | required | min / max) to verify my form request. This part works.
But I've got a custom validation which could be verified as a normal rule IF one of the request parameter - select1 - is set to 1 (for exemple).
I read a dozen of explanation but nothing clear enough.
So, let start with my code. Thank you for your indulgence...
My question is into the customTest function on the bottom.
Thank you,
1/ I made a new Request with
php artisan make:controller priceRequest.php
2/ I made some rules and changed some lines.
public function authorize()
{
return true;
}
// Permanent RULES
public function rules()
{
$rules = [
'field1' => 'required|min:1|max:15',
'field2' => 'required',
'date1' => 'required|date',
'select1' => 'required',
];
return $rules;
}
Then I integrated a new function to perform a custom verification
public function withValidator($validator) {
$validator->after(function ($validator) {
if (!$this->customTests($this->request->get('data'))){
$validator->errors()->add('custom', 'Something is wrong');
}
});
}
Finally, I wrote the customTests function and it is inside this one I'm stuck !!
public function customTests($data) {
if ($data['select1'] == 1) {
// HERE MY QUESTION
// I'd like to verify that $data['date2']
// is a date and is set. So, I'd like to
// add a rule to rules (has I made with
// 'date1' => 'required|date',
// and return TRUE if the rule match or
// FALSE
}
}

So, I found the solution to my problem.
I made a mistake and a confusion because I tried to add the new rule into the ->after() function.
So, the correct code is the following :
public function withValidator($validator)
{
$validator->sometimes('ticket.price', 'required|integer|min:0.1', function ($input) {
if ($any_condition_who_need_to_verify_new_rule) {
return true;
}
else {
# Everything is perfect, this rule cas be omitted
}
$validator->after(function ($validator) use ($ticket) {
/* New test more complicated which cannot be
tested with a rule */
if ($this_test_is_complex_and_fails) {
$validator->errors()->add('validateTicket', 'This is another problem');
return false;
}
});
return $validator;
}

Related

Laravel request remove fields before validation

I am trying to remove some fields before they are validated.
Trying to attempt this inside prepareForValidation()
use Illuminate\Foundation\Http\FormRequest;
class VideoRequest extends ApiRequest
{
// ..code..
protected function prepareForValidation()
{
$this->merge([
'public' => $this->toBoolean($this->public),
'notify' => $this->toBoolean($this->notify),
]);
$video_id = $this->route('video_id');
if($this->isMethod('put') && Video::salesStarted($video_id)){
Log::info('removing sales');
// attempt 1
$this->except(['sales_start', 'tickets', 'price']);
// attempt 2
$this->request->remove('sales_start');
// attempt 3
$this->offsetUnset('sales_start');
}
if($this->isMethod('put') && Video::streamStarted($video_id)){
Log::info('removing streams');
// attempt 1
$this->except(['stream_start', 'stream_count', 'archive']);
// attempt 2
$this->request->remove('sales_start');
// attempt 3
$this->offsetUnset('sales_start');
}
$thumb = $this->uploadThumbnail($video_id);
if($thumb !== null){
$this->merge($thumb);
}
}
// ..code..
}
I made sure the code was entering inside the if statement, however the fields are not being removed.
Running $this->request->remove() and $this->except() have no effect.
If I add safe() it throws Call to a member function safe() on null.
I also tried to use unset() but nothing seems to work.
The rules for the dates are like so:
'sales_start' => 'sometimes|required|date|after:now|before:stream_start',
'stream_start' => 'sometimes|required|date|after:now',
but the $request->validated() returns the errors although it shouldn't be validating the deleted fields.
"sales_start": [
"The sales start must be a date after now."
],
"stream_start": [
"The stream start must be a date after now."
]
Why are the fields not being deleted?
Edit
As requested I added some code.
This is what ApiRequest looks like:
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
abstract class ApiRequest extends FormRequest
{
protected function failedValidation(Validator $validator): void
{
$response['data'] = [];
$response['api_status'] = 'ng';
$response['status_message'] = 'failed validation';
$response['errors'] = $validator->errors()->toArray();
throw new HttpResponseException(
response()->json( $response, 422 )
);
}
protected function toBoolean($booleable)
{
return filter_var($booleable, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
}
And the request is called from the controller like so:
public function update(VideoRequest $request, $video_id)
{
... some code ...
$validated = $request->validated();
... some code ...
}
so $this refers to the VideoRequest that extends FormRequest.
Can't find anything about deleting. But acording to Laravel docs you pick what keys you need from a request as follows:
$request->only(['username', 'password']);
// plug everything you need into the array.
$request->except(['username', 'password']);
//plug everything you don't need into the array
The latter is probably most useful to you.
Example:
Say I have the following keys: ['username', 'password', 'randomVal'];
$request->only(['username', 'password']);
// Output:
['username', 'password']
$request->except(['username', 'password']);
// Output:
['randomVal']
To remove (unset) a key from a Request before it goes to the Controller you can use offsetUnset()
inside your request:
protected function prepareForValidation()
{
$this->offsetUnset('sales_start');//same goes for the other key to remove...
}
This is a bit of an ugly answer.
Instead of modifying the request before the validation, I tried adding exclude when getting rules().
So something along these lines:
public function rules() {
$ex = $this->isMethod('put') && Video::salesStarted($video_id) ? 'exclude|' : '';
return [
'sales_start' => $ex.'sometimes|required|other_stuff',
];
}
Note that the validation 'exclude' only works if added first.
So this won't work:
'sometimes|required|other_stuff|exclude' //exclude is called last
I am still unable to find out why remove(), exclude(), offsetUnset() were not working, so this is not the right answer, but I hope it helps if someone is having the same issue.
Edit
Setting this as correct answer as I was unable to find an alternative solution/fix.

Yii2 custom validation rule not working

I'm trying to write some custom validation for a model in yii2, but I can't figure out why the validation always is positive, although my validation function always adds an error.
for example the rules
public function rules()
{
return [
...
[['myattribute'], 'myvalidation'],
];
}
public function myvalidation($attribute, $params)
{
$this->addError($attribute, "error");
}
the validate() function still returns true. What's the problem?
In validation rule add skipOnError => false
[
['myattribute', 'myvalidation', 'skipOnError' => false],
]
Then
$model->validate(); // should return false;
What I will recommend you is watch this lesson on custom roles and
read docs on it. It's not the best video tutorial, but this guy made it work correctly.
In your case :
1.In [['myattribute'], 'myvalidation'], remove extra array from here, make your model rules and custom valdation function look like this
public function rules()
{
return [
...
['myattribute', 'myvalidation'],
];
}
public function myvalidation($attribute, $params)
{
$this->addError($attribute, "error");
}
it's not wrong, but it's used when you have more than one attribute.
2.Then go to your form and put `` to your ActiveForm so it will look like this
<?php $form = ActiveForm::begin(['enableAjaxValidation' => true]); ?>
I mean, your need to do it in case if you want to validate it without reloading (using ajax).
3.Also add this to your controller's action that is connected to your view
if(Yii::$app->request->isAjax && $model->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\widgets\ActiveForm::validate($model);
}
This will validate your input in the form via ajax, if it's not ajax, this will not work.
['reg_date','validateDate', 'skipOnEmpty' => false, 'skipOnError' => false]
add this to your rules
public function validateDate($attribute,$params,$validator)
{
$validator->addError($this,$attribute,"Bugungi sana bo'lishi kerak!");
}
check this in your controller side like this
if($model->validate()){
echo "validate works";
}

Laravel validation with custom condition

I'm trying to setup validation rule with condition but have no idea how to do following:
In my form I have title_url (array for multiple language versions). I want to have unique title_url but only when module_cat_id in the form has same value as existing rows in DB.
This is my rule:
'title_url.*' => 'required|min:3|unique:modules_cat_lang,title_url'
Any ideas how to solve this?
You can define your custom similar to code below:
\Validator::extend('custom_validator', function ($attribute, $value, $parameters) {
foreach ($value as $v) {
$query = \DB::table('modules_cat_lang') // use model if you have
->where('title_url', $v)
->where('module_cat_id', \Input::get('module_cat_id'));
if ($query->exists()) {
return false;
}
}
});
'title_url.*' => 'required|min:3|custom_validator'
Read more here https://laravel.com/docs/5.3/validation#custom-validation-rules .
If you want to add your own custom validation logic to the existing laravel validator then You can use after hooks. Please have a look at the below examples.
Reference : https://laravel.com/docs/8.x/validation#adding-after-hooks-to-form-requests
Example 1(Without Parameter)
$validator->after(function ($validator) {
if ('your condition') {
$validator->errors()->add('field', 'Something went wrong!');
}
});
Example 2(With Parameter) // Here you can pass a custom parameter($input)
$validator->after(function ($validator) use ($input) {
if ($input) {
$validator->errors()->add('field', 'Something went wrong!');
}
});

yii2 custom validation not working

I need to compare 2 attribute value in the model and only if first value is lower than second value form can validate.I try with below code but it not worked.
controller
public function actionOpanningBalance(){
$model = new Bill();
if ($model->load(Yii::$app->request->post())) {
$model->created_at = \Yii::$app->user->identity->id;
$model->save();
}else{
return $this->render('OpanningBalance', [
'model' => $model,
]);
}
}
Model
public function rules()
{
return [
[['outlet_id', 'sr_id', 'bill_number', 'bill_date', 'created_at', 'created_date','bill_amount','credit_amount'], 'required'],
[['outlet_id', 'sr_id', 'created_at', 'updated_at'], 'integer'],
[['bill_date', 'd_slip_date', 'cheque_date', 'created_date', 'updated_date','status'], 'safe'],
[['bill_amount', 'cash_amount', 'cheque_amount', 'credit_amount'], 'number'],
[['comment'], 'string'],
['credit_amount',function compareValue($attribute,$param){
if($this->$attribute > $this->bill_amount){
$this->addError($attribute, 'Credit amount should less than Bill amount');
}],
[['bill_number', 'd_slip_no', 'bank', 'branch'], 'string', 'max' => 225],
[['cheque_number'], 'string', 'max' => 100],
[['bill_number'], 'unique']
];
}
}
It's going in to the validator function but not add the error like i wanted
$this->addError($attribute, 'Credit amount should less than Bill amount');
anyone can help me with this?
If the validation is not adding any error, it's most likely being skipped. The issue is most likely becasue of default rules behaviour whereby it skips empty or already error given values as per here: https://www.yiiframework.com/doc/guide/2.0/en/input-validation#inline-validators
Specifically:
By default, inline validators will not be applied if their associated attributes receive empty inputs or if they have already failed some validation rules. If you want to make sure a rule is always applied, you may configure the skipOnEmpty and/or skipOnError properties to be false in the rule declarations.
So you would need to set up the skipOnEmpty or skipOnError values depending on what works for you:
[
['country', 'validateCountry', 'skipOnEmpty' => false, 'skipOnError' => false],
]
Try this:
public function actionOpanningBalance(){
$model = new Bill();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->created_at = \Yii::$app->user->identity->id;
$model->save();
}else{
return $this->render('OpanningBalance', [
'model' => $model,
]);
}
}
For Validation
You can use anonymous function :
['credit_amount',function ($attribute, $params) {
if ($this->$attribute > $this->bill_amount)) {
$this->addError($attribute, 'Credit amount should less than Bill amount.');
return false;
}
}],
you can use like this below answer is also write
public function rules(){
return [
['credit_amount','custom_function_validation', 'on' =>'scenario'];
}
public function custom_function_validation($attribute){
// add custom validation
if ($this->$attribute < $this->cash_amount)
$this->addError($attribute,'Credit amount should less than Bill amount.');
}
I've made custom_function_validation working using 3rd params like this:
public function is18yo($attribute, $params, $validator)
{
$dobDate = new DateTime($this->$attribute);
$now = new DateTime();
if ($now->diff($dobDate)->y < 18) {
$validator->addError($this, $attribute, 'At least 18 years old');
return false;
}
}
This is a back end validation and it will trigger on submit only. So you can try something like this inside your validation function.
if (!$this->hasErrors()) {
// Your validation code goes here.
}
If you check the basic Yii2 app generated you can see that example in file models/LoginForm.php, there is a function named validatePassword.
Validation will trigger only after submitting the form.

minmum length validation rule depending on another attribute not working in laravel

class AddRoomRequest extends Request {
public function rules()
{
return [
'name' => 'required|min:2|max:1000',
'base_occupancy' => 'required|integer|min:0|max:100',
'max_occupancy' => 'required|integer|min:0|max:100'
];
}
}
Above code shows my request class this validation rule is not working, even if i give base occupancy as 2 and max occupancy as 10, this will still show error.
bothvalues are int(11) in database.
You need to add a custom validation rule by extending the validation class. I can't test this right now, but try this:
Validator::extend('greater_than', function($attribute, $value, $parameters) {
if (isset($parameters[0])) {
return intval($value) > intval($parameter[0]);
} else {
return false;
}
}
Your max_occupancy rule would then be:
'max_occupancy' => 'required|integer|max:100|greater_than:base_occupancy'

Resources