简体   繁体   中英

Laravel with Confide - Custom validator not working?

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?

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',
        ]
    ];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM