php 在每个字段旁边显示表单验证错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11237655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display form validation errors next to each field
提问by nosx
I am working on a simple contact form written in php and have almost everything setup as I would like for it to be except right now, all validation errors are displaying in a div on the top of my form, and I would like to change the code below so they are displayed next to the form field that caused the error, but I am not sure how to accomplish this so I have come to the experts for some advise.
我正在处理一个用 php 编写的简单联系表单,并且几乎所有设置都按照我希望的方式设置,除了现在,所有验证错误都显示在表单顶部的 div 中,我想更改下面的代码,因此它们显示在导致错误的表单字段旁边,但我不确定如何完成此操作,因此我向专家寻求建议。
PHP Validation Routine
PHP 验证程序
$frm_valid = new Validate();
if(isset($_POST['submit'])){
$rules=array(
array('Type'=>'Required','Var'=>$_POST['name'],'Msg'=>'Name field is required.'),
array('Type'=>'Required','Var'=>$_POST['email'],'Msg'=>'Email address field is required.'),
array('Type'=>'Email','Var'=>$_POST['email'],'Msg'=>'Your email address format is invalid.'),
array('Type'=>'Required','Var'=>$_POST['subject'],'Msg'=>'Subject field is required.'),
array('Type'=>'Required','Var'=>$_POST['message'],'Msg'=>'Message field is required.'),
);
$result = $frm_valid->Valid($rules);
if(is_array($result)){
// Print validation errors (if any)
echo('<div"><b>The form cannot be submitted until the following errors are corrected.</b><ul>');
foreach($result as $key=>$val){
echo '<li>'.$val.'</li>';
}
echo('</ul></div><br />');
}
else {
// Do something else.
}
}
Forms HTML
表单 HTML
<form action="<? echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="text" name="name" value="" /> <br />
<input type="text" name="email" value="" /> <br />
<input type="text" name="subject" value="" /> <br />
<textarea name="message" cols="80" rows="7"></textarea> <br />
<input type="submit" name="submit" value="Send" />
</form>
回答by Praveen Kumar Purushothaman
You need to change the whole code for this!!! The structure, perhaps this way:
您需要为此更改整个代码!!!结构,也许是这样:
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<ul>
<li>
<label>
<span>Name</span>
<input type="text" name="name" />
<small class="errorText"><?php echo ($_POST["name"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<label>
<span>Email</span>
<input type="text" name="email" />
<small class="errorText"><?php echo ($_POST["email"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<label>
<span>Subject</span>
<input type="text" name="subject" />
<small class="errorText"><?php echo ($_POST["subject"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<label>
<span>Message</span>
<textarea name="message" cols="80" rows="7"></textarea>
<small class="errorText"><?php echo ($_POST["message"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<input type="submit" name="submit" value="Send" />
</li>
</ul>
</form>
And the CSS for the same:
和 CSS 相同:
* {font-family: Segoe UI, Tahoma;}
h1 {font-weight: bold; font-size: 14pt; padding: 5px 0; margin: 5px 0; border: 1px solid #999; border-width: 1px 0;}
input[type='submit'] {padding: 5px 20px; cursor: pointer;}
ul, li {display: block; list-style: none; margin: 0; padding: 0;}
ul li {padding: 5px 0;}
ul li label span {display: block; cursor: pointer;}
ul li label .errorText {color: #f00; font-weight: bold; vertical-align: top;}
ul li label textarea {width: 300px;}
You can see a live demo here: Demo
您可以在此处查看现场演示:演示
Error Handling in PHP
PHP 中的错误处理
Keep an error variable for each field. Say,
为每个字段保留一个错误变量。说,
<?php
$error = array(
"name" => "",
"email" => "",
"subject" => "",
"message" => ""
);
?>
Update them with the errors and display them below.
用错误更新它们并在下面显示它们。
<?php
if (empty()$_POST["email"])
$error["email"] = "Email is required!";
elseif (!isEmail($_POST["email"]))
$error["email"] = "Not a Valid Email!";
?>
If there are no errors, it would be empty and the user doesn't see the error message. In your Forms Code, you need to just update this way:
如果没有错误,它将为空并且用户看不到错误消息。在您的表单代码中,您只需要以这种方式更新:
<small class="errorText"><?php echo $error["name"]; ?></small>
<small class="errorText"><?php echo $error["email"]; ?></small>
where in the backend, the $error["email"]would have either "This field is required"or "Not a valid email address!".
在后端,the$error["email"]将具有"This field is required"或"Not a valid email address!"。
回答by Nawroz Salehi
You can use the following way if your every field has a single error :
如果您的每个字段都有一个错误,您可以使用以下方式:
<input name='myfield'>
@if ($errors->has('myfield'))
<span style="color:red;">
{{ $errors->first('myfield') }}
</span>
@endif

