Laravel get array of values imploded from Form Request - laravel

I have the following rules in my From Request:
return [
'brand_id' => 'required|numeric',
'name' => 'required|string',
'wage_type_id' => 'required|numeric',
'work_days' => 'required|array',
'weekly_min' => 'required|numeric',
'weekly_max' => 'required|numeric',
'weekly_utmost' => 'required|numeric',
'daily_std' => 'required|numeric',
'daily_min' => 'required|numeric',
'daily_max' => 'required|numeric',
];
One of them, work_days is supposed to be sent as an array.
I could create the work type from validated form request like
WorkType::create($request->all());
BUT I cannot store an array in database for sure.
Is there a way to get work_days imploded so that I can use the one line above to create my record from the request directly?
Note: I want the array imploded, not json encoded, because I'm going to insert into a field type of SET.

You can actually modify the request params before creating the model in your controller.
$request->merge([
"work_days" => implode(", ", $request->work_days)
]);
WorkType::create($request->all());
If you want it to be in the FormRequest, override the passedValidation() method
class SampleFormRequest extends FormRequest
{
public function passedValidation()
{
$this->request->add([
"work_days" => implode(", ", $this->request->get("work_days"))
]);
}
}
But for me, I prefer writing all properties separately and not using the $request->all()

Related

Multiple inputs in one validation rule

return [
'contract_code' => 'required',
'name' => 'required|string',
'abbreviation' => 'required|string',
'linecount_divisor' => 'required|integer'
];
// into input fields => 'required'
How to shorten the validation rule in multiple inputs?
if You have the same validation rule for multiple and you want to shorten your code just use form requests.
php artisan make:request RequestName
And then in the controller Functions use it
public function save(RequestName $requestName)
{
}
don't forget to use that request class.
use App\Http\Requests\RequestName;
Here is a solution assuming all input fields need to have a common rule like required.
$rules = array_map(function($curr) { return [$curr => 'required']; }, array_keys(request()->all()));
Try this one in Laravel
$validation=array();
$validation= [
'contract_code' => 'required',
'name' => 'required|string',
'abbreviation' => 'required|string',
'linecount_divisor' => 'required|integer'
];
$this->validate($request,$validation);

Required_if laravel request Id 's name must be equal to value-Validation

In my Project I want to perform some validations
public function rules()
{
return [
'first_name' => 'required|string|max:255',
'last_name' => 'string|max:255',
'email' => 'email|max:255|required_without:phone|unique:users',
'phone' => 'string|max:255|required_without:email|unique:users',
'password' => 'required|confirmed|min:8',
'school_id' => 'required|exists:schools,id',
'city' => 'required|string',
'class_id' => 'required|exists:klasses,id',
'school_name'=>'required_if://here i need validation
];
}
Here the school_id is holding the id of the school.. so in my case, If the user passed the id of a school whose having name Others I want to set the school_name as mandatory in the request how do I can achieve this?..
To make school_name mandatory when school_id is empty, you can simply use the required_if rule with the input.
Rule::requiredIf(function () {
return empty($this->input('school_id'));
}
It will check if there's input for the school_id, and if there's not, then the school_name is required.

Laravel one validation function for Store and Update

I am trying to have one validation function for both store and update. So I don't repeat code. It works well. The problem is testing 'unique'. I worked around with this idea. But feels long-winded. Is there a better way to do it?
I want to check for unique at the store.
At update, unique check ignores own id.
I don't want different validations like I did as the user will be
first notified of the unique error, he will fix it. then something
else might be wrong and he has to fix again.
.
public function validateRequest($request){
if($request->method() == "PUT")
{
$val = $this->validate($request, [
'name' => 'unique:customers,id',
'phone' => 'unique:customers,id',
]);
}
if($request->method() == "POST"){
$val = $this->validate($request, [
'name' => 'unique:customers',
'phone' => 'unique:customers'
]);
}
$validation = $this->validate($request, [
'name' => 'required',
'phone' => 'required|integer|gt:0',
'phone2' => 'nullable|integer|gt:0|',
'email' => 'email|nullable',
'note' => 'nullable',
],
[
'phone.integer' => "Invalid phone format. Use international format. Eg: 971550000000",
'phone2.integer' => "Invalid phone format. Use international format. Eg: 971550000000",
'required' => "Required Field",
]);
return $validation;
}

Validation : Laravel

In ContactsRequest.php
public function rules()
{
return [
'org_id' => 'required',
'name' => 'required',
'office' => 'required',
'mobile' => 'required_without:home',
'home' => 'required_without:mobile'
];
}
So basically what i want is , i have a form which will be taking the attributes specified in the code. But i want to modify code so that entering either one of 'home' or 'mobile' will allow me to create the new user.
What should be done.Any help would be appreciated

Laravel 4 - Process via controller after validating in routes file

Im having a little trouble getting used to the whole routes and controllers in Laravel 4.
From my understanding, routes should only be used to decide where to point the URL so it shouldnt be used to process uploads or anything like that.
So basically what i have is my routes file which validates the user fields, then how do i point it to a controller to process teh uploads and everything like that.
I currently have the following code, but when the validation passes and it should go to the controller file, it just displays a blank screen.
Any help would be greatly appreciated.
Route::post('create-profile', function()
{
// Validation rules
$rules = array(
'username' => 'required|unique:users,username|min:4|alpha_dash',
'emailaddress' => 'required|email|unique:users,email',
'country' => 'required',
'state' => 'required',
'genre' => 'required',
'filename' => 'image',
'password' => 'required|min:5|confirmed',
'password_confirmation' => 'required'
);
// Validate the inputs
$v = Validator::make( Input::all(), $rules );
// Was the validation successful?
if ( $v->fails() )
{
// Something went wrong
return Redirect::to('create-profile')->withErrors( $v )->withInput(Input::except('password', 'password_confirmation'));
} else {
// Here is where it seems to all go wrong.
Route::get('create-profile', 'CreateProfileController#processSignup');
}
});
Assuming that your url will be:
http://site.com/create-profile
What I'm showing is not ideal, but I think that your code should look more like the one below:
routes.php
<?php
Route::post('create-profile', 'CreateProfileController#processSignup');
Route::get('create-profile', 'CreateProfileController#signup');
CreateProfileController.php
<?php
Class CreateProfileController extends Controller {
public function processSignup()
{
// Validation rules
$rules = array(
'username' => 'required|unique:users,username|min:4|alpha_dash',
'emailaddress' => 'required|email|unique:users,email',
'country' => 'required',
'state' => 'required',
'genre' => 'required',
'filename' => 'image',
'password' => 'required|min:5|confirmed',
'password_confirmation' => 'required'
);
// Validate the inputs
$v = Validator::make( Input::all(), $rules );
// Was the validation successful?
if ( $v->fails() )
{
// Something went wrong
return Redirect::to('create-profile')->withErrors( $v )->withInput(Input::except('password', 'password_confirmation'));
}
return View::make('success');
}
public function signup()
{
return View::make('signup');
}
}
It's better to have all your routes pointing to controllers actions and let them do the job and, also, it's a little more readable and easy to understand.
Using resource controllers, you can reduce your routes names:
Route::resource('profile', 'ProfileController', array('only' => array('create', 'store')));
Wich will give you those routes:
http://site.com/profile/create
http://site.com/profile/store

Resources