gpt4 book ai didi

php - 与字段无关的无效消息

转载 作者:行者123 更新时间:2023-12-03 08:30:33 24 4
gpt4 key购买 nike

当两个密码字段不匹配时,我需要显示一个错误。我正在尝试通过在invalid_message repeated字段中设置password来实现此目的,但是当我尝试将错误称为{{form_errors(form.password)}}时,错误不会出现。但是,如果我使用{{form_errors(form)}},则会出现密码不匹配错误。我需要使错误字段具体化,并会非常重视您对此的输入:)

我尝试了许多在线搜索,但没有任何帮助。以下是我的实现,

  • twig
              {{ form_start(form, {'attr': {'class': 'form-horizontal', 'role': 'form', 'novalidate': 'novalidate'}}) }}
    <!--just placed the error here for debug purposes-->
    {{ form_errors(form) }}
    <div class="form-group {% if form.email.vars.errors|length > 0 %}has-error{% endif %} {% if form.email.vars.required == 'true' %}required{% endif %}">
    {{ form_label(form.email) }}
    <div class="col-sm-8">
    {{ form_widget(form.email) }}
    <span class="help-block">{{ form_errors(form.email) }}</span>
    </div>
    </div>
    <div class="form-group {% if form.password.vars.errors|length > 0 %}has-error{% endif %} {% if form.password.vars.required == 'true' %}required{% endif %}">
    {{ form_label(form.password.first) }}
    <div class="col-sm-8">
    {{ form_widget(form.password.first) }}

    </div>
    </div>
    <div class="form-group required">
    {{ form_label(form.password.second) }}
    <div class="col-sm-8">
    {{ form_widget(form.password.second) }}
    </div>
    </div>

    <div class="form-group">
    <div class="center-block btn-sign-in">
    {{ form_widget(form.submit) }}
    </div>
    </div>
    <span class="form-footer-msg">Already have an account? <a href="/signin">Sign In</a></span>
    {{ form_end(form) }}
  • form
    public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder->add('email', 'email', array('label'=>'Email',
    'attr'=>array('class'=>'form-control'),
    'required'=>true,
    'trim' => true,
    'data'=>'user1@test.com',
    'label_attr'=>array('class'=>'col-sm-3 control-label')));

    $builder->add( 'password', 'repeated', array( 'type' => 'password',
    //here is the password mismatch error message
    'invalid_message' => ErrorMessages::PASSWORDS_DONOT_MATCH,
    'options' => array('attr' => array('class' => 'password-field form-control')),
    'error_bubbling' => true,
    'required' => true,
    'trim' => true,
    'first_options' => array('label' => 'Password',
    'error_bubbling' => true,
    'label_attr'=>array('class'=>'col-sm-3 control-label')),
    'second_options' => array('label' => 'Confirm password',
    'error_bubbling' => true,
    'label_attr'=>array('class'=>'col-sm-3 control-label'))));

    $builder->add('submit', 'submit', array('label'=>'Create Account',
    'attr'=>array('class'=>'btn btn-primary')))
    ->setMethod('POST')
    ->getForm();
    }

    public function getName() {

    return 'signup';
    }
  • controller
    public function indexAction() {
    $signUp = new User();

    $form = $this->createForm(new SignUpForm(), $signUp,
    array('action'=>$this->generateUrl('accounts_signup')));

    $request = $this->get('request');
    $form->handleRequest($request);

    if ($request->getMethod() == 'POST') {

    if ($form->isValid()) {

    $request = $this->get("request");
    //do something
    }else{
    //do something else
    }
    }

    return $this->render('AccountsBundle:Signup:index.html.twig',
    array('form'=>$form->createView()));
    }

  • 先生,您对此事的意见非常感激:)非常感谢:)

    最佳答案

    不需要错误冒泡。这是一个工作示例:

        $builder->add('password', 'repeated', array(
    'type' => 'password',
    'label' => 'Zayso Password',
    'required' => true,
    'attr' => array('size' => 20),

    'invalid_message' => 'The password fields must match.',
    'constraints' => new NotBlankConstraint($constraintOptions),

    'first_options' => array('label' => 'Zayso Password'),
    'second_options' => array('label' => 'Zayso Password(confirm)'),

    'first_name' => 'pass1',
    'second_name' => 'pass2',
    ));

    # twig
    {{ form_row(form.password.pass1) }}
    {{ form_row(form.password.pass2) }}

    关于php - 与字段无关的无效消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26217142/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com