Avoiding double subscriptions in ajax-form-validation

In the Cookbook-master, there is a nice example of ajax-form-validation. But there is a problem as well : the same email can be registered infinitely.

To check whether the subscriber’s email already exists I can use
page('events/event')->registrations()->toStructure()->findBy('email', $data['email'])
but how to integrate it ?
It can’t go into plugins/validationForm.php because of error "Can't use method return value in write context".

What would be the sane solution to check whether the email exists already in “Registrations” and to send an error “You’ve already registered” ?

Maybe do this in the addToStructure function, there you get the structure field anyway and can check if it exists, and if not, don’t add it and return an error.

Hi, texnixe :slight_smile:
got it. Will return soon with details.

Edit by support: See remarks in the next post!


To check whether un email is not already in registrations field, you need to modify 2 files:

  • plugins/validateForm.php :

before the line :

try {

add :

$isnotnew = page('events/event')->registrations()->toStructure()->findBy('email', $data['email']);
if($response['success'] = true and $isnotnew == true) {
$response['error'] = "You're already subscribed!";}
elseif($response['success'] = true and $isnotnew == false) {

then don’t forget to find and replace :

  }
  return $response;

BY

	}
}  
  return $response;
  • controllers/event.php :

find (at the end) :

}
  }
  return compact('response', 'data');
};

and replace by :

}
  return compact('response', 'data');
}
};

hope it helps. And thanks for the idea, @texnixe :slight_smile:

The function uses variable for the page and the field, so I would keep using these variables here.

A simple if-else statement should be sufficient.

These two lines need a comparison operator: if($response['success'] == true

Did you try, @texnixe ?

No, I didn’t. Why? (and some more chars)

It won’t work. (and some more chars)

Well, maybe it doesn’t and there’s something more wrong with the code, but for sure you can’t use an assignment operator (=) instead of a comparison operator.

The assignment operator is used if you assign a value to a variable. But here you want to check if your variable has a certain value.

If you are using this code inside the addToStructure function, then $response is not known, so that is probably causing your error. Have you set debug to true to find your issues?

You’re certainly right but someone more silly will be needed to clear it out. I’m just glad it works. :neutral_face:

The debug was on. No messages.

Well, you are free to do what you want. I wouldn’t use that code like this. If correct code throws an error, you have to find out why it throws an error, not use wrong code only because it seems to work.

well… the original code doesn’t use “==”. There’s just
if($response['success'] = true”.
My injections need a comparison “==”.

in front of you, I’m a newbie, @texnixe. If you find a minute, have a look at the original ajax-form-validation code then compare it with my modifications. I suppose many users will be grateful to you as the check is useful.

Your corrected code didn’t throw any error. Just nothing happens (nowhere) when you press “submit”.

I guess, the debugger ignores ajax/json problems, if any. It’s a php debugger.

Yes, I will if I have some spare time, but currently, with having to get a project ready and some GDPR stuff for clients to get out, I’m sorry to not be more helpful.

I’m not even sure the structure method is the best place, I think I’d go for a custom validator that can be used in the form validation rules.

Maybe I can update my cookbook recipe at a later time, when Kirby 3 is released. Won’t do that for Kirby 2 anymore.

a custom validator would be better for sure