dongpigui8898 2016-12-13 14:37
浏览 10

在zend框架中验证电子邮件2

I have registration form in which there are name, email, etc fields. In email field when I type ** email@yahoo.co ** it is accepted. But in real it is wrong because email@yahoo.co ** is not end with m. It should be **email@yahoo.com.

Is there any way to validate it, so that it only accept email which is ended with .com and in which there is @.

view/register:

<?php
echo $this->form()->openTag($form);
?>
<dl class="zend_form">
<dt><?php echo $this->formLabel($form->get('name')); ?></dt>
<dd><?php 
    echo $this->formElement($form->get('name'));
    echo $this->formElementErrors($form->get('name'));
?></dd>

<dt><?php echo $this->formLabel($form->get('email')); ?></dt>
<dd><?php 
    echo $this->formElement($form->get('email'));
    echo $this->formElementErrors($form->get('email'));
?></dd>

<dt><?php echo $this->formLabel($form->get('password')); ?></dt>
<dd><?php 
    echo $this->formElement($form->get('password'));
    echo $this->formElementErrors($form->get('password'));
?></dd>

<dt><?php echo $this->formLabel($form->get('confirm_password')); ?></dt>
<dd><?php 
    echo $this->formElement($form->get('confirm_password'));
    echo $this->formElementErrors($form->get('confirm_password'));
?></dd>
<br/>
<dd><?php 
    echo $this->formElement($form->get('submit'));
    echo $this->formElementErrors($form->get('submit'));
?></dd>

</dl>
<?php echo $this->form()->closeTag() ?>

Thanks in advance

  • 写回答

1条回答 默认 最新

  • doucanshou6998 2016-12-16 20:55
    关注

    You can validate the form using a custom validator in which you allow domains from a specific whitelist (can be done using a callback validator for instance, even though a proper custom validator would be better).

    It really depends on your use case though, but you could also validate using a blacklist :)

    An interesting solution would probably be having an hidden field (checkbox or radiobutton), and first you validate against your blacklist using the keys of an array from your config, and pass the proper domain as a param for the error message...

    Say your config contains the following

    return [
        'domains_blacklist' => [
            'yahoo.co' => 'yahoo.com',
        ],
    ];
    

    In a custom validator, check whether the email finished with one of these strings:

    in_array($domain_from_value, array_keys($this->config['domains_blacklist'))
    

    In case it does, return an error with a message like "Are you sure the domain you typed (%domain%) is valid?".

    When you display the form again, make sure you tested whether the error exist, and when it exist just set the hidden field visible.

    Finally, change the validator so when the checkbox is checked it does not validate against the list anymore (use a second param in the valid method, it is called context and contains all the fields from the form).

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)