laracasts / Validation

Easy form validation.
https://packagist.org/packages/laracasts/validation
MIT License
150 stars 37 forks source link

Laracasts \ Validation \ FormValidationException Validation failed #7

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hello I always get this error!

Laracasts \ Validation \ FormValidationException Validation failed open: /var/www/laravel/vendor/laracasts/validation/src/Laracasts/Validation/FormValidator.php $formData, $this->getValidationRules(), $this->getValidationMessages() );

    if ($this->validation->fails())
    {
        throw new FormValidationException('Validation failed', $this->getValidationErrors());
    }
joedawson commented 10 years ago

+1 to this

Edit: @nacr did you solve this?

laracasts commented 10 years ago

I need more information, guys. What code are you running? What does your validation class look like? Is there any reason why it would fail validation?

joedawson commented 10 years ago

Please be aware that 'Model' is referring to the female kind.

Here's my ModelController.php

<?php 

use InkedFemales\Forms\AddModelForm;

class ModelsController extends \BaseController {

    protected $addModelForm;

    public function __construct(AddModelForm $addModelForm)
    {
        $this->addModelForm = $addModelForm;
    }

    public function store()
    {
        $input = Input::all();

        try
        {
            // Validate Input
            $this->addModelForm->validate($input);
            // If Passes, create.
            $model = Model::create($input);
            // Then redirect to models.
            return Redirect::route('models.index');
        }
        catch (FormValidationException $e)
        {
            // Failed.
            return Redirect::back()->withInput()->withErrors($e->getErrors());
        }

    }
}

Here's my InkedFemales\Forms\AddModelorm.php

<?php namespace InkedFemales\Forms;

use Laracasts\Validation\FormValidator;

class AddModelForm extends FormValidator {

    protected $rules = [
        'name'              => 'required|unique',
        'twitter_handle'    => 'unique',
        'instagram_handle'  => 'unique'
    ];
}
joedawson commented 10 years ago

This is super weird, it's appearing to work now. But receiving a different error this time when the form is submitted;

Validation rule unique requires at least 1 parameters.
JeffreyWay commented 10 years ago

Joe - maybe you want 'twitter_handle' => 'unique:table_name'

JeffreyWay commented 10 years ago

If it's working, I'll go ahead and close this thread.

jaimin107 commented 9 years ago

I also have this problem and got error.

Here is my RegistationForm.php file

<?php namespace basicAuth\formValidation;

use Laracasts\Validation\FormValidator; use Laracasts\Validation\FormValidationException;

class RegistrationForm extends FormValidator {

protected $rules = [
    'email' => 'required|email|unique:users',
    'password' => 'required',
    'first_name' => 'required',
    'last_name' => 'required',
];

}

This is RegistrationController.php file <?php

use basicAuth\Repo\UserRepositoryInterface; use basicAuth\formValidation\RegistrationForm;

class RegistrationController extends \BaseController {

/**
 * @var $user
 */
protected $user;

/**
 * @var RegistrationForm
 */
private $registrationForm;

function __construct(UserRepositoryInterface $user, RegistrationForm $registrationForm)
{
    $this->user = $user;
    $this->registrationForm = $registrationForm;
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    return View::make('pages.register');
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    $input = Input::only('first_name', 'last_name', 'email', 'password');

    $this->registrationForm->validate($input);

    $input = Input::only('first_name', 'last_name', 'email', 'password');
    $input = array_add($input, 'activated', true);

    $user = $this->user->create($input);

    // Find the group using the group name
    $usersGroup = Sentry::findGroupByName('Users');

    // Assign the group to the user
    $user->addGroup($usersGroup);

    return Redirect::to('login')->withFlashMessage('User Successfully Created!');
}

}

ghost commented 9 years ago

@jaimin107 What error you get with above code?

jaimin107 commented 9 years ago

I have solved that issue on time when I was working on that project.