比较两个字段以查看它们是否匹配

时间:2017-11-30 16:03:01

标签: forms validation silverstripe

我试图考虑验证表单的两个电子邮件字段的最佳方法:我有一个电子邮件字段和一个重新输入电子邮件字段,我想比较它们以查看它们是否匹配。如果没有,则显示错误消息,直到用户正确输入两个字段的电子邮件。

我知道可以创建自定义字段类以允许自定义验证,但我使用的是2个单独的字段,而不是一个,我不确定自定义类在这种情况下是否有效,因为我&# 39; m不仅仅验证一个字段。

我按照此处找到的示例进行操作,该示例涉及向表单的提交功能添加自定义验证:https://docs.silverstripe.org/en/3/developer_guides/forms/validation/

但是,我不想在任何地方重定向 - 我想留在表单上。

如果我能提供帮助,我想尽可能简单。

这是我到目前为止所拥有的。除了我提到的问题,其他一切都运行良好:

public function DemoForm() {
    $fieldsArr = array();

    $firstName = new TextField('first_name', 'First Name');
    $firstName->setAttribute('placeholder', 'First Name');

    array_push($fieldsArr, $firstName);

    $lastName = new TextField('last_name', 'Last Name');
    $lastName->setAttribute('placeholder', 'Last Name');

    array_push($fieldsArr,$lastName);

    $companytName = new TextField('company_name', 'Company Name');
    $companytName->setAttribute('placeholder', 'Company Name');

    array_push($fieldsArr,$companytName);

    $email = new EmailField('email', 'Email');
    $email->setAttribute('placeholder', 'Email');

    array_push($fieldsArr, $email);


    $retypeemail = new EmailField('retype_email', 'Retype Email');
    $retypeemail->setAttribute('placeholder', 'Retype Email');

    array_push($fieldsArr, $retypeemail);

    $fields = new FieldList($fieldsArr);

    $actions = new FieldList(
        new FormAction('submit', 'Submit')
    );

    $validator = new RequiredFields('first_name', 'last_name', 'email', "retype_email");

    return new Form($this, 'DemoForm', $fields, $actions, $validator);

}


public function submit($data, $form) {
    if($data['email'] != $data['retype_email']){
        $form->addErrorMessage('Email', 'The emails do not match.', 'bad');
        return $this->redirectBack();
    }
    else{
        $demoRequest = new DemoFormSubmission();

        $demoRequest->FirstName = $data['first_name'];
        $demoRequest->LastName = $data['last_name'];
        $demoRequest->EmailAddress =$data['email'];
        $demoRequest->CompanyName = $data['company_name'];

        $demoRequest->write();

        $email = new Email();

        $email->setTo('sara.dejaneiro@innismaggiore.com');
        $email->setFrom($data['email']);
        $email->setSubject("Demo request submission");

        $messageBody = "
        <p><strong>First Name:</strong> {$data['first_name']}</p>
        <p><strong>Last Name:</strong> {$data['last_name']}</p>
        <p><strong>Email: </strong> {$data['email']}</p>
        <p><strong>Company Name:</strong> {$data['company_name']}</p>

    ";
        $email->setBody($messageBody);
        $email->send();

        return $this->redirect("demo-request-submission-thank-you-page");
    }
}

1 个答案:

答案 0 :(得分:2)

鉴于您对我的评论的回复,我认为您可能最好:

  1. 创建自己的表单字段,扩展CompositeField,然后添加自定义验证功能(根据https://docs.silverstripe.org/en/3/developer_guides/forms/validation/#form-validation

  2. 通过扩展RequiredFields(https://github.com/silverstripe/silverstripe-framework/blob/3.6.2/forms/RequiredFields.php)然后重载PHP方法(https://github.com/silverstripe/silverstripe-framework/blob/3.6.2/forms/RequiredFields.php#L82)来创建自定义验证器。

  3. 2号可能是不那么繁琐的方法。您可以编写自己的自定义逻辑,然后如果验证失败,只需返回&#34; false&#34;然后SilverStripe将取消表单提交并将用户返回到表单,其数据仍在表单字段中。