[英]Bootstrap Validator after server side
我有一個表單,可以使用引導程序和驗證程序來驗證客戶端。 例:
<div class="col-sm-6 col-md-5">
<label>EMAIL ADDRESS</label>
<input class="form-control" id="Email" type="email" name="email" required="" data-error="Email address is required" value="maor@traseatlas.com" placeholder="">
<div class="help-block with-errors"></div>
</div>
很好 我接下來要做的是將表單發送到服務器端,並進行另一次驗證。
我期望的是,當服務器端返回錯誤代碼時,該字段未通過更改CSS等正確驗證字段的有效,從而使驗證器“打開”,並使其繼續執行與失敗一樣的行為在客戶端。
因此,如何處理HTML / CSS,以便從服務器返回時,在屏幕上也將顯示相同的警告?
我沒有使用Ajax進行服務器端驗證,只是正常的頁面發布。
謝謝。
一個簡單的解決方案是擁有一個$ errors數組。 當您通過驗證時,將任何錯誤推入所述數組。 如果最后$ errors數組包含任何錯誤,則重新顯示該頁面,並在適當時插入錯誤消息。
一個簡單的例子:
<?php
$errors = [];
if(empty($_POST['some-required-field'])){
$errors['some-required-field'] = "Some Required Field is required!";
}
// ... the rest ...
if(count($errors) == 0){
header('Location: /your-success-page.php');
}
?>
然后,在表單的標記中:
<?php if(isset($errors['some-required-field'])): ?>
<p class="help-block with-errors">
<?: $errors['some-required-field'] ?>
</p>
<?php endif; ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.