Could you show some code.
Arrays in Form Names - Good or Bad Practice?
I'm working with a form where multiple components are brought together, A Person and a List. In the form I'm separating these by array:
Some issues I've run into:
I've tried working with @errors but it accepts only dot notation. I didn't find any helpers for converting person[name] to dot notation so I had to write my own. In the validation the required validate outputs the entire dot notation person.name is required which doesn't necessarily work in ever case. I didn't see an easy way to "filter" this validate message without creating my own custom validation.
I'm new to Laravel so I guess my question is, how common is the naming convention above for my inputs? Is this something I should avoid or are there helper functions I'm not familiar with to aid me in this process?
:: Edits ::
This is really just theory. If I have multiple Model fields in a form, is it a better practice to prefix them with the model or group them in an array? post_xyz vs post[xyz].
Some examples:
A post form might have a post title, post body, and categories. Products might have available stores.
@howdy_mcgee If you’re validating using form requests, then you can define an attributes method that returns an array of the attribute names you’d like to use instead (i.e. without the prefix you have in your HTML form):
class FooFormRequest extends FormRequest
{
public function rules()
{
return [
// Your validation rules...
];
}
public function attributes()
{
return [
'person.name' => 'name',
// And so on...
'list.name' => 'name',
// And so on...
];
}
}
Please or to participate in this conversation.