如何在提交后检索获取请求值

时间:2017-11-22 14:34:17

标签: php symfony

我正在研究一个Symfony 3项目,我正试图从像这样的控制器动作中的请求中检索一个值(bedroomid

$bedroomid = $request->query->get('bedroomid');

工作正常。请注意,这是在if ($form->isSubmitted() && $form->isValid())条件之前。问题是在我提交表单后,$request->query->get('bedroomid');返回空值。我需要这个值将它传递给另一个这样的动作:

$url = $this->generateUrl('epita_housing_orders_show', array('id' => $id, 'bedroom'=>$bedroomid));
        $response = new RedirectResponse($url);
         return $response;

有什么想法吗?

修改

这是我的控制器动作:

/**
 * @Route("/booking/{id}", requirements={"id" = "\d+"}, name="studentbooking")
 */
public function studentBookingAction(Request $request, $id) {

    $em = $this->getDoctrine()->getManager();
    $user = $this->get('security.token_storage')->getToken()->getUser();      
    $role = $user->getRoles()[0];
    $listing = $em->getRepository('EpitaHousingBundle:Listing')->findOneBy(array('id'=>$id));

    $booking = new Booking($em, $id);
    $form = $this->createForm(BookingType::class, $booking);
    $form->handleRequest($request);
    $bedroomid = $request->query->get('bedroomid');
    $selectedbedroom = $em->getRepository('EpitaHousingBundle:Bedroom')->findOneBy(array('id'=>$bedroomid));

    if ($form->isSubmitted() && $form->isValid()) {

      if ($listing->getPropertytype()->getId() == 1) {
         $booking->setListing($listing);
         $em->persist($booking);
         $em->flush();
         $url = $this->generateUrl('epita_housing_orders_show', array('id' => $id));
        }
      else {
         $booking->setBedroom($selectedbedroom);
         $em->persist($booking); 
         $em->flush();
         $url = $this->generateUrl('epita_housing_orders_show', array('id' => $id, 'bedroom'=>$bedroomid));
      }
     $response = new RedirectResponse($url);
     return $response;
    } 
    return $this->render('EpitaHousingBundle:Student:confirmbookingpage.html.twig', array(
        'id'=>$id,
        'form' => $form->createView(),
        'listing' => $listing,
        'bedroomid' => $bedroomid,
    ));
}

confirmbookingpage.html.twig

{% extends 'EpitaHousingBundle::layout.html.twig' %}
{% trans_default_domain 'FOSUserBundle' %}
{% form_theme form 'EpitaHousingBundle:Form:fields.html.twig' %}

{% block content %}
   <div class="container text-center">   
       <style>
             .error-message{
                color: red;
            }
        </style> 
    <div class="row content"> 
         <div class="col-sm-1"> </div>  
        <div class="col-sm-10 well well-sm">          
        <div class="impinst" style="background-color:#9b3163; color:white;">
            <b> <u>Important Instructions</u> </b> 
        </div>    

        <div class="row">
          <div class="col-sm-12" align="left">  
            <p>  
                The booking service is available to EPITA International Department Students only. 
                This is a free service from the airport to the EPITA campus and surrounding areas.</p>
            <p>Please do not fill this form if:</p>
            <ul>
                <li>You have not yet booked your flight tickets.</li>
                <li>You arrive within the next 72 hours. The airport pickup request must be made atleast 
                    72 hours before the request time.</li>
                <li>You do not want to use the airport pickup service.</li>
            </ul>
            <br>  
            <p>Please note:</p>
            <ul>
                <li>     
                    We may be unable to process your application for airport pickup if you request for a pickup on a non working day(weekends,
                    <br> National Holidays) or between 6PM and 8AM(any day). In such scenarios, we will inform you about the non-availability and provide <br> you with some alternatives. </li> 
                <li style="color:red;">You can cancel (due to technical issues: flight cancelled etc.) not less than 6 hours prior to your arrival time in Paris. If you do not<br>
                    show up at the airport or cancel too late, an 80-euros fee will be automatically charged.</li>   
                </ul>
            <br> 
                <p>For any questions, please do not hesitate to contact us at international@epita.fr</p>   
            </div> 
        </div>  
        <br>
        <div class="impinst" style="background-color:#9b3163; color:white;">
            <b> <u>Booking Details</u> </b> 
        </div> 
        <br><br> 
        {{ form_start(form, {'method': 'post', 'action': path('studentbooking', {'id': id }), 'attr': {'class': 'studentbooking','novalidate': 'novalidate'}}) }}              
        <div class="row">
            <div class="col-sm-2 text-left">{{ form_label(form.arrivaldate, 'Booking From') }}</div>
            <div class="col-sm-3">{{ form_widget(form.arrivaldate) }}</div> 
            <div class="col-sm-7 text-left">{{ form_errors(form.arrivaldate) }}</div> 
        </div>         
        <br>
        <div class="row">
            <div class="col-sm-2 text-left">{{ form_label(form.bookeduntill, 'Booking Till') }}</div>
            <div class="col-sm-3">{{ form_widget(form.bookeduntill) }}</div>
            <div class="col-sm-7 text-left">{{ form_errors(form.bookeduntill) }}</div> 
        </div>   
        <br>    
        <div class="row">
            <div class="col-sm-2 text-left"><b>Amount to be paid</b></div>
            <div class="col-sm-2"><input type="text" name="rent" style="width:110%;margin-left:35px;" value="250" disabled="disabled"/></div>
            <div class="col-sm-2">Euros</div> 

        </div>       
        <br>  
        <div class="row">
        <div class="col-sm-12 text-left">        
            <b> By paying the booking fee, you will bring to an end to the process of your online booking. Your application will then be
            transferred to our board of examiners for processing.(You will not be able to modify your booking anymore)</b>          
        </div>
        </div>     
        <br> 

       {% if listing.propertytype.id == 1 %}
      <a href="{{ path('epita_housing_orders_show', {'listing':id,'id': id }) }}"> 
          <button type="submit" name="confirm" value="Confirm" class="btn btn-primary">Confirm</button> 
      </a>  
      {% else %}
      <a href="{{ path('epita_housing_orders_show', {'bedroom' : bedroomid, 'id': id }) }}"> 
          <button type="submit" name="confirm" value="Confirm" class="btn btn-primary">Confirm</button> 
      </a>    
      {% endif %}

        &nbsp;&nbsp;  
        <a href="{{ path('studentlistingview', {'id': id }) }}">    
                    <input type="submit" name = "cancel" value="Cancel" class="btn btn-primary"/> 
        </a> 
                    <input type="hidden" name = "bedroomid" value={{bedroomid}}/>
        {{ form_end(form) }}
        </div>      
        </div>          
   </div>           
{% endblock %}     

这是将我重定向到学生预订行动的页面:

{% if listing.propertytype.id == 1 %}
      <a href="{{ path('studentbooking', {'listing':id,'id': id }) }}"> 
          <button type="submit" name="approve" value="approve" class="btn btn-primary">Book Now</button> 
      </a>  
      {% else %}
      <a href="{{ path('studentbooking', {'bedroom' : bedroom.id, 'id': id }) }}"> 
{#          <input type="hidden" name="bedroomid" value={{ bedroom.id }}/>#}
          <button type="submit" name="approve" value="approve" class="btn btn-primary">Book Now</button> 
      </a>   

      {% endif %} 

更新的答案:

$form = $this->createForm(BookingType::class, $booking, array('empty_data' => $bedroomid));

在Bookingformtype中创建Hiddenfield,如下所示:

public $bedroomid;
public function __construct($bedroomid = array())
{
        $this->bedroomid = $bedroomid;
}        
public function buildForm(FormBuilderInterface $builder, array $options) {
    $this->bedroomid = $options['empty_data'];
    $builder->add('booking', HiddenType::class, array(    
        'data'=> $this->bedroomid,
        'required' => false,
    ));
   }

尝试控制器:

if ($form->isSubmitted() && $form->isValid()) {
       $bedroom = $form->getData()->getBedroom();
 }

现在提交内容你得到卧室价值......

0 个答案:

没有答案