Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bellini's avatar

Validate dropdown values

Hey i have a dropdown with 3 values and i want to validate them in order to ensure the user didnt change the html on the browser and sent something else as an input field. the dropdown is going inside the column 'coin'.

 <select class="custom-select" id="inputGroupSelect04" aria-label="Example select with button addon" name="coin" required>
    <option value="" selected disabled hidden>Choose one coin</option>
    <option value="Bitcoin">Bitcoin</option>
    <option value="Ethereum">Ethereum</option>
    <option value="Ripple">Ripple</option>
  </select>

how can i validate this to make sure only one of those 3 options is accepted by my database?

0 likes
5 replies
Cronix's avatar

Typically the better thing to do would be put your currencies in the database. Then use the database to generate the select element, and use the database for the validation in rule. Then you have a single source of truth and only one place you need to update if you add/remove/edit a currency. If you hardcode it, then you have multiple places to update (your views and validation rules).

Things like "yes/no" options...not so much, but things that can change really should. I'm sure there will be other cryptocurrencies created in the future, so you will most likely at least be adding new ones.

1 like
bellini's avatar

@CRONIX - You mean create a table just for storing the cryptocurrencies? For eg name: Ethereum initials:ETH and then loop through it?

Cronix's avatar
Cronix
Best Answer
Level 67

Yes, and give them id's and store the id's in the foreign tables rather than the name of the currency.

currencies
----
id
name
<select name="currency">
    <option value="" selected>Select a currency</option>
    @foreach ($currencies as $currency)
        <option value="{{ $currency->id }}">{{ $currency->name }}</option>
    @endforeach
</select>

validation rule:

'currency' => 'required|exists:currencies,id'

You just need to create a basic currency model and pass $currencies to the view(s) with the form(s).

1 like

Please or to participate in this conversation.