1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Validate Field to Certain Code

Discussion in 'Community' started by act242, Nov 8, 2018.

  1. act242

    act242 New Member

    Level: Community
    Good afternoon all.

    I am sure this has been answered before - just don't know what to search for.

    I want to have a field entitled Competition ID. Users have to enter a certain code in order for the form to be submitted to the database.

    Ideally, I would like the field to check a list of codes to see if it validates - but for now I have no clue on how to have it validate just one.

  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    The way to do that would be to have you list of valid codes in a table (create a Fabrik list). If it's called (say) "valid_codes", with a field called "code", you would then use a PHP validation plugin that does this:

    Code (Text):

    $myDb = JFactory::getDbo();
    $myQuery = $myDb->getQuery(true);
    $myQuery->select('code')->from->valid_codes->where('code = ' . $myDb->quote($data));
    $results = $myDb->loadResultt();
    return !empty($results);
    That code will return true (pass validation) if there is a row with a code matching the submitted field's value, and false (fail validation) if not.

    If you want each code to only be used some predefined number of times, it would be easy to add a count field to the valid_codes table, and add some code in a PHP form plugin to mark it used, and check for that in the validation as well.

    -- hugh

Share This Page