Reputation: 309
Code:
public function specificationsave(Request $request) {
if (!empty($request->product_id)) {
if (empty($request->specificationkeys) && empty($request->specificationvalues)) {
return response()->json(["message" => 'Empty form submitted'], 202);
} else {
// something code
}
} else {
return response()->json(["message" => 'something went wrong with product'], 403);
}
}
Form code:
<input type="text" name="specificationkeys[]"/>
<input type="text" name="specificationvalues[]"/>
Problem:
When I am putting empty condition on my specificationkeys
array it is not working, because my key has a empty array so I can i check that the key has array or not or as I want empty check without validator because fields are not mandatory.
Upvotes: 0
Views: 502
Reputation: 152
use simple count function for this.
if (count($request->specificationkeys) == 0 && count($request->specificationvalues) == 0){
//your logic for empty
}else{
//Your logic for not empty
}
Upvotes: 1
Reputation: 15095
for example product_id
is return as array
check condition like this
if($request->product_id && is_array($request->product_id) && count($request->product_id) > 0) {
//success validation
}else {
//failed validation
}
Upvotes: 1
Reputation: 5004
Try this:
public function specificationsave(Request $request) {
if (isset($request->product_id) && !empty($request->product_id)) {
if (is_array($request->specificationkeys) &&
count($request->specificationkeys) !== 0 &&
is_array($request->specificationvalues) &&
count($request->specificationvalues) !== 0) {
return response()->json(["message" => 'Empty form submitted'], 202);
} else {
// something code
}
} else {
return response()->json(["message" => 'something went wrong with product'], 403);
}
}
Upvotes: 0