1

I have some validation that checks for a unique email when creating a new person. Works great.

When I am updating the person, it's no surprise the system won't let me save because the email I am saving already exists.

I am working on a condition that compares the id to the existing person to who is getting updated.

Plugin_EmailValidator.php

...
$person = craft()->db->createCommand()
    ->select('id, 'firstName, lastName')
    ->from('people')
    ->where('email=:email', array(':email' => $model['email']))
    ->queryRow();

if(!empty($person)) {
    error_log('MATCH FOUND');

   // I have the id of the existing account. Need to compare against uri segment?
  $existingId = $person['id];
  $segment = craft()->request->getLastSegment();

  error_log($segment); // error!

   $message = Craft::t("Email already exists.");
   $this->addError($model, $attribute, $message);
}

This is what my url looks like:

/admin/plugin/controller/update/123

I see examples of using the request methods within twig, i'm trying to access it via php.

I've also tried this:

craft()->request->getQuery('update');

This will run without error, but it also returns nothing.

3

Have a look at the segments property (shortcut for getSegments() method) of the HttpRequestServices class, which returns an array of uri segments.

craft()->request->segments;

To get the last segment you could use php's end() or array_pop() method.

$segments = craft()->request->segments;
echo end($segments);
| improve this answer | |
  • So close, yet so far. Thank you - this worked perfectly. – Damon Apr 24 '15 at 21:53
  • Glad it worked! "So close, yet so far."?! The first of many challenges ahead? or what? :) – Douglas McDonald Apr 25 '15 at 0:25
  • That - yes. :) And I was close to getting the above to work. On the right track anyway. Coming from CodeIgniter I knew what I was looking for, just not sure how to play by Craft's rules just yet. – Damon Apr 25 '15 at 0:40
  • Most of the time I don't know these answers off the top of my head either — but getting much better at finding answers. Most of this kind of functionality will be in the services class (so requestServices in this case). Then look through the properties and methods until something catches your eye that looks like it might do the trick — or just var_dump() the object and go from there. – Douglas McDonald Apr 25 '15 at 0:49

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.