I've a simple array of rules in rules function:
return [
'anumber' => 'required|numeric'
];
And I fill this in getValidatorInstance method this way:
request()->merge(['anumber' => IdsCompany::getNumDBempty()]);
getNumDBempty()
is working as expected, and returns an int
or false
, I want the validator to only accept the request if the function returns a number.
Controller method:
public function store(StoreIdsCompanyRequest $request, IdsCompany $ids_company_reg)
{
//$ids_company_reg = new IdsCompany;
$data = request()->all();
$user = User::find(auth()->id());
$numEmpresasAsesor = DB::table('accesses')
->where('userId', '=', auth()->id())
->distinct('companyId')->count();
if ($numEmpresasAsesor < ($user->consultant->asesorLimiteEmpresas))
{
if($ids_company_reg->createCompany_ForConsultant($data,$user->id))
{
return redirect()->route('consultants.companies.index')->with('success',trans('textos.SACC_registro_guardado'));
}
else
{
return redirect()->route('consultants.companies.create')->withInput()->withErrors($ids_company_reg->errors);
}
}
else
{
$ids_company_reg->errors = new IlluminateMessageBag;
$ids_company_reg->errors->add('error', trans('textos.CCF_numero_empresas'));
return redirect()->route('consultants.companies.create')->withInput()->withErrors($ids_company_reg->errors);
}
}
The thing is that anumber
is merged with the request but it keeps telling me that anumber
is compulsory.
I've been fighting and searching for a couple of days, and I didn't find any clue of why the validator won't work on variables merged in getValidatorInstance()
. (I've tried to validate this in others FormRequest
and the result is the same, they ignore it like the variable was never there, but the request has it inside.)
Working in Laravel 5.5 atm, and planning to upgrade 5.6 when my form request are done and working.
Well i figured it out:
It seems that the correct way to add new variables is this:
$data['anumber'] = IdsCompany::getNumDBempty();
And this to add it to the request: request()->merge(['anumber' => IdsCompany::getNumDBempty()]);
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.