Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does it work with validator unique rule? If so should rules be unique:collection? #77

Closed
bitinn opened this issue Dec 4, 2013 · 8 comments

Comments

@bitinn
Copy link

bitinn commented Dec 4, 2013

A simple rule appear to result in this problem:

1) MongoCategoryTest::testValidationUniqueId

ErrorException: Undefined index: result

/var/www/leaf/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Builder.php:151
/var/www/leaf/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1005
/var/www/leaf/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Builder.php:230
/var/www/leaf/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1311
/var/www/leaf/vendor/laravel/framework/src/Illuminate/Validation/DatabasePresenceVerifier.php:57
/var/www/leaf/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:759
/var/www/leaf/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:264
/var/www/leaf/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:225
/var/www/leaf/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:239
/var/www/leaf/app/models/BaseModelMongo.php:35

my validation method:

    /**
     * Take some data and validate again model rules
     *
     * @param   array    data for validation      
     * @return  boolean  result
     */
    public function validate($data)
    {
        // make a new validator object
        $v = Validator::make($data, $this->rules);

        // check for failure
        if ($v->fails())
        {
            // set errors and return error message
            $this->errors = $v->messages();
            return false;
        }

        // validation pass
        return true;
    }

rules:

    protected $rules = array(
        'id' => 'unique:collection|min:1',
        'slug' => 'unique:collection|min:1|max:64',
        'name' => 'min:1|max:256',
    );
@bitinn
Copy link
Author

bitinn commented Dec 4, 2013

ps: removing unique rule and my test works as intended.

@jenssegers
Copy link
Contributor

Validation seems to work for me. It's a bit hard to add it to the tests because it requires quite some components.

@bitinn
Copy link
Author

bitinn commented Dec 4, 2013

would you say following is correct?

    protected $collection = 'leaf.category';
    protected $connection = 'mongodb';

...

    protected $rules = array(
        'id' => 'unique:leaf.category',
    );

i believe i am on latest dev-master already.

@jenssegers
Copy link
Contributor

I don't know about the '.', I tested it with a collection name without a dot.

@jenssegers
Copy link
Contributor

Collection names with a dot seems to work. It runs this query:

mycollection.aggregate([{"$match":{"id":"xxx"}},{"$group":{"_id":null,"*":{"$sum":1}}}])

@bitinn
Copy link
Author

bitinn commented Dec 4, 2013

this explains why, we were using mongodb 2.0 for testing, which doesn't have aggregate

@jenssegers
Copy link
Contributor

Bingo :p

@bitinn
Copy link
Author

bitinn commented Dec 4, 2013

i will close this for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants