草庐IT

php - 如何正确存储注册用户和大会其他参与者所需的信息? (方案 1 有效,但方案 2 和 3 无效)

coder 2024-04-19 原文

我在下面有一张注册表格,用于在大会上注册用户。注册有 4 种不同的场景。所以注册表单需要处理这 4 种不同的场景。但它不工作,只有场景 1 工作正常。

你知道如何实现这种场景吗?

场景 1:(唯一运行良好的场景)

  • 大会有两种门票类型“门票类型1”和“门票类型2”
  • 工单类型“tt1”有一个自定义问题关联“Whats your phone?”,工单类型“tt2”没有任何关联的自定义问题
  • 所有参与者是“1”,这意味着有必要收集每个参与者的信息(姓名和姓氏)
  • 此场景的图表:

场景 2:

  • 大会有“tt1”和“tt2”两种门票类型
  • 工单类型“tt1”和“tt2”没有关联任何自定义问题
  • all participants为“1”表示需要收集每个参与者的信息
  • 此场景的图表:

场景 3:

  • 大会有“tt1”和“tt2”两种门票类型
  • 工单类型“tt1”有一个自定义问题关联“Whats your phone?”,工单类型“tt2”没有任何关联的自定义问题
  • 所有参与者为“0”,表示不需要收集每个参与者的信息(姓名)
  • 此场景的图表:

场景 4:

  • 大会有“tt1”和“tt2”两种门票类型
  • 工单类型“tt1”和“tt2”没有关联任何自定义问题
  • 所有参与者为“0”,表示不需要收集每个参与者的信息(姓名)
  • 此场景的图表:

在变量“$selectedTypes”中可以找到上一页中选择的门票

//registration.blade.php页面中的注册表单

      <form method="post" id="step1form" action="">
    {{csrf_field()}}
    @if (!empty($allParticipants))
        @if($allParticipants == 1)
            <p>Please fill in all fields. Your tickets will be sent to
                p{{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>

            @foreach($selectedTypes as $k => $selectedType)
                @foreach(range(1,$selectedType['quantity']) as $val)

                    <h6>Participant - {{$val}}  - {{$k}}</h6>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="payment_method" value="referencias">
                        <label class="form-check-label d-flex align-items-center" for="exampleRadios1">
                            <span class="mr-auto">Fill the following fields with the authenticated user information.</span>
                        </label>
                    </div>
                    <div class="form-group font-size-sm">
                        <label for="participant_name" class="text-gray">Name</label>
                        <input type="text" name="participant_name[]" required class="form-control" value="">
                    </div>
                    <div class="form-group font-size-sm">
                        <label for="participant_surname" class="text-gray">Surname</label>
                        <input type="text" required class="form-control" name="participant_surname[]" value="">
                    </div>
                    <input type="hidden" name="ttypes[]" value="{{ $selectedType['id'] }}"/>
                    @foreach($selectedType['questions'] as $customQuestion)
                        <div class="form-group">
                            <label for="participant_question">{{$customQuestion->question}}</label>
                            <input type="text"
                                   @if($customQuestion->pivot->required == "1") required @endif
                                   class="form-control" name="participant_question[]">
                            <input type="hidden" name="participant_question_required[]"
                                   value="{{ $customQuestion->pivot->required }}">
                            <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                        </div>
                    @endforeach
                @endforeach
            @endforeach
            @else
                <p>Its not necessary aditional info. Your tickets will be sent to {{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>

            @endif
        @endif

    <input type="submit" href="#step2"
           id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
</form>

//registration.blade.php 完成在大会上注册用户的方法

public function StoreUserInfo(Request $request, $id, $slug = null, Validator $validator){
    $allParticipants = Congress::where('id', $id)->first()->all_participants;
    $user = Auth::user();

    if($allParticipants){
        $rules = [
            'participant_name.*' => 'required|max:255|string',
            'participant_surname.*' => 'required|max:255|string',
        ];

        $messages = [
            'participant_question.*.required' => 'The participant is required'
        ];

        foreach ($request->participant_question_required as $key => $value) {
            $rule = 'string|max:255'; // I think string should come before max
            //dd($value);
            // if this was required, ie 1, prepend "required|" to the rule
            if ($value) {
                $rule = 'required|' . $rule;
            }

            // add the individual rule for this array key to the $rules array
            $rules["participant_question.{$key}"] = $rule;
        }

        $validator = Validator::make($request->all(), $rules, $messages);

        if($validator->passes()) {
            $registration = Registration::create([
                'congress_id' => $id,
                'main_participant_id' => $user->id,
                'status' => 'C',
            ]);

            $participants = [];

            for ($i = 0; $i < count($request->participant_name); $i++)
                $participants[] = Participant::create([
                    'name' => $request->participant_name[$i],
                    'surname' => $request->participant_surname[$i],
                    'registration_id' => $registration->id,
                    'ticket_type_id' => $request->rtypes[$i]

                ]);

            for ($i = 0; $i < count($request->participant_question); $i++)
                $answer = Answer::create([
                    'question_id' => $request->participant_question_id[$i],
                    'participant_id' => $participants[$i]->id,
                    'answer' => $request->participant_question[$i],
                ]);
            }

        return response()->json([
            'success' => true,
            'message' => 'success'
        ], 200);
    }


    else {

        $messages = [
            'participant_question.*.required' => 'The participant is required'
        ];


        foreach ($request->participant_question_required as $key => $value) {
            $rule = 'string|max:255'; // I think string should come before max
            //dd($value);
            // if this was required, ie 1, prepend "required|" to the rule
            if ($value) {
                $rule = 'required|' . $rule;
            }

            // add the individual rule for this array key to the $rules array
            $rules["participant_question.{$key}"] = $rule;
        }


        $validator = Validator::make($request->all(), $rules, $messages);


        if ($validator->passes()) {

            $registration = Registration::create([
                'congress_id' => $id,
                'main_participant_id' => $user->id,
                'status' => 'C',

            ]);

            $participants = [];

            for ($i = 0; $i < count($request->participant_name); $i++)
                $participants[] = Participant::create([
                    'name' => '',
                    'surname' => '',
                    'registration_id' => $registration->id,
                    'ticket_type_id' => $request->rtypes[$i]

                ]);

            for ($i = 0; $i < count($request->participant_question); $i++)
                $answer = Answer::create([
                    'question_id' => $request->participant_question_id[$i],
                    'participant_id' => $participants[$i]->id,
                    'answer' => $request->participant_question[$i],
                ]);
        }

        return response()->json([
            'success' => true,
            'message' => 'success'
        ], 200);

    }
}

问题的相关模型:

class Congress extends Model
{
    // A conference has many ticket types
    public function ticketTypes(){
        return $this->hasMany('App\TicketType', 'congress_id');
    }

    public function registrations(){
        return $this->hasMany('App\Registration', 'congress_id');
    }
}

// RegistrationModel
class Registration extends Model
{
    // a registration has one user that do the registration (main_participant_id)
    public function customer(){
        return $this->belongsTo('App\User');
    }
    public function congress(){
        return $this->belongsTo('App\Congress');
    }
}

class TicketType extends Model
{
    public function congress(){
        return $this->belongsTo('App\Congress');
    }
}

class Question extends Model
{
    public function registration_type(){
        return $this->belongsToMany('App\RegistrationType', 'ticket_type_questions')
            ->withPivot('required');
    }
}

class Answer extends Model
{
    public function question(){
        return $this->belongsTo('Question');
    }
    public function participant(){
        return $this->belongsTo('Participant');
    }
}

图表显示了我现在拥有的代码的一些问题,即问题中的代码:

最佳答案

我看到两件事

1.- 当 $allParticipants == 0 时,表单不发送任何内容,可能更像是:

    <form method="post" id="step1form" action="">
    {{csrf_field()}}
    @if (!is_null($allParticipants) && is_int($allParticipants))
        @if($allParticipants == 1)   
            <p>Please fill in all fields. Your tickets will be sent to
                p{{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>
        @else
            <p>Its not necessary aditional info. Your tickets will be sent to {{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>
        @endif

        @foreach($selectedTypes as $selectedType)
            @foreach(range(1,$selectedType['quantity']) as $test)
                <h6>Participant - 1 - {{$test}}</h6>
                <div class="form-check">
                    <input class="form-check-input" type="radio" name="payment_method" value="referencias">
                    <label class="form-check-label d-flex align-items-center" for="exampleRadios1">
                        <span class="mr-auto">Fill the following fields with the authenticated user information.</span>
                    </label>
                </div>
                @if($allParticipants == 1)
                    <div class="form-group font-size-sm">
                        <label for="participant_name" class="text-gray">Name</label>
                        <input type="text" name="participant_name[]" required class="form-control" value="">
                    </div>
                    <div class="form-group font-size-sm">
                        <label for="participant_surname" class="text-gray">Surname</label>
                        <input type="text" required class="form-control" name="participant_surname[]" value="">
                    </div>
                   @foreach($selectedType['questions'] as $customQuestion)
                    <div class="form-group">
                        <label for="participant_question">{{$customQuestion->question}}</label>
                        <input type="text"
                                @if($customQuestion->pivot->required == "1") required @endif
                                class="form-control" name="participant_question[]">
                        <input type="hidden" name="participant_question_required[]"
                                value="{{ $customQuestion->pivot->required }}">
                        <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                    </div>
                   @endforeach
                @else
                    <input type="hidden" value="foo" name="participant_name[]"/>
                    <input type="hidden" value="bar" name="participant_surname[]"/>
                @endif
                <input type="hidden" name="ttypes[]" value="{{ $selectedType['id'] }}"/>                
            @endforeach
            @if ($allParticipants == 0) 
              @foreach($selectedType['questions'] as $customQuestion)
                    <div class="form-group">
                        <label for="participant_question">{{$customQuestion->question}}</label>
                        <input type="text"
                                @if($customQuestion->pivot->required == "1") required @endif
                                class="form-control" name="participant_question[]">
                        <input type="hidden" name="participant_question_required[]"
                                value="{{ $customQuestion->pivot->required }}">
                        <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                    </div>
                @endforeach
            @endif
        @endforeach
    @endif

    <input type="submit" href="#step2"
            id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
    </form>

2.- 问题似乎取决于工单类型并且是可选的,函数应该考虑到它。

public function StoreUserInfo(Request $request, $id, $slug = null, Validator $validator){
    $allParticipants = Congress::where('id', $id)->first()->all_participants;
    $user = Auth::user();

    $rules = [];
    $messages = [];

    if(isset($request->participant_question_required)) {
        $messages = [
            'participant_question.*.required' => 'The participant is required'
        ];

        foreach ($request->participant_question_required as $key => $value) {
            $rule = 'string|max:255'; // I think string should come before max
            //dd($value);
            // if this was required, ie 1, prepend "required|" to the rule
            if ($value) {
                $rule = 'required|' . $rule;
            }

            // add the individual rule for this array key to the $rules array
            $rules["participant_question.{$key}"] = $rule;
        }
    }

    if($allParticipants){
        $rules = [
            'participant_name.*' => 'required|max:255|string',
            'participant_surname.*' => 'required|max:255|string',
        ];
    }

    $validator = Validator::make($request->all(), $rules, $messages);

    if($validator->passes()) {
        $registration = Registration::create([
            'congress_id' => $id,
            'main_participant_id' => $user->id,
            'status' => 'C',
        ]);

        $participants = [];
        for ($i = 0; $i < count($request->participant_name); $i++) {
            $name = ($allParticipants) ? $request->participant_name[$i] : '';
            $surname = ($allParticipants) ? $request->participant_surname[$i] : '';
            $participants[] = Participant::create([
                'name' => $name,
                'surname' => $surname,
                'registration_id' => $registration->id,
                'ticket_type_id' => $request->rtypes[$i]

            ]);
        }

        if (isset($request->participant_question))
            for ($i = 0; $i < count($request->participant_question); $i++)
                    $answer = Answer::create([
                        'question_id' => $request->participant_question_id[$i],
                        'participant_id' => $participants[$i]->id,
                        'answer' => $request->participant_question[$i],
                    ]);
    }

    return response()->json([
        'success' => true,
        'message' => 'success'
    ], 200);
}

希望有用! 问候

关于php - 如何正确存储注册用户和大会其他参与者所需的信息? (方案 1 有效,但方案 2 和 3 无效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50030430/

有关php - 如何正确存储注册用户和大会其他参与者所需的信息? (方案 1 有效,但方案 2 和 3 无效)的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  4. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  5. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  6. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  7. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  8. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  9. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  10. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

随机推荐