1

The documentation of the Zizaco/confide package for Laravel isn't very descriptive when explaining how to use custom validators. I'd like to be able to validate first name, last name, address, city, state etc. fields in my registration process however I haven't found anything that works besides modifying the vendor package. Per the documentation, I should be able to put this in app/models/AccountValidator.php

<?php
use Zizaco\Confide\UserValidatorInterface;
use Zizaco\Confide\ConfideUserInterface;

class AccountValidator implements UserValidatorInterface {

    public function validate(ConfideUserInterface $user) {

        unset($user->password_confirmation); // Because this column doesn't really exists.

        Log::info("Using a custom validator!");

        return true;
    }
}

and in app/start/global.php

App::bind('confide.user_validator', 'AccountValidator');

I've tried putting my rules in the validate function as well as outside of it with no luck, instead of validating anything it'll try to insert the empty values to the database. I've also tried composer dump-autoload as mentioned here. I found this StackOverflow question but the only answer doesn't appear to be an "official" solution, and Zizaco has not replied to this question on Github (yes I did try that code as well), so I'm not sure what else to try.

Any suggestions?

Community
  • 1
  • 1
Winter
  • 1,699
  • 1
  • 19
  • 26
  • Hmm...it looks like you're doing it right. Did you add `implements ConfideUserInterface` to your model and add `use ConfideUser` inside of the class declaration? – Ben Harold Dec 29 '14 at 21:39
  • Yep, it's always one of the first things I do. It validates the username, email and password fields - none of the extras. I did however notice that there's [two separate rulesets in the vendor package](https://github.com/Zizaco/confide/blob/master/src/Confide/UserValidator.php#L44). Should I also be passing that multidimensional array in the custom validator? – Winter Dec 29 '14 at 23:20
  • It looks like you do need to pass the `$rules` as a multi-dimensional array. Have a look at line 163 of UserValidator.php. I checked my `UserValidator` and mine is setup as multi-dimensional. – Ben Harold Dec 29 '14 at 23:36
  • I think I figured it out, I was only passing the rules at first instead of validating them. I setup the validate function to actually validate the data and all seems to work fine now. The documentation wasn't clear, I had thought you only need to pass the rules to the package and it would take care of the data automatically. Guess not.. – Winter Dec 30 '14 at 01:43
  • I don't have a `validate` method in my `UserValidator`, only the `$rules` parameter (as a multi-dimensional array), just FYI. – Ben Harold Dec 30 '14 at 01:52
  • I figured it out, instead of implementing `UserValidatorInterface` I extended `UserValidator` on my custom validator and it works fine now without the `validate` method. My bad, thanks for your help! – Winter Dec 30 '14 at 02:22

1 Answers1

3

This is how I do it:

<?php

use Zizaco\Confide\UserValidator as ConfideUserValidator;
use Zizaco\Confide\UserValidatorInterface;

class UserValidator extends ConfideUserValidator implements UserValidatorInterface
{
    public $rules = [
        'create' => [
            'username' => 'required',
            'email'    => 'required|email',
            'password' => 'required|min:4',
        ],
        'update' => [
            'username' => 'required',
            'email'    => 'required|email',
            'password' => 'required|min:4',
        ]
    ];
}
Ben Harold
  • 6,242
  • 6
  • 47
  • 71