Search code examples
phphtmlif-statementinputmin

How to put an if statement inside an input's 'min' attribute


I want to make an if condition inside the min="" attribute of an input element.

Here is my code:

<input type="number" id="qty" name="qty" min="1" max="{{ $product_info[0]->quantity - $sessionQty }}" step="1" value="1" readonly>

What I want is to be able to write a condition like:

min = "if($product_info[0]->quantity - $sessionQty == 0) {
           echo 0;
       } else {
           echo 1;
       }"

When my $product_info[0]->quantity - $sessionQty would be equal to zero, the minimum value should be set to 0. when the condition is not zero, the minimum value should be set to 1.

Is this possible?


Solution

  • I am assuming it is php code that you are putting inside min in that case this is the working code as below

    min="<?php echo ($product_info[0]->quantity - $sessionQty) == 0 ? '0' : '1'; ?>"

    if {{}} is from some templating framework then it should also work like below

    min="{{ ($product_info[0]->quantity - $sessionQty) == 0 ? '0' : '1' }}"