This has been driving me crazy all day!!
My form creates users. The admin who creates these users can add several rows of inputs and then saves everything at the end. I'm using the clone() method in jQuery and I have already done the client side validation for required inputs etc... I have a very strong Domain layer but now I need server side validation so that Laravel can check if the email already exists since it must be unique. I am very proficient in Laravel, however with the new FormRequest objects I'm stuck on how to loop through each input etc since the FormRequestObject only seems to accept one entry. This all in ajax...
My formData looks like this:
counter:2
_token:KwGAUheSXbzkInh1RZ4RPenx4Fd4fF5DsPm5bjyO
firstname_1:name1
lastname_1:last1
email_1:email@email.com
password_1:keith
firstname_2:name2
lastname_2:last2
email_2:email@email.com
password_2:keith
As you can the input names have an incrementing id so the rules() in form request must loop through these. I'm really stuck, cannot find one example online.
My form reuqest class looks like this:
<?php namespace hidden\Http\Controllers\UserAccess\Requests;
use hidden\Http\Requests\Request;
use Illuminate\Auth\Guard;
use hidden\Domain\Services\UserAccess\GetUserFromEmailService;
class CreateAdministratorRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required'
];
}
}
My controller method so far (It will eventually loop through the inputs and dispatched a command but pointless at the moment):
/**
* @param CreateAdministratorRequest $request
*/
public function createAdministrators(CreateAdministratorRequest $request)
{
// dispatch command per entry
}