简体   繁体   English

在 symfony 和 html 中选择默认选择字段

[英]Select the default choice field in a select in symfony and html

Here, I have in my database an insurance table where I store the insurance (with their name and logo) to which doctors can be affiliated.Now the doctor has the possibility to choose one, two or no insurance in his doctor space.But I have a problem, when the doctor is not affiliated with any insurance, I want to choose a default value showing that this doctor does not have insurance yet, but whatever I choose that is not insurance does not take effect.在这里,我在我的数据库中有一个保险表,我在其中存储了医生可以附属的保险(带有他们的名称和徽标)。现在医生可以在他的医生空间中选择一种、两种或不选择保险。但是我有一个问题,当医生不隶属于任何保险时,我想选择一个默认值,表明该医生还没有保险,但是我选择的不是保险的任何东西都不生效。

Controller控制器

public function editAction(Request $request)
{
    $user = $this->getUser();
    if (null === $user) {
        throw new NotFoundHttpException('Utilisateur Inexistant');
    }

    $em             = $this->getDoctrine()->getManager();
    $repo           = $em->getRepository('DoctixMedecinBundle:Medecin');
    $specialiteRepo = $em->getRepository('DoctixAdminBundle:Specialite');

    $cliniqueRepo = $em->getRepository('DoctixAdminBundle:Clinique');

    $assuranceRepo = $em->getRepository('DoctixMedecinBundle:Assurance');

    $object  = $assuranceRepo->findOneBy(['id' => $request->get('assurance')]);
    $medecin = $repo->findOneBy([
        'user' => $user,
    ]);

    if ($request->isMethod('POST')) {
        if ('' != ($pass = $request->get('pass'))) {
            $medecin->getUser()->setSalt('');
            $factory          = $this->get('security.encoder_factory');
            $encoder          = $factory->getEncoder($medecin->getUser());
            $password_encoder = $encoder->encodePassword($pass, $medecin->getUser()->getSalt());
            $medecin->getUser()->setPassword($password_encoder);
        }

        $medecin->setSpecialite($specialiteRepo->find($request->get('specialite')));
        $medecin->setClinique($cliniqueRepo->find($request->get('clinique')));
        $medecin->getUser()->setAdresse($request->get('adresse'));
        $medecin->getUser()->setNumTel($request->get('telephone'));
        $medecin->setQuartier($request->get('quartier'));
        $medecin->setNumOrdre($request->get('numordre'));

        if ($object) {
            $medecin->addAssurance($object);
        }
        $em->persist($medecin);

        $em->flush();

        // redirection avec le status http 301 ::)))))
        $url = $this->generateUrl('medecin_parametre');

        return $this->redirect($url, 301);
    } else {
        return $this->render('DoctixMedecinBundle:Medecin:editProfile.html.twig', [
            'medecin'     => $medecin,
            'specialites' => $specialiteRepo->findAll(),
            'cliniques'   => $cliniqueRepo->findAll(),
            'assurances'  => $assuranceRepo->findAll(),
        ]);
    }
}

Twig枝条

<div class="col-md-6">

  <div class="form-group">
    <label for="assurance" class="control-label">Assurance </label>

    <select id="assurance" class="form-control" placeholder="Assurance" name="assurance" multiple>
      <option value="Pas d Assurance" selected="Pas encore">Pas d'Assurance</option>

      {% for assurance in assurances %}
      <option value="{{ assurance.id }}"
              {% for it in medecin.assurance %}
              {{ it.id== assurance.id ?
      'selected' : '' }}
      {% endfor %} >{{ assurance.nom|upper }}</option>
      {% endfor %}
    </select>

  </div>

</div>

<div class="row">
  <div class="col-md-offset-3 col-md-3">
    <input type="submit" class="btn_1 btn-success medium" form="editProfil" value="Valider">
    <a href="{{ path('medecin_parametre') }}" class=" btn_1 btn-danger medium">Annuler </a>
  </div>
</div>

twig view where will be displayed or not the insurance to which the doctor is affiliated树枝视图将显示或不显示医生所属的保险

<div class="col-md-6">
  <div class="form-group">
    <label>Vos Assurances</label>
    {% for item in medecin.assurance %}
    {% if item is not empty %}
    <input type="text" class="form-control" readonly
           placeholder="{{ item.nom|upper }} ">
    {% else %}
    <input type="text" class="form-control" readonly
           placeholder="Vous n'avez pas d'assurance ">
    {% endif %}
    {% endfor %}
  </div>
</div>

Now how to display like a paragraph saying No insurance when the doctor does not have insurance ?现在医生没有保险的情况下如何显示无保险的段落? Thanks.谢谢。

To check if the medicin have an any assurances you'll have to check if the array medecin.assurance is empty or not要检查药物是否有任何保证,您必须检查数组medecin.assurance是否为空

Something like this:像这样的东西:

{% if medecin.assurance is empty %}
    {# This medecin has no assurances, act accordingly #}
{% else %}
    {# Display medecin's assurances #}
    {% for item in medecin.assurance %}
        {# Display each assurance #}
    {% endfor %}
{% endif %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM