简体   繁体   English

symfony2中的复选框验证

[英]Checkbox validation in symfony2

I am creating a registration form in symfony2 . 我正在symfony2中创建注册表 I need to validate this checkbox, If a user has not checked this checkbox then there should be a client side validation.I have used client side validation also but it is not validating it. 我需要验证此复选框,如果用户尚未选中此复选框,则应该进行客户端验证。我也使用了客户端验证,但未对其进行验证。
This is my registration form type. 这是我的注册表类型。

$builder->add('check', 'checkbox', array(
                    'label'     => 'I agree the terms and conditions',
                    'required'  => false,
                    'mapped' => false,
                    'value'=>false,
                    'translation_domain' => 'FOSUserBundle'));

This is my client side Java script 这是我的客户端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;
                    }

Can you test with 你可以用

if($("#fos_user_registration_form_check").is(':checked'))
{
    $("#msgCheck").html('Check the Box').css('color','red').show();
    $error=true;
} else {
    $("#msgCheck").hide();
    $error=false;
}

I had many client validations to be done on this form so what I did is that, I declared error as false in the starting and then did all the validations in the similar way. 我需要在此表单上进行许多客户端验证,所以我所做的是,我在一开始就将错误声明为false,然后以类似方式进行了所有验证。 otherwise error was going true for many situations so this is a better solution. 否则错误在许多情况下都会变为真,因此这是一个更好的解决方案。

  $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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM