I
I
Ivan Petrov2017-06-17 22:03:18
Yii
Ivan Petrov, 2017-06-17 22:03:18

City search validation in yii 1?

Hello. It is not entirely clear, the validation system in yii1 and how it can be used in my case. What would be the best way to do this?
In layouts/main.php I have a search form:

<form action="" method="POST" id="select_city">
          <input type="text" name="referal" class="who" onclick="check()" required>
           <input type="submit" name="submit" value="Выбрать" class="searchCity">
</form>

When you click on the button, the data from the field is written to cookies:
if(isset($_POST['submit'])) {
      $selectedCityId = $_POST['referal'];
       $cookie = new CHttpCookie('referal', $selectedCityId);
       Yii::app()->request->cookies['referal'] = $cookie;
}

Now I can do it, even if you do not enter anything in the field, everything is recorded in cookies. How to do it correctly so that if the entered in the field is not in the table with cities, it is not recorded in cookies and, accordingly, an error pops up. How can I do that? Thank you.
A piece of cookie writing code, also in layouts/main.php.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-06-17
@BorisKorobkov

If there are few cities, replace the textbox with a selectbox with all possible cities. If there is a lot - use a widget, for example, select2.
Well, it’s better to store the city not with text, but with id.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question