0

I'm trying to set a max attribute based on the retrieved value in my database but it doesn't seem to work for me.

These is the codes that I tried

//
<input type="number" name="entered_qty" placeholder="Enter Quantity" min="1" max="<?=$data->store_quantity?>" required>
//
<input type="number" name="entered_qty" placeholder="Enter Quantity" min="1" max="{{$data->store_quantity}}" required>

I also tried to retrieve it as value="{{$data->store_quantity}}" to see if I'm able to retrieve the value in my database and it does. Furthermore there data types in my database are int

but if I do it manually like this, it works just fine

<input type="number" name="entered_qty" placeholder="Enter Quantity" min="1" max="5" required>

this is also how I retrieve it in my database in my controller

public function products($encryption_id){
    $encrypt_id = Crypt::decrypt($encryption_id);
    $data=product::where('id',$encrypt_id)->first();

    return view("admin.products",compact("data"));
}

What should I do?

NeedHalp
  • 49
  • 6
  • see here https://stackoverflow.com/questions/18510845/maxlength-ignored-for-input-type-number-in-chrome – John Lobo Jan 07 '23 at 04:18
  • are you referring to `maxlenght` ?, but what I'm trying is the max value – NeedHalp Jan 07 '23 at 04:39
  • `it doesn't seem to work for me`. How it doesn't seem to work to you? Is there any exception thrown, what's shown for `max` attribute? What's the result you get? – Huy Phạm Jan 07 '23 at 05:55

2 Answers2

0

You can validate the user using the dropdown as shown below.

  <select name="entered_qty">
    <option value="0">Select Quantity</option>
    @if(empty($data->store_quantity))
      @for ($i = 1; $i < $data->store_quantity; $i++)
        <option value="{{$i}}">{{$i}}</option>
      @endfor
    @endif
  </select>
0

{{$data->store_quantity}} may not be an integer or number or may be smaller than 0.

Try this:

@if(is_int($data->store_quantity) && $data->store_quantity  > 0)
    <input type="number" name="entered_qty" placeholder="Enter Quantity" min="1" max="{{$data->store_quantity}}" required>
@else
    // The data ur ($data->store_quantity) may not be an integer or it's smaller than 0...
    <p>"Error: Data received is not an integer or number or its smaller than 0"</p>
@endif
  • 1
    Thanks, it's already fixed, as what you mentioned is the problem, had to update the imported data in my database as it was the one affected. – NeedHalp Jan 08 '23 at 03:08