Neil Creagh
Neil Creagh

Reputation: 262

Controlling select options with a numeric field - Silverstripe

'Quantity' is a simple numeric field in the backend.

In a form on the front-end website I'm displaying a dropdown select menu 'Quantity' that shows a max of ten items (and defaults to 10 items if no quantity is provided)

So, for example, if 5 is entered then the select menu should only show options 1 - 5.

This works, but what is a better way to write this:

<% if Quantity %>
  <% if Quantity == 1 %>
     <option value="1">1</option>
  <% end_if %>
  <% if Quantity == 2 %>
     <option value="1">1</option>
     <option value="2">2</option>
  <% end_if %>
  <% if Quantity == 3 %>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
  <% end_if %>
  <% if Quantity == 4 %>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
  <% end_if %>
  <% if Quantity == 5 %>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option> 
  <% end_if %>
  <% if Quantity == 6 %>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option> 
     <option value="6">6</option>
  <% end_if %>
  <% if Quantity == 7 %>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option> 
     <option value="6">6</option>
     <option value="7">7</option>
  <% end_if %>
  <% if Quantity == 8 %>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option> 
     <option value="6">6</option>
     <option value="7">7</option>
     <option value="8">8</option>
  <% end_if %>
  <% if Quantity == 9 %>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option> 
     <option value="6">6</option>
     <option value="7">7</option>
     <option value="8">8</option>
     <option value="9">9</option>
  <% end_if %>
  <% if Quantity > 9 %>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option> 
     <option value="6">6</option>
     <option value="7">7</option>
     <option value="8">8</option>
     <option value="9">9</option>
     <option value="10">10</option>
  <% end_if %>
<% else %>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option> 
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<% end_if %>

Upvotes: 0

Views: 225

Answers (1)

Fatal Error
Fatal Error

Reputation: 1024

Method 1 (Best)

Let's say you've created a Form object for this. You could create a method in your form object to populate the dropdownfield.

MyForm.php

public function setQuantity($quantity)
{
    $range = ArrayLib::valuekey(range(1, $quantity));

    $this->Fields()->dataFieldByName('QuantityDropdownField') // your dropdownfield
        ->setSource($range);

    return $this;
}

Method 2 (Okay)

If you don't want to create a Silverstripe form for this. You could do this.

Page.php

public function getQuantityArrayList()
{
    $array = [];

    for ($i = 1; $i <= $this->Quantity; $i++) {
        $array[] = ArrayData::create([
            'Value' => $i
        ]);
    }

    return ArrayList::create($array);
}

Page.ss

<% loop $QuantityArrayList %>
    <option value="$Value">$Value</option>
<% end_loop %>

Method 3 (Bad)

If, for whatever reason you don't like that either you could do this.

Page.ss

<option value="1">1</option>
<% if not $Quantity || $Quantity == 2 %>
    <option value="2">2</option>
<% end_if %>
<% if not $Quantity || $Quantity == 3 %>
    <option value="3">3</option>
<% end_if %>
<% if not $Quantity || $Quantity == 4 %>
    <option value="4">4</option>
<% end_if %>
<% if not $Quantity || $Quantity == 5 %>
    <option value="5">5</option>
<% end_if %>
<% if not $Quantity || $Quantity == 6 %>
    <option value="6">6</option>
<% end_if %>
<% if not $Quantity || $Quantity == 7 %>
    <option value="7">7</option>
<% end_if %>
<% if not $Quantity || $Quantity == 8 %>
    <option value="8">8</option>
<% end_if %>
<% if not $Quantity || $Quantity == 9 %>
    <option value="9">9</option>
<% end_if %>
<% if not $Quantity || $Quantity > 9 %>
    <option value="10">10</option>
<% end_if %>

All these are better than what you have got right now.
I haven't tested any of this, but you probably get the idea.

Upvotes: 1

Related Questions