#1 March 2, 2020 2:41pm

dlowetz
Member
Registered: March 2, 2020
Posts: 3

Validating that module field has unique value

Hi, I am new to this framework and I've taken over working on an existing site for a client. They have a public_users table and module set up.  There is a public-user extension setup with a "classes/" directory and a PublicUser class that extends BigTreeModuledefined.

When a new user is added, we need to ensure that there is not already an existing user with the same email address.  I'm having trouble figuring where to implement this validation, either in the backend, or if possible, frontend.

Any help would be greatly appreciated.


Thanks!
Daniel

Offline

#2 March 4, 2020 9:27am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 905

Re: Validating that module field has unique value

Hi Daniel!

In the front-end of your website you have full control over how the data gets into the database. The easiest way to check if a user exists in your table before inserting it would be to make an instance of your module's class and use getMatching. e.g.

$mod = new PublicUsers;

if (count($mod->getMatching("email", $_POST["email"])) {
    // user exists
} else {
    // insert user
}

On the back-end, you could do this through a custom field type. You could throw an error in your custom field's process file if there's already a matching email in the database table.

Offline

#3 March 5, 2020 12:40pm

dlowetz
Member
Registered: March 2, 2020
Posts: 3

Re: Validating that module field has unique value

Thank you so much for aiming me in a good direction. I will explore both of these approaches and see which works best.  If I get stumped I will post a follow-up in this thread, in case anyone else could benefit from it.

Thanks again!

Offline

#4 March 19, 2020 8:15pm

dlowetz
Member
Registered: March 2, 2020
Posts: 3

Re: Validating that module field has unique value

I got this working by creating a custom field type and made it add an error to $bigtree["errors"] when the email already exists.  The only problem now is that it throws the error when updating the record.  How can I make it only test the condition only when a new record is being added?

Thanks a million!

Offline

#5 March 20, 2020 9:25am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 905

Re: Validating that module field has unique value

You can check for whether you're updating an existing entry by doing:

if (!empty($bigtree["existing_data"]["id"])) {
   // Updating something that already exists
}

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.