Silverstripe Version: 4.9
Question: Why cant values obtained from getRequest->getVar() be compared?
I am trying to compare a value which I have obtained from a query through a form
<form method="GET">
<label for="year">Select year:</label>
<select name="year" id="year" onchange="this.form.submit()">
<option value="2022">2022</option>
<option value="2021">2021</option>
<option value="2020">2020</option>
<option value="2019">2019</option>
</select>
</form>
public function getQueryYear()
{
return $this->getRequest()->getVar('year');
}
So when I select a value from the form, for example 2022, the function getQueryYear() returns a string as follows (confirmed from var_dump):
string(4) “2022”
Then, inside the template if I try to compare the value of this obtained string:
<% if $getQueryYear == '2022' %>
It does not return true. I have tried casting to integer as well and comparing to int using:
(int)$this->getRequest()->getVar('year');
<% if $getQueryYear == 2022 %>
Still doesnt work. Any ideas? Thank you.