Home > database >  want to take only one input between 2 input field from html form
want to take only one input between 2 input field from html form

Time:06-26

I have 2 input in html form. stock in and stock out. In form submission it send st_in and st_out to server with request. I want- *if two value(stock_in stock_out) is submitted system should through error. *If no value submitted(stock_in stock_out) system should through error. *Either "stock in" or "stock out" value should submit then the save method will store the data.

 public function stock_record(Request $request){
        $stock=new Transection;
        $stock->in=$request->st_in;
        $stock->out=$request->st_out;
        $stock->product_id=$request->st_name;
        $stock->barcode=$request->barcode;
        $stock->description=$request->st_description;
        $stock->user_id=Auth::id();
        $stock->save();
        return redirect('dashboard')->with('message',"Transection successfully updated");
    }

enter image description here

enter image description here

enter image description here

CodePudding user response:

public function stock_record(Request $request){

if($request->st_in == null && $request->st_out == null)
{ 
return back()->withError('Error ...');
}

if($request->has('st_in') && $request->has('st_out'))
{
return back()->withError('Error ...');
} else if($request->has('st_in') || $request->has('st_out))
{
        $stock=new Transection;
        $stock->in=$request->st_in ?? null;
        $stock->out=$request->st_out ?? null;
        $stock->product_id=$request->st_name;
        $stock->barcode=$request->barcode;
        $stock->description=$request->st_description;
        $stock->user_id=Auth::id();
        $stock->save();
        return redirect('dashboard')->with('message',"Transection successfully updated");
    }
}

CodePudding user response:

It's working Now

public function stock_record(Request $request) {

    if ($request->st_in == null && $request->st_out == null) {

       return back()->with('message', "you didn't either fill stock in or out");
    }

//correction Here if ($request->st_in > 0 && $request->st_out > 0) { return back()->with('message', "Error, You cannot fill both stock in and out at a time") //

  • Related