Laravel request validation image required in create but not required in update












0















ProductsRequest.php code:




public function rules()
{
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this -> product_id,


'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this -> product_id,

'category_id' => 'required
|exists:categories,id',

'seasons_id' => 'required
|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'required
|image|mimes:'.trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),

'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}


These rules apply for both store and update methods.




Problem is:




I want the image to be required only on store and not required in update, since user can just update the product basic info without choosing a new image for the product every time he update the product.




What I have tried:




I have tried to create two different ProductsRequest one for store and other for update but I know that this achievement is not the best achievement because my code must be DRY.










share|improve this question






















  • Do you require both category_id and seasons_id when creating the resource as well?
    – Peter Sowah
    Nov 23 '18 at 17:58










  • @PeterSowah yes because both of them may be updated and both of them is required
    – Ahmed essam
    Nov 23 '18 at 18:24
















0















ProductsRequest.php code:




public function rules()
{
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this -> product_id,


'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this -> product_id,

'category_id' => 'required
|exists:categories,id',

'seasons_id' => 'required
|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'required
|image|mimes:'.trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),

'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}


These rules apply for both store and update methods.




Problem is:




I want the image to be required only on store and not required in update, since user can just update the product basic info without choosing a new image for the product every time he update the product.




What I have tried:




I have tried to create two different ProductsRequest one for store and other for update but I know that this achievement is not the best achievement because my code must be DRY.










share|improve this question






















  • Do you require both category_id and seasons_id when creating the resource as well?
    – Peter Sowah
    Nov 23 '18 at 17:58










  • @PeterSowah yes because both of them may be updated and both of them is required
    – Ahmed essam
    Nov 23 '18 at 18:24














0












0








0








ProductsRequest.php code:




public function rules()
{
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this -> product_id,


'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this -> product_id,

'category_id' => 'required
|exists:categories,id',

'seasons_id' => 'required
|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'required
|image|mimes:'.trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),

'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}


These rules apply for both store and update methods.




Problem is:




I want the image to be required only on store and not required in update, since user can just update the product basic info without choosing a new image for the product every time he update the product.




What I have tried:




I have tried to create two different ProductsRequest one for store and other for update but I know that this achievement is not the best achievement because my code must be DRY.










share|improve this question














ProductsRequest.php code:




public function rules()
{
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this -> product_id,


'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this -> product_id,

'category_id' => 'required
|exists:categories,id',

'seasons_id' => 'required
|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'required
|image|mimes:'.trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),

'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}


These rules apply for both store and update methods.




Problem is:




I want the image to be required only on store and not required in update, since user can just update the product basic info without choosing a new image for the product every time he update the product.




What I have tried:




I have tried to create two different ProductsRequest one for store and other for update but I know that this achievement is not the best achievement because my code must be DRY.







php laravel laravel-validation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 17:45









Ahmed essam

315




315












  • Do you require both category_id and seasons_id when creating the resource as well?
    – Peter Sowah
    Nov 23 '18 at 17:58










  • @PeterSowah yes because both of them may be updated and both of them is required
    – Ahmed essam
    Nov 23 '18 at 18:24


















  • Do you require both category_id and seasons_id when creating the resource as well?
    – Peter Sowah
    Nov 23 '18 at 17:58










  • @PeterSowah yes because both of them may be updated and both of them is required
    – Ahmed essam
    Nov 23 '18 at 18:24
















Do you require both category_id and seasons_id when creating the resource as well?
– Peter Sowah
Nov 23 '18 at 17:58




Do you require both category_id and seasons_id when creating the resource as well?
– Peter Sowah
Nov 23 '18 at 17:58












@PeterSowah yes because both of them may be updated and both of them is required
– Ahmed essam
Nov 23 '18 at 18:24




@PeterSowah yes because both of them may be updated and both of them is required
– Ahmed essam
Nov 23 '18 at 18:24












2 Answers
2






active

oldest

votes


















2














You can do this in your ProductsRequest file;



public function rules()
{
if($request()->isMethod('put')) // could be patch as well
{
// Update rules here - Don't require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'required|image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}else{
// store rules here - require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}
}

}





share|improve this answer























  • I will try this solution
    – Ahmed essam
    Nov 23 '18 at 18:32










  • I don't have the $request variable in the ProductRequest.php file..
    – Ahmed essam
    Nov 23 '18 at 19:14



















1














Use required_without rules



If primary key and element with name is id exist in your array



'image'         => 'required_without:id`


If primary key and element with name is product_id exist in your array



 'image'         => 'required_without:product_id`


You can get more detail from laravel validation






share|improve this answer























  • What is the id that you wrote in the first line of code? I understand the product_id that I am going to send with the patch request
    – Ahmed essam
    Nov 23 '18 at 18:32










  • Ok then just use what you have in array.
    – C2486
    Nov 23 '18 at 18:34










  • I have product_id in the update method so how I add that with required_with?
    – Ahmed essam
    Nov 23 '18 at 18:37










  • Sorry its my bad you need to use 'required_without'
    – C2486
    Nov 23 '18 at 18:53










  • The problem is that I am sending the product_id in the URL and I don't have anything to differentiate between both requests except POST and PATCH request..
    – Ahmed essam
    Nov 23 '18 at 19:13











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451027%2flaravel-request-validation-image-required-in-create-but-not-required-in-update%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can do this in your ProductsRequest file;



public function rules()
{
if($request()->isMethod('put')) // could be patch as well
{
// Update rules here - Don't require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'required|image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}else{
// store rules here - require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}
}

}





share|improve this answer























  • I will try this solution
    – Ahmed essam
    Nov 23 '18 at 18:32










  • I don't have the $request variable in the ProductRequest.php file..
    – Ahmed essam
    Nov 23 '18 at 19:14
















2














You can do this in your ProductsRequest file;



public function rules()
{
if($request()->isMethod('put')) // could be patch as well
{
// Update rules here - Don't require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'required|image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}else{
// store rules here - require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}
}

}





share|improve this answer























  • I will try this solution
    – Ahmed essam
    Nov 23 '18 at 18:32










  • I don't have the $request variable in the ProductRequest.php file..
    – Ahmed essam
    Nov 23 '18 at 19:14














2












2








2






You can do this in your ProductsRequest file;



public function rules()
{
if($request()->isMethod('put')) // could be patch as well
{
// Update rules here - Don't require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'required|image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}else{
// store rules here - require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}
}

}





share|improve this answer














You can do this in your ProductsRequest file;



public function rules()
{
if($request()->isMethod('put')) // could be patch as well
{
// Update rules here - Don't require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'required|image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}else{
// store rules here - require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,

'category_id' => 'required|exists:categories,id',

'seasons_id' => 'required|exists:seasons,id',

// REQUIRED IMAGE ONLY IN STORE
'image' => 'image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}
}

}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 21:41

























answered Nov 23 '18 at 17:55









Peter Sowah

427110




427110












  • I will try this solution
    – Ahmed essam
    Nov 23 '18 at 18:32










  • I don't have the $request variable in the ProductRequest.php file..
    – Ahmed essam
    Nov 23 '18 at 19:14


















  • I will try this solution
    – Ahmed essam
    Nov 23 '18 at 18:32










  • I don't have the $request variable in the ProductRequest.php file..
    – Ahmed essam
    Nov 23 '18 at 19:14
















I will try this solution
– Ahmed essam
Nov 23 '18 at 18:32




I will try this solution
– Ahmed essam
Nov 23 '18 at 18:32












I don't have the $request variable in the ProductRequest.php file..
– Ahmed essam
Nov 23 '18 at 19:14




I don't have the $request variable in the ProductRequest.php file..
– Ahmed essam
Nov 23 '18 at 19:14













1














Use required_without rules



If primary key and element with name is id exist in your array



'image'         => 'required_without:id`


If primary key and element with name is product_id exist in your array



 'image'         => 'required_without:product_id`


You can get more detail from laravel validation






share|improve this answer























  • What is the id that you wrote in the first line of code? I understand the product_id that I am going to send with the patch request
    – Ahmed essam
    Nov 23 '18 at 18:32










  • Ok then just use what you have in array.
    – C2486
    Nov 23 '18 at 18:34










  • I have product_id in the update method so how I add that with required_with?
    – Ahmed essam
    Nov 23 '18 at 18:37










  • Sorry its my bad you need to use 'required_without'
    – C2486
    Nov 23 '18 at 18:53










  • The problem is that I am sending the product_id in the URL and I don't have anything to differentiate between both requests except POST and PATCH request..
    – Ahmed essam
    Nov 23 '18 at 19:13
















1














Use required_without rules



If primary key and element with name is id exist in your array



'image'         => 'required_without:id`


If primary key and element with name is product_id exist in your array



 'image'         => 'required_without:product_id`


You can get more detail from laravel validation






share|improve this answer























  • What is the id that you wrote in the first line of code? I understand the product_id that I am going to send with the patch request
    – Ahmed essam
    Nov 23 '18 at 18:32










  • Ok then just use what you have in array.
    – C2486
    Nov 23 '18 at 18:34










  • I have product_id in the update method so how I add that with required_with?
    – Ahmed essam
    Nov 23 '18 at 18:37










  • Sorry its my bad you need to use 'required_without'
    – C2486
    Nov 23 '18 at 18:53










  • The problem is that I am sending the product_id in the URL and I don't have anything to differentiate between both requests except POST and PATCH request..
    – Ahmed essam
    Nov 23 '18 at 19:13














1












1








1






Use required_without rules



If primary key and element with name is id exist in your array



'image'         => 'required_without:id`


If primary key and element with name is product_id exist in your array



 'image'         => 'required_without:product_id`


You can get more detail from laravel validation






share|improve this answer














Use required_without rules



If primary key and element with name is id exist in your array



'image'         => 'required_without:id`


If primary key and element with name is product_id exist in your array



 'image'         => 'required_without:product_id`


You can get more detail from laravel validation







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 18:52

























answered Nov 23 '18 at 17:53









C2486

19k32666




19k32666












  • What is the id that you wrote in the first line of code? I understand the product_id that I am going to send with the patch request
    – Ahmed essam
    Nov 23 '18 at 18:32










  • Ok then just use what you have in array.
    – C2486
    Nov 23 '18 at 18:34










  • I have product_id in the update method so how I add that with required_with?
    – Ahmed essam
    Nov 23 '18 at 18:37










  • Sorry its my bad you need to use 'required_without'
    – C2486
    Nov 23 '18 at 18:53










  • The problem is that I am sending the product_id in the URL and I don't have anything to differentiate between both requests except POST and PATCH request..
    – Ahmed essam
    Nov 23 '18 at 19:13


















  • What is the id that you wrote in the first line of code? I understand the product_id that I am going to send with the patch request
    – Ahmed essam
    Nov 23 '18 at 18:32










  • Ok then just use what you have in array.
    – C2486
    Nov 23 '18 at 18:34










  • I have product_id in the update method so how I add that with required_with?
    – Ahmed essam
    Nov 23 '18 at 18:37










  • Sorry its my bad you need to use 'required_without'
    – C2486
    Nov 23 '18 at 18:53










  • The problem is that I am sending the product_id in the URL and I don't have anything to differentiate between both requests except POST and PATCH request..
    – Ahmed essam
    Nov 23 '18 at 19:13
















What is the id that you wrote in the first line of code? I understand the product_id that I am going to send with the patch request
– Ahmed essam
Nov 23 '18 at 18:32




What is the id that you wrote in the first line of code? I understand the product_id that I am going to send with the patch request
– Ahmed essam
Nov 23 '18 at 18:32












Ok then just use what you have in array.
– C2486
Nov 23 '18 at 18:34




Ok then just use what you have in array.
– C2486
Nov 23 '18 at 18:34












I have product_id in the update method so how I add that with required_with?
– Ahmed essam
Nov 23 '18 at 18:37




I have product_id in the update method so how I add that with required_with?
– Ahmed essam
Nov 23 '18 at 18:37












Sorry its my bad you need to use 'required_without'
– C2486
Nov 23 '18 at 18:53




Sorry its my bad you need to use 'required_without'
– C2486
Nov 23 '18 at 18:53












The problem is that I am sending the product_id in the URL and I don't have anything to differentiate between both requests except POST and PATCH request..
– Ahmed essam
Nov 23 '18 at 19:13




The problem is that I am sending the product_id in the URL and I don't have anything to differentiate between both requests except POST and PATCH request..
– Ahmed essam
Nov 23 '18 at 19:13


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451027%2flaravel-request-validation-image-required-in-create-but-not-required-in-update%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista