猿问

Codeigniter 表单验证 - 如何拒绝默认值

我有一个设置了默认值的表单,例如:


 <input type="text" id="mail" name="mail" value="<?php echo set_value('mail', 'jdoe@example.com'); ?>" />

我使用 Codeigniter 的 form_validation 类验证表单。对于这个领域:


$config = array(

        array(

            'field' =>  'user',

            'label' =>  'Naam',

            'rules' =>  'trim|required|valid_email'

        ));

这是我的问题。当用户保持字段不变并提交其默认值“jdoe@example.com”时,我想拒绝该值。我知道我可以使用回调,但这意味着我必须为表单的每个字段编写一个回调。不太好!


我研究了验证规则。'differs' 看起来很有希望,但它只检查一个字段是否与另一个字段不同。


现在在 Codeigniter 中处理默认值的最佳方式是什么?


慕田峪9158850
浏览 108回答 1
1回答

慕容森

听起来您应该使用占位符而不是默认值。否则你要求的东西没有多大意义。<input type="text" id="mail" name="mail" value="<?php echo set_value('mail'); ?>" placeholder="jdoe@example.com" />否则,您可以尝试内置验证规则“in_list”,例如 in_list[red,blue,green]$config = array(&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'field' =>&nbsp; 'user',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' =>&nbsp; 'Naam',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'rules' =>&nbsp; 'trim|required|valid_email|in_list[jdoe@example.com]'&nbsp; &nbsp; &nbsp; &nbsp; ));编辑:实际上你会想要 in_list 规则的反面,我认为这需要一个回调,但你只需要编写一个回调来用于每个字段。
随时随地看视频慕课网APP
我要回答