I have a checkbox in Wordpress, which is validating properly until the user doesn´t check the box.
Then it will remain acting as unchecked and will not allow to login again, showing allways the oninvalid message (no matter if it is checked or unchecked).
<input type="checkbox" required id="accept_terms" name="accept_terms"
class="validate[required]" value="1" oninvalid="this.setCustomValidity ('Please...')">
I have also tried with onchange instead of oninvalid. Same problem.
It only had a way to change validation state, adding validity.valueMissing solved it.
onchange="this.setCustomValidity(validity.valueMissing ? 'Please...
Related
I'm having a trouble on how can I check the radio button when the v-models is empty, . If the value of my formfields.status is empty the radio button should automatically checked. Is there anyway trick how to implement it?
This is status value looks like in my dev tools extension when status value is empty
This is what I've tried but nothing works, thanks in advance!.
<div class="checkbox-inline">
<label class="radio mr-2">
<input class="details-input" type="radio" name="status_radio" value="1" v-model="formFields.status" :checked="formFields.status == null"/> Active
</label>
<label class="radio">
<input class="details-input" type="radio" name="status_radio" value="0" v-model="formFields.status"/> Inactive
</label>
</div>
V-model is working with the checked property of radio buttons, so setting that on your own as well might result in faulty behaviour.
When you're using V-model, you'd expect the UI to be always in sync with the data. Note that the "empty selection" state is invalid for radio buttons, there should be one always selected. If you wanted to allow empty selection using an optional select field would be better from UX perspective as well.
So, the easiest way to fix it is disallowing the empty state for your model by providing a default value to the status field, which would control which radio button should be checked by default.
In short:
use "status: 1" in your data to set the default value
remove the :checked bindings from the template
Probably very simple this one, but it's still troubling me.
I have a form, embedded in php, which I need to perform validation on.
An example of one of the fields is:
Server 1:
</td>
<td>
<input name='1_server[1]' value='$defaultserver1' id='server1' required oninvalid="this.setCustomValidity('Please enter server name/IP Address for this connection')">
</td>
</tr>
...as per the above, I have a default value that can be set/inserted when the page loads, but I mainly want to cater for if this isn't set.
If you delete the field, the validation works fine. The problem is, retyping in the field still persist to show the invalid warning.
To Summarise:
If field is empty, and the form submit button is clicked, validation warning shows. You can't, however, fill in the form after this point, as it seems to get 'stuck' in think that the field is 'invalid', even if you complete it.
Can anyone help? I've no doubt I'm being soft...
Edit: Fiddle here https://jsfiddle.net/v03sb4hq/
Resolved this.
setting the oninvalid="setCustomValidity('please fill in')" flag will, on invalid input, set the Validity to a fixed state of 'invalid'.
To counter-act this, I need to state a cleared flag when an input is detected on the same field, i.e. oninput="setCustomValidity('')"
So, my revised code is now...
<input name='1_server[1]' value='$defaultserver1' id='server1' required oninvalid="setCustomValidity('Please enter server name/IP Address for this connection')" oninput="setCustomValidity('')">
Fiddle updated here: https://jsfiddle.net/v03sb4hq/2/
Is $defaultserver1 a php variable? In that case you'd have to echo it within the HTML code, I think.
<input name='1_server[1]' value='<?php echo $defaultserver1;?>' id='server1' required oninvalid="this.setCustomValidity('Please enter server name/IP Address for this connection')">
I created a from with multiple required fields that the users my complete. Then I also have 3 checkboxes of which the user must select at least one of them. When the user submit the form I start doing the checks and each error I find I store in an array. If there are any errors I display the errors for the user plus the form with the values that the user already entered. This bit work fine. I want to know how I would be able to return a checked checkbox if the user checked any of then and any amount, but none, fo them? With from fields it's easy:
<input type="text" name="First_Name" value="<?php echo $First_Name; ?>" />
My question is in what way I would be able to get the result of my checkbox the user select? My guess is that it might be something like:
if(isset())
But I am not sure. Any help on this please?
<input type="checkbox" name="check1" value="1"<?php if (isset($_POST['check1'])) echo " checked"; ?>>
Try if(isset($_POST['checkbox'])) { do this }
If the checkbox has been selected the if statement will run.
When you submit a checkbox, the "value" that has been given will be set. So if your value gets set into a database this probably will be a tinyint(1)
you should use the following:
<input type="text" name="aCheckBox" value="1" <?php if (#$_POST['aCheckBox']=="1") { echo 'checked="checked"';}?> />
The #-sign is supressing the notices, so you dont have to use isset. Isset is useless anyway, because some browsers send the field in the post array but without value. So the isset would ways return true.
I prepend
$('.VHere').prepend('<input type="checkbox" value="test1" id="testid">');
And write into the DB. When posting via form, the does not capture #testid. It capture every other field in the form except for this. Also if the page is reloaded this particular field is captured.
Any idea why.
We need to see more code, but there is no name associated with this input, which may be why the content is not being added to the DB.
I would think it would be
$('.VHere').
prepend('<input type="checkbox" name="testid" value="test1" id="testid">');
where testid populates the field
may be this works?
$('.VHere').prepend('<input type="checkbox" name="testid" checked="checked" value="test1" id="testid">');
because unchecked box cannot be submitted with post as AD7six says and name is of course important to post and catch back..
<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
This works fine, however when you click on submit, and the checkbox is ticked, it passes BOTH the hidden value and the original checkbox value to the $_POST variable in php, can this be avoided?
I have the hidden value there, so that unticked checkboxes are passed to the $_POST variable as well as the ticked ones.
The better approach is to remove the hidden field, and simply have a check in PHP:
if ($_POST['check_box_1']=='1') { /*Do something for ticked*/ }
else { /*Do something for unticked*/ }
You shouldn't need the hidden field. You should in fact not trust any of the form fields sent in the first place. What this means is that you cannot make code which takes the sent fields and trust them to send the correct data (which I assume you do now).
What you should do is to handle all fields you expect to get. That way if you don't get the checkbox value you can still handle that as if it was unticked. Then you also get the added inherent feature of throwing away form data you don't expect in the first place.
No, it will pass all the form data, whatever it is. The right way to do this is not to set the checkbox via a hidden field but to set the checkbox with whatever its state actually is!
I mean... why are you adding the hidden field to begin with?
Your PHP is receiving two fields named check_box_1, and last time I checked there was no way to guarantee that the params would get read into the REQUEST hash in the exact same order as you sent them, so, there's no way to tell which one will arrive last (that's the one whose value will get set). So... this is not the right approach to whatever problem you're trying to solve here.
Welcome to Stack, btw! If you find answers useful or helpful, make sure to mark them as correct and vote them up.
That's normal.
They must be both type="checkbox" to pass only 1 value.
If you want to get only 1 in any cases you can do:
<input type="checkbox" style="display:none;" name="check_box_1" value="0">
Make sure the first input field is of type Checkbox, or else it won't behave like one.
<input type="checkbox" name="check_box_0" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
Everything is working normal with your code so far.
I'm assuming you are creating the hidden field so that 0 is passed to the server when the checkbox is not checked. The problem is that they both get passed when the check box is checked.
As Death said, the way you should be doing it is with a single checkbox and then checking if the value has been sent to the server or not. That's just how checkboxes work.
If you want to have a default set then you will have to handle all that on the server side based on weather the checkbox has a value.
For example:
$myValue = "";
if(isset($_POST['check_box_1']))
{
$myValue=$_POST['check_box_1'];
}
else
{
$myValue="0";
}