如何在laravel 5中验证克隆的表单元素

如何在laravel 5中验证克隆的表单元素

问题描述:

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
    }

这让我整日疯狂!! p>

我的表单创建 用户。 创建这些用户的管理员可以添加多行输入,然后在最后保存所有内容。 我在jQuery中使用clone()方法,我已经对所需的输入进行了客户端验证等...我有一个非常强大的Domain层,但现在我需要服务器端验证,以便Laravel可以检查电子邮件是否已经 存在,因为它必须是唯一的。 我非常精通Laravel,但是对于新的FormRequest对象,我仍然坚持如何遍历每个输入等,因为FormRequestObject似乎只接受一个条目。 这一切都在ajax ... p>

我的formData如下所示: p>

  counter:2 
_token:KwGAUheSXbzkInh1RZ4RPenx4Fd4fF5DsPm5bjyO 
firstname_1:name1 \  nlastname_1:last1 
email_1:email@email.com 
password_1:基思
firstname_2:NAME2 
lastname_2:last2 
email_2:email@email.com 
password_2:基思
 代码>  PRE> 
 
 因为您可以输入名称具有递增ID,因此表单请求中的rules()必须循环这些。 我真的卡住了,在网上找不到一个例子。 p> 
 
 

我的表单reuqest类看起来像这样: p>

 &lt;?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 {  
 
 
 / ** 
 *确定用户是否有权发出此请求。
 * 
 * @return bool 
 * / 
 public function authorize()
 {
  ; 
} 
 
 / ** 
 *获取适用于请求的验证规则。
 * 
 * @return array 
 * / 
公共函数规则()
 {
返回 [
'firstname'=&gt;  'required',
'lastname'=&gt;  'required',
'email'=&gt;  'required | email | unique:users,email',
'password'=&gt;  'required'
]; 
} 
 
} 
  code>  pre> 
 
 

我的控制器方法到目前为止(它最终会循环输入并调度命令但是 目前毫无意义): p>

  / ** 
 * @param CreateAdministratorRequest $ request 
 * / 
 n public function createAdministrators(CreateAdministratorRequest $ request)
 {
  //每个条目的调度命令
} 
  code>  pre> 
  div>

If you want to keep your structure as possible you could do the following.

public function rules()
{
    $counter = $this->get('counter');
    $emails= [];

    //  validate manually if every submitted email is unique
    // so, we need every email value
    for ($i = 1; $i <= $counter; $i++){
        $emails[] = $this->get('email_' . $i);
    }

    // Now, validate if every email between them are unique
    // array_unique() delete repeated values, so we compare
    // the size before and after filter the values
    $unique = (count($emails) === count(array_unique($emails)));

    if (! $unique){
         return redirect()->back()
             ->withMessage('Sorry, all emails should be different');
    }

    // Now, you need to add every field into the rules
    $rules = [];

    for ($i = 1; $i <= $counter; $i++){

        $rules['email_' . $i] = 'required|email|unique:users,email';
        $rules['lastname_' . $i] = 'required';
        $rules['firstname_' . $i] = 'required';
        $rules['password_' . $i] = 'required';
    }


    return $rules;
}