Validating selected checkboxes (array) with 'in' - php

I have the following checkboxes in my form:
<div class="form-group">
<label for="likes_turtles" class="col-md-3 control-label">Likes Turtles</label>
<div class="col-md-6">
<div class="checkbox">
<label>
<input type="checkbox" name="likes_turtles[]" value="Yes"> Yes
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="likes_turtles[]" value="No"> No
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="likes_turtles[]" value="Maybe so"> Maybe so
</label>
</div>
</div>
</div>
As you can see, they are an array of items because I want to allow selection of multiple items.
I am trying make it so that the checked items must be valid.
Here is my validation rule:
[likes_turtles] => required|in:Yes,No,Maybe so
The problem is, I'm getting the following error when I check any or all of these items:
The selected likes turtles is invalid.
How do I make laravel simply ensure any items in the request array likes_turtles belong to the specified list e.g. Yes,No,Maybe so

According to the documentation on validating arrays, you could do it like this:
[
"likes_turtles" => "required",
"likes_turtles.*" => "in:Yes,No,Maybe so"
]

Related

How to generate questionnaire form with a MySQL table?

I have a "questionnaire" table which contains question_id, question, and variable_scored (with rows like variable1, variable2, etc.) fields.
What I'm trying to do is generating an HTML questionnaire form based on the content of the table like so
"Question 1 here"
4 radio buttons inputs value for variable1
"Question 2 here"
4 radio buttons inputs value for variable2
Here's my code of current still static form
<div class="form-group">
<label for="input2" class="col-sm-12">Question Here</label>
<div class="col-sm-12">
<div class="radio">
<label><input type="radio" name="variable1" value='4'>4</label>
</div>
<div class="radio">
<label><input type="radio" name="variable1" value="3">3</label>
</div>
<div class="radio">
<label><input type="radio" name="variable1" value="2">2</label>
</div>
<div class="radio">
<label><input type="radio" name="variable1" value="1">1</label>
</div>
I thing you can fetch all details from controller and pass this array into view file.
And view file you can just assign this value like.
<label for="input2" class="col-sm-12"><?php echo $data['question'] ?></label>
<div class="radio">
<label><input type="radio" name="<?php echo $data['variable1'] ?>" value='4'>4</label>
</div>
You can try this one may it works for you.
I provide you also link for you.
link

Force user to pick at least one checkbox - Laravel

How to validate if user has picked at least one checkbox by validating on the server side in Laravel 5.2
I have a simple select box:
<label for="has_driverslicense">KFZ Führerschein: </label>
<select class="form-control" required="required" id="has_driverslicense" name="has_driverslicense">
<option value="0">Nein</option>
<option value="1">Ja</option>
</select>
and a hidden div:
<div id="kfz" style="display: none;">
<div>
<input id="checkbox-20" class="checkbox-style" name="B" type="checkbox">
<label for="checkbox-20" class="checkbox-style-3-label">B (Auto bis 3,49t)</label>
</div>
<div>
<input id="checkbox-21" class="checkbox-style" name="B+E" type="checkbox">
<label for="checkbox-21" class="checkbox-style-3-label">BE (Auto mit Anhänger)</label>
</div>
<div>
<input id="checkbox-22" class="checkbox-style" name="C1" type="checkbox">
<label for="checkbox-22" class="checkbox-style-3-label">C1 (LKW bis 7,49t)</label>
</div>
<div>
<input id="checkbox-23" class="checkbox-style" name="C" type="checkbox">
<label for="checkbox-23" class="checkbox-style-3-label">C (LKW bis 40t)</label>
</div>
<div>
<input id="checkbox-24" class="checkbox-style" name="CE" type="checkbox">
<label for="checkbox-24" class="checkbox-style-3-label">CE (LKW mit Anhänger)</label>
</div>
<div>
<input id="checkbox-25" class="checkbox-style" name="fahrerkarte" type="checkbox">
<label for="checkbox-25" class="checkbox-style-3-label">Fahrerkarte</label>
</div>
<div>
<input id="checkbox-26" class="checkbox-style" name="gabelstapler" type="checkbox">
<label for="checkbox-26" class="checkbox-style-3-label">Gabelstaplerführerschein</label>
</div>
<div>
<input id="checkbox-27" class="checkbox-style" name="gelaendestapler" type="checkbox">
<label for="checkbox-27" class="checkbox-style-3-label">Geländestaplerführerschein</label>
</div>
<div>
<input id="checkbox-28" class="checkbox-style" name="IPAF" type="checkbox">
<label for="checkbox-28" class="checkbox-style-3-label">IPAF – Arbeitsbühnen</label>
</div>
</div>
When user chooses yes/ja he should choose which category does he have.
At the moment all works but i can get a user with driving licence but no category and that is baaaad! :) If you know what i mean!
Help please.
Here is the FIDDLE example
You need to name all of your checkboxes with the same name attribute, ie name="license_type" and you can then switch their names to value ie value="B".
Once this is done in your validation rules you would set.
'license_type' => 'required_if:has_driverslicense,1'

Symfony2, Twig and Checkboxes custom twig template

I am using bootstrap 3 for my template and I am aware of the templating that exist within the Symfony Twig Bridge. Below is what I would like to have in the end.
Where I have a grey area is where I would like to have my label at 90% alignment.
Below is what I currently have:
<div class="form-group">
<div id="campaign_channel" class="col-xs-6">
<input id="campaign_channel_1" type="checkbox" value="1" name="campaign[channel][]">
<label for="campaign_channel_1">Facebook</label>
<input id="campaign_channel_2" type="checkbox" value="2" name="campaign[channel][]">
<label for="campaign_channel_2">Twiiter</label>
<input id="campaign_channel_3" type="checkbox" value="3" name="campaign[channel][]">
<label for="campaign_channel_3">LinkedIn</label>
<input id="campaign_channel_4" type="checkbox" value="4" name="campaign[channel][]">
<label for="campaign_channel_4">Instagram</label>
<input id="campaign_channel_5" type="checkbox" value="5" name="campaign[channel][]">
<label for="campaign_channel_5">Youtube</label>
</div>
</div>
Below is what I would like to have:
<div class="form-group">
<div id="campaign_channel" class="checkbox col-xs-6">
<div class="checkbox-label">Channel(s)</div>
<div class="checkbox-clmn-1">
</div>
<div class="checkbox-clmn-2">
</div>
</div>
</div>
If, as shown in the example, we have 5 checkboxes I want to split any number of checkboxes by using the multiples of three. Say have the first three checkboxes in the first column in a case where total number is odd and then if even then checkboxes should be spraed equally in both checkbox columns.
Where exactly can I create this custom template "fields.html.twig" in my Symfony application so that I can actually play around with this idea?
There is fully explained:
http://symfony.com/doc/current/cookbook/form/form_customization.html#form-theming-in-twig
You can create your own widget rendering (for ex. choice_widget) in separate file or inside file where your form is rendering

Check textarea is empty and checkbox was selected at least one when submit using jquery

I develop an app using codigniter. In view, I have a form like this :
<div class="box-content">
<?php
$properties = array('class' => 'form-horizontal', 'id' => 'myForm');
echo form_open("control_request/userRequest", $properties);
?>
<fieldset>
<div class="control-group">
<label class="control-label">Jenis Request :</label>
<div class="controls" id="chekboxes">
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Login" value="Login" > Login </label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Printer" value="Printer"> Printer </label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Monitor" value="Monitor"> Monitor</label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Computer" value="Computer"> Computer</label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Network" value="Network"> Network</label>
<label class="checkbox inline"><input type="checkbox" name="request[]" id="Lain-lain" value="Lain-lain" > Lain-lain</label>
</div>
</div>
<div class="control-group hidden-phone">
<label class="control-label" for="Complaint" Complaint : </label>
<div class="controls">
<textarea class="cleditor" name="Complaint" id="Complaint" rows="3"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary" id="submit" >Submit</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
<?php echo form_close(); ?>
Now, I create some javascript using jquery to validate, if the checkbox and textarea was qualify before submit. Here is the logic of jquery :
<script>
$(document).ready(function() {
$("#submit").click(function(e){
if (chekbox not selected at least one ) {
<p> Please choose at least one </p>
}
if textbox is empty {
<p> Pleaser fill the textbox </p>
}
if (all qualified)
submit
});
</script>
I just wanna check all of the element is qualified before submitting. But if not qualified, there will be a error message like this
()Checkbox1 ()Checkbox2 ()Checkbox3 ()Checkbox 4 <p>Please select one</p>
<textarea></textarea> <p>Please describe your complaint</p>
I am using jquery 1.9.1 , I will keep learn, and for the help thank you so much..
JSFIDDLE
Your question is not clear ..but it may help you what do you need
$("textarea:empty").length == 0 // text area empty
$(".checkbox [type=checkbox]:checked").length >= 1 // at least one is selected
If you need to make the textarea mandatory, use requierd
attribute. The jQuery validate plugin that you have used will do the
rest of the trick for validation.
For checking if at least one of the checkboxes is selected, you can
add one rule for checkboxes:
rules: {
'request[]': {
required: true,
minlength:2
}
},
OR
You can add a custom rule:
$.validator.addMethod("request[]", function(value, elem, param) {
if($(".request[]:checked").length > 0){
return true;
}else {
return false;
}
},"You must select at least one checkbox to continue");

Check the correct checkbox according to value fetched from database

I am using codeigniter for a project. On the html side, I have 3 checkboxes in which I will see which is the checked one and store the value in database. Now I want to fetch the value and checked the corresponding checkbox.
My checkboxes is as below
<div class="span2">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="flat">
Flat
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
<div class="span2">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="regular">
Regular
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
<div class="span2 border-right">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="sloped">
Sloped
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
So for example if I checked input element with value sloped, the value sloped will be stored in the database, when user logs in, its preloaded with its checked input of the value sloped
Thanks in advance guys!
Okay without your controller and how you're returning the data I am going to give you the basics of how this works. Essentially you're going to be checking the value of shoulder and determining the appropriate box. So in your controller you'd send data to the view something like this (again I have no idea what your db or tables looks like so this is example only).
Controller:
$this->load->model('someModel');
//the following populates the formData variable with an array from your database.
//I am going to assume you know how to do this.
$data['formData'] = $this->someModel->getData();
$this->load->view('someView',$data);
View, while it can be easier to use CI's built in form handler it's not necessary so I'll just use your code for example.
<div class="span2">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="flat"
checked="<?=$formdata['shoulder'] == 'flat' ? 'checked' : '' ;?>">
Flat
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
<div class="span2">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="regular"
checked="<?=$formdata['shoulder'] == 'regular' ? 'checked' : '' ;?>">
Regular
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
<div class="span2 border-right">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="sloped"
checked="<?=$formdata['shoulder'] == 'sloped' ? 'checked' : '' ;?>">
Sloped
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
What the above code is doing is using a shorthand if statement to determine which box should be checked. In each one it checks if the value of 'shoulder' that your database returned is the same as the checkbox and sets it's checked value to checked if it is and blank if it isn't.
It also uses php shorttags, so if those aren't enabled on your server either enable them or adjust the php tags to read:
checked="<?php echo ($formdata['shoulder'] == 'flat' ? 'checked' : '') ;?>"

Categories