Laravel Require if is null in database - laravel

laravel 5.8
I want a rule of laravel validation for update that be like this:
only push error when the value of this field is null and this field in the database is null too
if the field is null but in the database this field has value that validation is ok
I tried this
'logo' => 'required_without:logo',

You may have to write a custom Rule or do some checks on the field to wrap around your validation definitions.
$rules= [...];
if($model->field === null){
$rules['logo' => 'required'];
}

Related

How store null in database using vue and laravel?

I have Vue project with Laravel API, and also I have a column named expired_date: date it is nullable
this is the response after I dd the data from the network console:
The problem is when I store the data I just found the expired_date store value 0000-00-00
My code of store:
$data = $request->except('image');
if (!$request->expired_date) {
$data['expired_date'] = null;
}
Post::create($data);
The issue is that an empty string '' is being saved instead of null, resulting in 0000-00-00 values saved to the field. In this case it's because the ConvertEmptyStringsToNull middleware included with the framework was mistakenly commented out and disabled, so the solution is to re-enable that middleware.
Other common causes are forgetting to make the field nullable in the database, or having an incorrect default value.
To explicitly set a field to null without using the ConvertEmptyStringsToNull middleware, it is possible to use a mutator similar to this inside of the model:
public function setExpiredDateAttribute($date) {
$this->attributes['expired_date'] = empty($date) ? null : Carbon::parse($date);
}

Validate if another field is empty laravel validation

I want to validate field if present and will fail if another field not present
How to do it ? I have create custom validation to get record based id on storage
I have field product_id and amount and the amount field validate between max and min based on product_id, if i present the amount = 100000 and not fill the product_id field the validating for amount field is passed and display error because i am not fill the product_id i want check before the custom validation execute will check if product_id empty or not..
I have read all validation method on laravel docs still didnt find the solution.
How to do it ?
Finally i found a solution...
on Form Request class i just add sometimes rule on validate instance so when the product_id field not present on amount field will not validate and when present it will validate using validation rules like this:
class StoreRequest extend FormRequest
{
protected function getValidatorInstance()
{
$validator = parent::getValidatorInstance();
$validator->sometimes('amount', 'required|numeric|max_amount:product_id', function ($input) {
return Arr::has($input, 'product_id');
});
return $validator;
}
}

Laravel unique validation must include appended text when validating

I am using the Laravel tenancy package in which there is a domains table in my database and each value in the domain column within this table is appended with .test.co.uk.
When the user enters the URL in the form, they are presented with an input element (shown above) in which they enter a URL/Domain but the .test.co.uk is already appended so the only thing they need to enter is the text that goes before that, e.g. they would enter johnsmith and in the domain column it would store johnsmith.test.co.uk. The problem I have is that I need the validation on this column to be unique but also include the .test.co.uk when performing the validation so that it looks at the value stored in the table because if a user enters johnsmith and there is currently a record in the domains table where the value is johnsmith.test.co.uk then the validation would pass but I need the validation to fail in this scenario. I am currently using a Request class which is extending the FormRequest class and have this:
public function rules()
{
return [
'url' => 'required|string|unique:domains,domain',
];
}
I have also tried a rule object but I don't think a rule object is the correct solution to this problem. Is there a convenient "Laravel" way of doing this?
In your Request class use prepareForValidation()
Docs: https://laravel.com/docs/7.x/validation#prepare-input-for-validation
protected function prepareForValidation()
{
$this->merge([
'url' => $this->url . '.test.co.uk',
]);
}

Laravel 5.2 model save method can set not null fields to empty string automatic?

My laravel version is 5.2. Today I did a test, in my posts table, there are some fields can not be null name title. I save a post like this:
$post = new App\Post();
$post->name = 'testBook';
$post->save();
I found a new record inserted into table, the title field is empty string. So laravel can automatic set not null fields to empty string if I didn't pass values to the attributes?

Default value for a model validate rule

I have a question regarding default values for model fields, this problem has been driving me crazy for a while.
To give you an example, I have a model with a status field that must be validated:
$validate = array (
...
'status' => array(
'list' => array(
'rule' => array('inList', array('0','1')),
'allowEmpty' => true
)
)
)
The data that is inserted in the database is not submitted from a form.
If the status field is missing, I want it to default to 1, otherwise it must be validated.
Is there a way to do this from the model without using custom validation rules? I know I can set the default value in the MySQL table, but I still want to validate it from the model, in case a different value is submitted.
EDIT
I looked at the Validation class code and the inList validation is just a stupid wrapper for an in_array check:
Cake.Utility.Validation.php
public static function inList($check, $list, $strict = true) {
return in_array($check, $list, $strict);
}
What do you know, there is also a third param that is not mentioned in the CakePHP Api docs ($strict), which defaults to true.
From the in_array docs:
If the third parameter strict is set to TRUE then the in_array()
function will also check the types of the needle in the haystack.
It still doesn't solve my problem, but now I know why array('inList', array('0','1')) was not validating empty fields even though allowEmpty was set to true.
Thank you.
You can use the beforeSave method to set a default value. Don't forget to return true or you will abort the save.

Resources