0

Whoo, that title is a mouthful...

I'm trying to check against a unique school "code" that an athlete has to input to register with a team. The code is different based on what team they are trying to join (referenced in a segment of registration page URL). I have no idea what I'm doing when it comes to PHP or plugins but I hope I'm on the right track. I have no idea how to test what values my variables have.

I've essentially copied the user/SaveUser controller into my own plugin (I have to place user in Athlete user group & related the specific team entry to user, hence custom plugin) and placed snippet below right before the if (craft()->users->saveUser($user)) line.

$teamName = craft()->request->getPost('fields.teamName');
//Get team name, set in hidden input in form, which was grabbed from segment


$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'teams';
$criteria->title   = $teamName;
$criteria->limit   = 1;
$entry = $criteria->first();
//Find the Team entry in my 'teams' section

$schoolCode = $entry->schoolCode;
//Place value of schoolCode field in variable

$codeInput = craft()->request->getPost('fields.schoolCode');
//Grab user's input

if ($schoolCode == $codeInput)
{ 
    craft()->users->saveUser($user)
}

This is, of course, not working. I'm not getting any errors either.

Am I anywhere close to doing this right?

  • I'd start by var_dumping some variables to see if they have the values you're expecting. $teamName, $schoolCode and $codeInput as a starting point. – Brad Bell Apr 7 '15 at 2:19
  • Ah, var_dump - I knew there had to be a simple way... That helped. I figured I would have botched the plugin code. – Chase Giunta Apr 7 '15 at 18:51
1

Thanks to Brad Bell, I was able to troubleshoot using var_dump. The code above works correctly, but I'm unaware of how to do a simple validation of the code within the controller, w/o creating a new model, & send back a response.

Instead (for simplicity on my end), when the validation fails, I redirected to an error specific page, referencing the code entered & team name.

| improve this answer | |
  • Another option to consider is to call your controller via ajax and send back a response — you can then set the error via javascript, and maintain the data that the user has already been entered. – Douglas McDonald Apr 8 '15 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.