Unique Validation ONLY for specific value and NOT whole column

Unique Validation ONLY for specific value and NOT whole column

CapamaniaCapamania Posts: 229Questions: 79Answers: 5
edited August 2016 in Editor

The below filters the table by 'user_id'. Yet the 'unique' validation for 'name' is for the whole column. How can I validate 'name' ONLY for specific 'user_id' provided by $_POST["selectUserID"] ?

Editor::inst( $db, 'table' )
    ->fields(
        Field::inst( 'account_id' ),
        Field::inst( 'user_id' ),
        Field::inst( 'name' )
            ->validator( 'Validate::unique', array(
                'message' => 'Name is already in use. Please choose another Name'
            ) ) 
    )
    
    ->where( function ($z) {
            if ( $_POST["selectUserID"] ) {
                $z->where( 'user_id', $_POST["selectUserID"], '=' );
                    }
    } ) 

Many thanks.

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Hi,

    I'm afraid that the built in unique validator doesn't have an option for what you are looking for (a custom WHERE condition). You could currently need to use a custom validator to run a custom query and check if the value is valid or not.

    Regards,
    Allan

  • CapamaniaCapamania Posts: 229Questions: 79Answers: 5
    edited August 2016

    Thanks Allan! How should this custom validator look like considering the where loop that filters by the desired value of the user:

        ->where( function ($z) {
                if ( $_POST["selectUserID"] ) {
                    $z->where( 'user_id', $_POST["selectUserID"], '=' );
                        }
        } ) 
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    The page I linked to describes the basic make up of custom validation methods. You would need to make a query to the database to check if the input is valid for the user or not. Another option would be to modify the Editor libraries to add an option to add your own condition to the query.

    If you'd like me to write an example for you, that would be covered by the Quick Support 60 option.

    Allan

This discussion has been closed.