0

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.

1
  • show us dd($request->specificationkeys) and dd($request->specificationvalues) Commented Oct 24, 2018 at 6:08

3 Answers 3

1

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 
}
Sign up to request clarification or add additional context in comments.

Comments

1

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  
 } 

Comments

0

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);
    }
}

2 Comments

what if file array is passed to request how can i check for images in that
Here is example how check array with images from request @JagdishSharma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.