Laravel - Unique Rules Validation - GetMessages Does Not Exist
I have a simple quiz app I'm writing where there is a series of challenges (Easy, Medium, Hard), each with their own questions.
The questions to be added should be unique.
I have the following code to "store" the data
$v = ChallengeQuests::validate(Input::all());
if ( $v->passes() ) {
print 'validate passed';
$record = ChallengeQuests::create(array(
'challenge_id'=> (int) Input::get('challenge_id'),
'question_id'=> (int) Input::get('question_id')
));
$record->save();
return redirect()->to($url['redirects_to']);
} else {
print 'error';
print_r($v->getMessages());
return Redirect::to('/')->withErrors($v->getMessages());
}
In my model I have a validation method
// model
class ChallengeQuests extends Model
{
//
protected $table = 'challengequests';
protected $fillable=[
'challenge_id',
'question_id'
];
public static function validate($input) {
$rules = array(
'challenge_id' => 'Required|Integer',
'question_id' => 'Required|Integer|Unique:questions,id'
);
return Validator::make($input, $rules);
}
}
But when I run my code, Laravel complains
BadMethodCallException in Validator.php line 3016:
Method [getMessages] does not exist.
I'm wanting it so that the question_id is unique.
What am I doing wrong?
Edit:
I am using:
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
Stack trace:
BadMethodCallException in Validator.php line 3016:
Method [getMessages] does not exist.
in Validator.php line 3016
at Validator->__call('getMessages', array()) in ChallengeQuestionsController.php line 78
at ChallengeQuestionsController->store(object(Request))
at call_user_func_array(array(object(ChallengeQuestionsController), 'store'), array(object(Request))) in Controller.php line 80
at Controller->callAction('store', array(object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ChallengeQuestionsController), object(Route), 'store') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ChallengeQuestionsController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ChallengeQuestionsController', 'store') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53
at require_once('/Applications/XAMPP/xamppfiles/htdocs/laravel/scquiz/public/index.php') in server.php line 21
Answer
The correct method used to get the validation messages is not getMessages()
it's messages()
, so your code should look like this:
return Redirect::to('/')->withErrors($v->messages());
Also if you're using Laravel 5, you might want to look into using Form Request Validation which does the same thing you're trying to achieve but in a nicer way, and handles the validation on a different layer, taking care of passing the errors and page redirection for you.
By using a Form Request in your case, the controller method would be reduced to this:
public function store(ChallangeQuestsFormRequest $request)
{
ChallengeQuests::create($request->only('challenge_id', 'question_id'));
return redirect()->to($url['redirects_to']);
}
Since the rules and validation as well as redirection in case of an error, would be handled by the ChallangeQuestsFormRequest
class. Also, using create
to create a model entry will automatically save the entry, so there's no need to use save
on the result of create
.
Related Questions
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → DropzoneJS & Laravel - Output form validation errors
- → laravel check between 2 integer from database
- → How can I use rules method with formRequest in Laravel Js Validation?
- → Is it possible to turn off JavaScript for some tests using Codeception?
- → How to set required validation for inner field of repeater in OctoberCMS?
- → Model Validation in laravel 5.1 not working
- → Issues with jQuery Validate onkeyup and highlighting
- → Laravel validation required rule not working
- → Nested array validation laravel
- → How to change Laravel Validation message for max file size in MB instead of KB?
- → Backbone: Show validation errors for each model in a collection on VIEW
- → OctoberCMS plugin show all the validations at once