Hi tuyenlaptrinh,
Thanks for replying so quickly.
I am wanting to stay in Laravel and not use any further plugins. After a little more digging, I came up with the following:
<div class="form-group">
{{ Form::checkbox('agree', 1, null) }}
{{ Form::label('agree', ' I agree to the Terms & Conditions')}}
{{ $errors->first('agree') }}
</div>
However, I don't know what to do next? Can anyone help?
I want to force users to check the box and add a link to the text 'Terms & Conditions'.
With kind regards,
Mark
This is my rule for register and it work for me.
Model
public static $register_rules = [
'fullname'=> 'required',
'email' => 'required|email',
'password' => 'required|min:3|max:20',
'repassword' => 'required|min:3|max:20',
'terms' => 'required' // This is check checkbox is checked
];
Thanks again for your quick reply.
However, I don't want to have to rebuild the page from scratch. I just want to add the checkbox and ensure that it has been ticked before the form may be submitted. Is there a way of doing that?
With thanks in advance,
Mark
You can use vanilla JS (in addition to server-side validation):
<script type=text/javascript>
function validate(){
var agreeBox = document.getElementById('agree');
if (!agreeBox.checked){
alert('You must agree!');
return false;
}
}
</script>
When opening your form, call the validator onSubmit
{{ Form::open(['route' => 'users.store', 'onSubmit' => 'validate()']) }}
Please please, with jquery validator it ok. or follow aowie1
Thanks aowie1 & tuyenlaptrinh for your help.
I feel that I am almost there.
I modified the code at the start of the form as was suggested, so it read:
{{ Form::open(['route' => 'users.store', 'onSubmit' =>'checkForm(this)']) }}
But, I couldn't get the Javascript to work.
I found this elsewhere:
<script type=text/javascript>
function checkForm(form)
{
if(!form.agree.checked) {
alert("Please indicate that you accept the Terms and Conditions");
form.agree.focus();
return false;
}
return true;
}
</script>
I added the script before the form.
When I left the box unticked, the correct message popped up, but on clicking OK in the box, the form was submitted anyway. If I ticked the box, the form posted to users.store as expected, but the message "Whoops, looks like something went wrong" comes up on an otherwise empty screen.
Both of you have helped a lot. But, I am VERY new to all this and think I may need a little more hand-holding!
With kind regards,
Mark
@Rwthwyn: Change your code like this and it will work:
{{ Form::open(['route' => 'users.store', 'onSubmit' =>'return checkForm(this)']) }}
Notice the "return" while calling your onSubmit event, this is important :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community