3
I am creating form using the following code:
In the create.blade.php file
{!! Form::open(['route'=>'orcamentos.store']) !!}
{!! Form::text('descricao', null, ['class'=>'form-control', 'autofocus']) !!}
{!! Form::text('produto[]', null, ['class'=>'form-control']) !!}
{!! Form::text('produto[]', null, ['class'=>'form-control']) !!}
// Esse campo produto pode se repetir inúmeras vezes em tela
{!! Form::submit('Salvar', ['class'=>'btn btn-primary']) !!}
Note that I may have 'N' input "product".
On my controller I have this:
public function store(OrcamentoRequest $request){...}
And in the Orcamentorequest class I want to validate the mandating of these fields, does anyone know how I can do that?
Within function rules
I have already made some attempts based on the searches made on the internet, but n have succeeded.
Below follows some unsuccessful attempts:
ATTEMPT 1:
$rules = [
'descricao' => 'required',
'produto.*' => 'required',
];
ATTEMPT 2:
foreach($this->request->get('produto') as $key => $val){
$rules['produto.'.$key] = 'required';
}
I also found something in the documentation itself. link here but nothing worked.
The mistake you’re making is this:
ErrorException in helpers.php line 531:
htmlentities() expects parameter 1 to be string, array given
Has anyone been through this before? Or does he know the answer?
Thanks for the explanations... I used the example of 5.2 but he does not know how to fill in the fields at the moment that shows the error message. It happened to you too?
– AndersonSouza
I’ll edit, so that it happens, be aware
– Miguel
Thank you, that worked, but I think the code ta getting a little 'weird' too... changing my design a little and leaving the inputs so product[1], product[2]... and putting inside the Rules so 'product. *' => required . So Laravel already knows how to validate and fill the data back when reassembling the screen, I think I’ll go this way. I honestly didn’t want to touch my views, but within the proper circumstances I think it’s the best option. Thank you, your help was very important.
– AndersonSouza
You have to at least add in the values of the inputs what I have above, products.0, remember that it is not products[0] is how I have there. And add
withInput()
in redirect when there are errors– Miguel
I just didn’t like this part very much here: {{old('products.1')}} , this having to stay fixed in the inputs of the view I think means "gambiarra", because how do I get the data editing view?? , then that your solution would not solve I think. So I will use the numbered inputs because that way Laravel already knows how to work.
– AndersonSouza
I also confess that I think the same. But I don’t know anything better for it using the Laravel
– Miguel
What is the most advised place for me to put the conclusions we have reached from these speeches to the next person not needing to read all the comments?
– AndersonSouza
You can edit the question according to the things that have been clarified and that the solution covers. I’ve already edited the title, I think this is more appropriate to our dicussion
– Miguel
You can also save input values in Session, e.g.:
value={{Session:get('products')[0]}}
...– Miguel
@Andersonsouza realized his doubt as to what to do in editing. Easy, no
value
putvalue="{{$obj_a_editar->product0 or old('products.0')}}"
, which is the equivalent ofvalue="@if(isset($obj_a_editar)){{$obj_a_editar->product0}}@else{{old('product.0')}}@endif"
– Miguel
Yeah, but I think it’s gonna be too much code... but thanks for trying to help. But I will change the name of inputs in the same views, I will leave so product[1], product[2] ... will give me more work now, but the code will get cleaner.
– AndersonSouza