Require field that contains ID from associated table - the correct way

Hello,

please can anyone advice me how to require field in form, which contains id from associated table?

Example:
Table 1: subjects(‘id’, ‘subject_name’, ‘reg’, ‘street’, ‘city’, ‘zip’, ‘country_id’)
Table 2: countries(‘id’, ‘iso’, ‘name’)

Add form related to table 1 contains field, in which user has to select country. Nevertheless I can always send the form without selecting the country, even if the ->notEmpty() is set up for each item in CountriesTable.php (as well as in SubjectsTable.php).

CountriesTable.php:
CountriesTable.php

SubjectsTable.php

Any help?

In SubjectsTable.php, try adding

$validator
    ->integer('country_id')
    ->requirePresence('country_id', 'create');

That was pretty easy, I did not realize that ‘country_id’ related lines were missing . Thank you very much!