Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7763691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:43:21+00:00 2026-06-01T14:43:21+00:00

I have a base model that I extend from. In it, I have defined

  • 0

I have a base model that I extend from.
In it, I have defined two validation filters. One checks if a record is unique, the other checks if a record exists. They work the exact same way except ones return value will be the opposite of the other.

So, it doesn’t sound right to write the same code twice to only return a different value.
I’d like to know how I can call one custom validator from another.

Here’s my code for the unique validator:

<?php
Validator::add('unique', function($value, $rule, $options) {
    $model = $options['model'];
    $primary = $model::meta('key');

    foreach ($options['conditions'] as $field => $check) {
        if (!is_numeric($field)) {
            if (is_array($check)) {
                /**
                 * array(
                 *   'exists',
                 *   'message'    => 'You are too old.',
                 *   'conditions' => array(
                 *       
                 *       'Users.age' => array('>' => '18')
                 *   )
                 * )
                 */
                $conditions[$field] = $check;
            }
        } else {
            /**
             * Regular lithium conditions array:
             * array(
             *   'exists',
             *   'message'    => 'This email already exists.',
             *   'conditions' => array(
             *       'Users.email' //no key ($field) defined
             *   )
             * )
             */
            $conditions[$check] = $value;
        }
    }

    /**
     * Checking to see if the entity exists.
     * If it exists, record exists.
     * If record exists, we make sure the record is not checked
     * against itself by matching with the primary key.
     */
    if (isset($options['values'][$primary])) {
        //primary key value exists so it's probably an update
        $conditions[$primary] = array('!=' => $options['values'][$primary]);
    }

    $exists = $model::count($conditions);
    return ($exists) ? false : true;
});
?>

exists should work like this:

<?php
Validator::add('exists', function($value, $rule, $options) {
    $model = $options['model'];
    return !$model::unique($value, $rule, $options);
});
?>

But obviously, it can’t be done that way. Would I have to define the validation function as an anonymous function, assign it to a variable and pass that in instead of the closure?
Or is there a way I can call unique from within exists?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:43:22+00:00Added an answer on June 1, 2026 at 2:43 pm

    The anonymous function method would work. And then you could use that variable in another anonymous function you define for the ‘exists’ validator. Here’s another idea that incorporates it into your base model class:

    <?php
    
    namespace app\data\Model;
    
    use lithium\util\Validator;
    
    class Model extends \lithium\data\Model {
    
        public static function __init() {
            static::_isBase(__CLASS__, true);
            Validator::add('unique', function($value, $rule, $options) {
                $model = $options['model'];
                return $model::unique(compact('value') + $options);
            });
            Validator::add('exists', function($value, $rule, $options) {
                $model = $options['model'];
                return !$model::unique(compact('value') + $options);
            });
            parent::__init();
        }
    
        // ... code ...
    
        public static function unique($options) {
            $primary = static::meta('key');
    
            foreach ($options['conditions'] as $field => $check) {
                if (!is_numeric($field)) {
                    if (is_array($check)) {
                        /**
                         * array(
                         *   'exists',
                         *   'message'  => 'You are too old.',
                         *   'conditions' => array(
                         *
                         *     'Users.age' => array('>' => '18')
                         *   )
                         * )
                         */
                        $conditions[$field] = $check;
                    }
                } else {
                    /**
                     * Regular lithium conditions array:
                     * array(
                     *   'exists',
                     *   'message'  => 'This email already exists.',
                     *   'conditions' => array(
                     *     'Users.email' //no key ($field) defined
                     *   )
                     * )
                     */
                    $conditions[$check] = $options['value'];
                }
            }
    
            /**
             * Checking to see if the entity exists.
             * If it exists, record exists.
             * If record exists, we make sure the record is not checked
             * against itself by matching with the primary key.
             */
            if (isset($options['values'][$primary])) {
                //primary key value exists so it's probably an update
                $conditions[$primary] = array('!=' => $options['values'][$primary]);
            }
    
            $exists = $model::count($conditions);
            return ($exists) ? false : true;
        }
    
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Article and Post that both inherit from a base model
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
Okay, so we have a utility here in-house that generates business-model classes from our
I have a general OO design question that stems from a Hibernate Model. Example
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have an entity model where the base class in an inheritance structure has
I have a model with nested attributes : class Foo < ActiveRecord::Base has_many :bar
I have a model: review.rb class Review < ActiveRecord::Base validates :title, :presence => true
I have an entity model that has audit information on every table (50+ tables)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.