[英]Checkbox validation in symfony2
我正在symfony2中創建注冊表 。 我需要驗證此復選框,如果用戶尚未選中此復選框,則應該進行客戶端驗證。我也使用了客戶端驗證,但未對其進行驗證。
這是我的注冊表類型。
$builder->add('check', 'checkbox', array(
'label' => 'I agree the terms and conditions',
'required' => false,
'mapped' => false,
'value'=>false,
'translation_domain' => 'FOSUserBundle'));
這是我的客戶端Java腳本
var check = $("#fos_user_registration_form_check").val();
if(check!=1)
{
$("#msgCheck").html('Check the Box').css('color','red').show();
$error=true;
}else{
$("#msgCheck").hide();
$error=false;
}
你可以用
if($("#fos_user_registration_form_check").is(':checked'))
{
$("#msgCheck").html('Check the Box').css('color','red').show();
$error=true;
} else {
$("#msgCheck").hide();
$error=false;
}
我需要在此表單上進行許多客戶端驗證,所以我所做的是,我在一開始就將錯誤聲明為false,然后以類似方式進行了所有驗證。 否則錯誤在許多情況下都會變為真,因此這是一個更好的解決方案。
$error=false;
if($("#fos_user_registration_form_check").is(':checked'))
{
$("#msgCheck").html('Check the Box').css('color','red').show();
$error=true;
} else {
$("#msgCheck").hide();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.