Unique id in field

Permalink
I am developing an add-on for a client and in one of the fields I need it to be a unique integer.

Dow can I specify that the column is unique in the table and confirm on save that the value is unique otherwise provide an error to the user.

Is it possible to ensure the field on the form only accepts values like 1, 203, 999 etc. No value below zero.

 
linuxoid replied on at Permalink Reply
linuxoid
Are you saving an HTML table in a block or user input values in a database with PHP? If the latter, are you using Doctrine or simple DB queries?

Do you need to check input values in front end or back end or both?
hardsoft replied on at Permalink Reply
I am saving to a database table. I need to check the frontend value from the form and conform it is not in the database table. If present in the database table inform the user of a duplicate value.
linuxoid replied on at Permalink Reply
linuxoid
How about something like this:
$app = Application::getFacadeApplication();
$db = $app->make('database')->connection();
$q = 'SELECT column_name FROM table_name WHERE column_name = ?';
$v = [$variable_value];
$fetched_value = $db->fetchColumn($q, $v);
if (!empty($fetched_value)) {
    ...
}