dqqyp90576 2015-03-27 17:02
浏览 65
已采纳

如何在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
    }
  • 写回答

1条回答 默认 最新

  • dongwen7380 2015-03-27 17:32
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图