0

The validate() function from my sales controller seems not working, I am comparing it to my other controller, it looks like it should work but it is not. it looks like the validate() is being bypassed. here's my controller

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class SalesController extends Controller
{
    public function store(Request $request)
    {
        $this->validate($request, [
            'user_id' => 'required',
            'status_id' => 'required',
            'currency_id' => 'required',
            'company_id' => 'required',
            'invoice_no' => 'nullable',
            'notes' => 'nullable',
            'admin_notes' => 'nullable',
            'due_date' => 'nullable',
            'publish' => 'nullable',
            'product_id' => 'required|min:1',
            'product_code' => 'required|min:1',
            'product_name' => 'required|min:1',
            'quantity' => 'required'
        ]);


         $sales = $request->only(
            'user_id',
            'status_id',
            'currency_id',
            'currency_rate',
            'due_date',
            'company_id',
            'invoice_no',
            'notes',
            'admin_notes',
            'delivery_date',
            'publish'
        );
        $sales['grandtotal'] = (float) str_replace(',', '', $request->grandtotal);
        $sales['grandtotalcost'] = (float) str_replace(',', '', $request->grandtotalcost);
        $sales = Sales::create($sales);

        $input = $request->all();
        for($i=0; $i<= count($input['quantity']); $i++) {
        if(empty($input['quantity'][$i]) || !is_numeric($input['quantity'][$i])) continue;
            $items = [ 
                'sales_id' => $sales->id,
                'product_id' => $input['product_id'][$i],
                'product_code' => $input['product_code'][$i],
                'product_name' => $input['product_name'][$i],
                'price' => $input['price'][$i],
                'cost' => $input['cost'][$i],
                'quantity' => intval($input['quantity'][$i]),
                'total_price' => (float) str_replace(',', '', $input['total_price'][$i]),
                'total_cost' => (float) str_replace(',', '', $input['total_cost'][$i]),
            ];

            Salesitems::create($items);
        }

        // $ponumbers = Ponumbers::create($request->only('purchase_no'));
        $invnum = $request->all();
        $invnumbers = new Invnumbers;
        $invnumbers->sales_num = $invnum['invoice_no'];
        $invnumbers->save();

        if ($request){
            Session::flash('message','Invoice was successfully added');
            Session::flash('m-class','alert-success');
        } else {
            Session::flash('message','Data is not saved');
            Session::flash('m-class','alert-danger');
            return redirect()->route('sales.index');
        }
        return redirect()->route('sales.index');
    }
}

My Blade

<input class="form-control autocomplete_txt product_name" type='text' data-type="product_name" id='product_name_1' name='product_name[]' for="1" readonly/>
@if ($errors->has('product_name')) <p class="help-block">{{ $errors->first('product_name') }}</p> @endif

if I submit my form with product name, instead of throwing error from validate,

enter image description here

2 Answers 2

2

By seeing your code, to me it seems your product_id should be an array. So the validation should be:

'product_id' => 'array|required|min:1',

'product_id.*' => 'required',

instead of

'product_id' => 'required|min:1',

Sign up to request clarification or add additional context in comments.

9 Comments

Hi @Imran, yes it is an array. I just followed your suggestion but still it has an error.
What is the error now? Is it same as before or we have solved the validation issue with this and facing another one?
same error, Integrity constraint violation: 1048 Column 'product_id' cannot be null... currently here's my validate looks like, $this->validate($request, [ 'product_id' => 'array|required|min:1', ]); ... I exclude the other part
Can you add dd($input); after this line: $input = $request->all(); and tell me what you have in product_id key?
here's the result @lmran "product_id" => array:3 [▼ 0 => null 1 => null 2 => null ] it is my intension to make the product_id nulled so that I can test if the validate is working
|
0

Try to use $request->validate([... instead of $this->validate($request, [.... I'm not sure is there validate in your controller...

2 Comments

Try to replace 'product_id' => 'required|min:1', by 'product_id.*' => 'required',. This should check each element in your product_id array. But I'm not sure it would work. Saw it here.
Hi @Petr I think it working because the form is not sending but its not returning the error message

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.