0

I am creating a registration form in 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

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;
                    }
1
  • 1
    why dont you just set 'required' => true? and let the browser handle it? Youll have HTML5 validation as well as serverside. Commented Dec 31, 2015 at 7:58

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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. 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();

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.