I am using validation in an Laravel 5.5 controller like this...
$membership = Membership::find($request->input('membership_id'));
/* Validation Rules */
$rules = [
'key' => [
'required',
Rule::in([$membership->key]),
],
];
This works correctly if membership_id is provided, but if not then it with error....
Trying to get property of non-object
Is there a way I can include the Membership:find function inside of the validation Rule instead so that is respects the require validation?
Use the optional() helper to avoid the "Trying to get property of non-object" error:
Rule::in([optional($membership)->key]),
So, if membership will not be found, null will be returned and a user will be redirected back with validation error message.
Related
I am unable to change the attribute for the array of files in Laravel Validation.
in my FormRequest class, I've made the following validation
public function rules() {
return [
'product_images' => 'nullable|array',
'product_images.*' => [nullable, File::image()],
];
}
public function attributes() {
return [
'product_images.*' => 'Showcase images'
];
}
But in validation errors, it still shows as product images.0 must be an image & not as Showcase images must be an image which should be the expected output.
PS: This is happening only in case of file validations and working fine in normal validations
Add a custom validation message to the lang/validation.php. You can put it at the designated array for custom validation message at the very bottom of the file.
example :
'product_images.*' => [
'image' => 'Show case image must be an image',
]
also insead of File::image() change it to image because I don't if it will work if you dont chamge it.
I create validation using laravel form request file and I want to set session in laravel request file and send the session with validation error to blade view.. can I do that ?
Thanks :)
Using Laravels validation whether that be within the App\Http\Requests or in the controller itself using
$this->validate($request, [
'name' => 'required
]);
Will automatically return errors back to the view itself for you to display within the $errors variable.
If you're calling the Validator::make() method yourself and wish to check the errors manually and redirect. You can do this by using
$validator = Validator::make($request, [...]);
if($validator->fails()) {
return redirect()->back()->withErrors($validator->errors());
}
I am trying to create a custom validation to check if a file was uploaded in a form.
I looked at the custom errors docs but couldn't make it work.
I found this tutorial: Custom Error
In my controller I do this:
if($request->hasFile('file'))
$has_file = 1;
else
$has_file = 0;
$this->validate($request, ['file' => 'isuploaded']);
In App/ServiceProvider I added this:
Validator::extend('isuploaded', function($attribute, $value, $parameters, $validator) {
return ?;
});
What should I return here? How do I send the $has_file to the validator?
If the file field is required the validate will check it for you.
Assuming that your form file input has name="file", just:
$this->validate($request, [
'file' => 'required
]);
In this way if there is no file the validation will fail.
If this is not your case and you want to create the custom rules in Validator::extend you should write your own business logic to check if the file exists and just return true/false.
BadMethodCallException in Validator.php line 3162: Method [validateThisFieldIsRequired] does not exist.
When I dont't provide data to the field than it is inserted successfully, but I provide data to the field than it shows error.
In model:
public static $rules = [
'name' => 'this field is required'
];
In repository:
public function rules()
{
return State::$rules;
}
The problem is that you're passing a message as the field rule instead of an actual rule. So your rule should be the following:
public static $rules = [
'name' => 'required'
];
As shown in the Laravel Validation Documentation a rule is an array of key value pair, where the key is the name of the field that will be validated and the value is the validation rule. So in your cause the field is name and the validation rule is required.
If you want to modify the rule validation messages from the default ones found in lang/en/validation.php, you can read about it the Custom Error Messages section of the documentation.
Was tryin to validate my registration form calling the validation method from the basemodel in my controller
The method
public function postSIgnup ()
{
$validation = User::validate(Input::all());
}
Routes
Route::post('register', array('before=>'csrf', 'uses'=>'UsersController#postSignup'));
Help mi solve this problem
You can't just say 'validate my whole form'.
The reason this error occurs is because you are trying to use the validation method from Laravel.
Basic Form Validation in Laravel
First you want to grab all your form data/content.
$input = Input::all();
Secondly you can setup some rules. Those rules can be set in an array which Laravel will use.
Make sure the names are correctly spelled. (Those are the ones u used in your form-template.)
$rules = array(
'real_name' => 'Required|Min:3|Max:80|Alpha',
'email' => 'Required|Between:3,64|Email|Unique:users',
'age' => 'Integer|Min:18',
'password' =>'Required|AlphaNum|Between:4,8|Confirmed',
'password_confirmation'=>'Required|AlphaNum|Between:4,8'
);
To make use of the validator you have to create anew instance first:
You attach the form input and the rules you have set above.
When they match you can save your form to your database or what else you would like to do with it. That will probably look like:
$validator = Validator::make($input,$rules);
Cool,
We can check now if the Validator passes or not...
if($validator->fails()){
$messages = $validator->messages();
return Redirect::to('yourpage')->withErrors($messages);
}else{
// Handle your data...
}