You have validation rule exists ... check the documentation
Jan 15, 2019
5
Level 4
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?
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.