AngularJS Validation on <select> with a prompt option - validation

I have the following code for a select drop down input that is styled in Bootstrap.
<select class="form-control" name="businessprocess" ng-model="businessprocess" required>
<option value="">-- Select Business Process --</option>
<option value="C">Process C</option>
<option value="Q">Process Q</option>
</select>
Before the user is able to submit this form, he or she must select a business process.
So I have put the required directive on the select statement, however because the first option tag has -- Select Business Process -- that still counts as a selected option, and thus the required flag is met, and therefore the form validates even though no actual Business Process is selected.
How can I overcome this issue?
Thank You.

This approach could/should solve your issue:
1) declare the options inside of your scope:
$scope.processes = [
{ code: "C", name: "Process C" },
{ code: "Q", name: "Process Q" }
];
And 2) use this declaration:
<select class="form-control" name="businessprocess" ng-model="businessprocess" required
ng-options="c.code as c.name for c in processes" >
<option value="">-- Select Business Process --</option>
</select>
The trick here is in fact, that during the angular cycles, will firstly fix the issue that the the current value is not among the processes options. So, the default would be set to:
<option value="">-- Select Business Process --</option>
and required will be working as expected (more details)

you can initial the value of selector in controller:
<select class="form-control" name="businessprocess" ng-model="businessprocess">
<option value="A">-- Select Business Process --</option>
<option value="C">Process C</option>
<option value="Q">Process Q</option>
</select>
in Controller:
$scope.businessprocess = "A" ;
or "C","Q",whatever you want, so the select will always have value. i think you don't need "required" here in select.
If you don't want an init a value. also do some extra effect when user don't select it.
<select class="form-control" name="businessprocess" ng-model="businessprocess" myRequired>
<option value="">-- Select Business Process --</option>
<option value="C">Process C</option>
<option value="Q">Process Q</option>
</select>
then write the directive:
model.directive("myRequired", function() {
return {
restrict: 'AE',
scope: {},
require: 'ngModel',
link: function(scope, iElement, iAttrs) {
if(iElement.val() == ""){
//do something
return;
} else {
//do other things
}
});
}
};
});

JS
angular.module('interfaceApp')
.directive('requiredSelect', function () {
return {
restrict: 'AE',
require: 'ngModel',
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
attr.requiredSelect = true; // force truthy in case we are on non input element
var validator = function(value) {
if (attr.requiredSelect && ctrl.$isEmpty(value)) {
ctrl.$setValidity('requiredSelect', false);
return;
} else {
ctrl.$setValidity('requiredSelect', true);
return value;
}
};
ctrl.$formatters.push(validator);
ctrl.$parsers.unshift(validator);
attr.$observe('requiredSelect', function() {
validator(ctrl.$viewValue);
});
}
};
});

a best way and straight one is to use:
HTML
<select name="businessprocess" ng-model="businessprocess" required>
<option selected disabled value="">-- Select Business Process --</option>
<option ng-repeat="v in processes" value="{{v.id}}">{{v.value}}</option>
</select>

You can try to add form, like:
HTML
<div ng-app="myApp" ng-controller="MyCtrl">
<form name="mainForm">
<select name="businessprocess" ng-model="businessprocess" required>
<option value="">-- Select Business Process --</option>
<option ng-repeat="v in processes" value="{{v.id}}">{{v.value}}</option>
</select>
<span class="error" ng-show="mainForm.businessprocess.$error.required">required</span>
</form>
</div>
js
angular.module('myApp', []);
function MyCtrl($scope, $timeout) {
$scope.processes = [{
id: "C",
value: "Process C"
}, {
id: "Q",
value: "Process Q"
}];
}
Demo Fiddle

Use the following code snippet
<select class="form-control" name="businessprocess" ng-model="businessprocess">
<option value="A" disabled selected hidden>-- Select Business Process --</option>
<option value="C">Process C</option>
<option value="Q">Process Q</option>
</select>

Add "disabled" to the first option:
<select class="form-control" name="businessprocess" ng-model="businessprocess" required>
<option value="" disabled>-- Select Business Process --</option>
<option value="C">Process C</option>
<option value="Q">Process Q</option>

This works for me. Form is invalid until user selects any other value than "Choose..."
<select name="country" id="country" class="form-control" formControlName="country" required>
<option selected value="">Choose...</option>
<option *ngFor="let country of countries">{{country.country}}</option>
</select>

Maybe this code, can be usefull for this... in this case the form is called myform
<select ng-model="selectX" id="selectX" name="selectX" required>
<option value="" ></option>
<option value="0" >0</option>
<option value="1">1</option>
</select>
<span style="color:red" ng-show="myform.selectX.$dirty && myform.selectX.$invalid">
<span ng-show="myform.selectX.$error.required">Is required.</span>
</span>

Related

How to set a null value inside a drop-down selection without "parent_id" required

I would like to make a drop-down selection with the first selection comes with " NO PARENT ID REFERRED " and set as null value and pass into database in laravel
Here is my code
<div class="form-group">
<label for="bit_app_policy_category_parent">Parent Category</label>
<select id="bit_app_policy_category_parent" name="parent_id" class="form-control">
<option selected disabled>Please select one option</option>
#foreach($parents as $parent)
#if ($parent-> status != 'Freeze')
<option value="{{ $parent->id}}">{{$parent->description}} </option>
#endif
#endforeach
</select>
</div>
<div class="form-group">
<label for="bit_app_policy_category_status">Status<span class="required">*</span></label>
<select id="bit_app_policy_category_status" name="status" class="form-control">
<option value="Active">Active</option>
<option value="Freeze">Freeze</option>
</select>
</div>
Dropdown selection`
Expected result =
Please select one option
-----No parent ID referred-----(able to choose and pass value into db)
description 11 and so on .
PLEASE NOTE THAT IM UNABLE TO USE FORM METHOD AS MY LRAVEL UNABLE TO UPDATE DUDE TO SPECIFIC PURPOSES
try to add another <option> before foreach.
example codes:
<option value="null" selected>Please select one option</option>
#foreach($parents as $parent)
#if ($parent-> status != 'Freeze')
<option value="{{ $parent->id}}">{{$parent->description}} </option>
#endif
#endforeach
</select>
```

How to prevent a form field with display:none from being submitted

I have a sign-up form which asks users to select a country from a drop down menu.
I have a bit of script that, depending on the country selected by the user, displays a particular Region/State field.
$("#Country").change(function() {
if ( $(this).val() == "GB") {
$("#RegionsUS").hide();
$("#RegionsOTHER").hide();
$("#RegionsUK").show();
}
else if ( $(this).val() == "US") {
$("#RegionsUS").show();
$("#RegionsOTHER").hide();
$("#RegionsUK").hide();
}
else {
$("#RegionsOTHER").show();
$("#RegionsUS").hide();
$("#RegionsUK").hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<select name="Country" id="Country" class="form-control" required>
<option selected>Please select</option>
<option value="GB">United Kingdom</option>
<option value="US">United States</option>
<option value="FR">France</option>
</select>
<input type="text" id="RegionsOTHER" name="Region" class="form-control" placeholder="Region/State" maxlength="50">
<select id="RegionsUS" name="Region" class="form-control" style="display:none;">
<option value="Alabama">Alabama</option>
</select>
<select id="RegionsUK" name="Region" class="form-control" style="display:none;">
<option value="England">England</option>
<option value="Scotland">Scotand</option>
</select>
</form>
Problem is, when the form is submitted, all three region fields are submitted despite two of them always being hidden.
How do I prevent the hidden fields from being submitted with the form?
Thank you.
NJ
Sorry, despite searching pretty much all day yesterday and not seeing the following, my question is obviously very similar to this question:
Preventing an HTML form field from being submitted
So I've edited my function and HTML as follows:
$("#Country").change(function() {
if ( $(this).val() == 425) {
$("#RegionsUS").hide();
document.getElementById('RegionsUS').disabled = true;
$("#RegionsOTHER").hide();
document.getElementById('RegionsOTHER').disabled = true;
$("#RegionsUK").show();
document.getElementById('RegionsUK').disabled = false;
}
else if ( $(this).val() == 426) {
$("#RegionsUS").show();
document.getElementById('RegionsUS').disabled = false;
$("#RegionsOTHER").hide();
document.getElementById('RegionsOTHER').disabled = true;
$("#RegionsUK").hide();
document.getElementById('RegionsUK').disabled = true;
}
else {
$("#RegionsOTHER").show();
document.getElementById('RegionsOTHER').disabled = false;
$("#RegionsUS").hide();
document.getElementById('RegionsUS').disabled = true;
$("#RegionsUK").hide();
document.getElementById('RegionsUK').disabled = true;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<select name="Country" id="Country" class="form-control" required>
<option selected>Please select</option>
<option value="GB">United Kingdom</option>
<option value="US">United States</option>
<option value="FR">France</option>
</select>
<input type="text" id="RegionsOTHER" name="Region" class="form-control" placeholder="Region/State" maxlength="50">
<select id="RegionsUS" name="Region" class="form-control" style="display:none;" disabled="disabled">
<option value="Alabama">Alabama</option>
</select>
<select id="RegionsUK" name="Region" class="form-control" style="display:none;" disabled="disabled">
<option value="England">England</option>
<option value="Scotland">Scotand</option>
</select>
</form>

Populate select tag with data from API in Vue and Axios

I want to populate select tag with data from api get request. Here is my html code
<div id="app">
<label for="country" class="control-label">Country</label>
<select v-model="selectedCountry" #change="onChangeCountry" name="country" id="country" class="form-control" tabindex="11">
<option selected disabled value="">Please select one</option>
#foreach($countries as $country)
<option value="{{ $country->id }}">{{ $country->name }}</option>
#endforeach
</select>
<label for="city" class="control-label">City</label>
<select v-model="cities" name="city" id="city" class="form-control" tabindex="12">
<option v-bind:value="city.id">#{{ city.name }}</option>
</select>
</div>
And now my JavaScript code:
<script type="text/javascript">
new Vue({
el: '#app',
data: {
selectedCountry: '',
cities: []
},
methods: {
onChangeCountry: function (event) {
axios.get('http://localhost:8000/api/cities/country/' + this.selectedCountry)
.then(function (
this.cities = response.data
}).catch(function(error) {
console.log('an error occured ' + error);
});
}
}
});
</script>
I'm pretty sure that the data is received because i did a lot of console.log but I don't know how to append the received data to my second select tag nor how to proceed.
Try this in the select
<select v-model="selectedCity" name="city" id="city" class="form-control" tabindex="12">
<option v-for="(city,cityIndex) in cities" :key="city.id" :value="city.id">{{ city.name }}</option>
</select>
Add 'selectedCity' to the data object and then access its value through this.selectedCity
This is in the vue docs
https://v2.vuejs.org/v2/guide/list.html
https://v2.vuejs.org/v2/guide/forms.html#Select
I finally got it working
i just needed to create a city variable in data function
and in select i don't need to bind it to array of cities[], the city variable is fine v-model="city"
<select v-model="country" class="form-control">
<option disabled value="" selected="selected">Please select one</option>
<option v-for="country in countries" :key="country.id" v-bind:value="country.id">
{{country.name}}
</option>
</select>
<script>
export default {
data() {
return {
countries: {},
country:''
}
</script>
You need to use v-for to loop the cities and create the option elements:
<select name="city" id="city" class="form-control" tabindex="12">
<option v-for="(city, index) in cities"
:key="index"
:value="city.id">#{{ city.name }}
</option>
</select>
Also, change your data property into a function so it's reactive:
data () {
return {
selectedCountry: '',
cities: []
}
}

selected option in select element does not work in react

Following code in react shows this error:
"Use the defaultValue or value props on ".
<select>
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
tried this but still does not work:
<select value="1">
<option value="" disabled>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
Trying to implement as in doc in this link but cant make the selected work inside option. Also i am looking to make "choose your option" as a default selected option when the form first loads
You should keep a state variable in your component. I've written some code, and here is a live JSBin:
var MySelect = React.createClass({
getInitialState: function() {
return {
value: "0"
};
},
handleChange: function(event) {
var value = event.target.value;
console.log(value, " was selected");
this.setState({value: event.target.value});
},
render: function() {
return (
<select value={this.state.value} onChange={this.handleChange}>
<option value="0">Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
);
}
})
React.render(
<MySelect />, document.body
);
Replace the console.log with a Flux event or pass the value to your parent with a callback.

Codeigniter -> Select Menu Form Validation

I have come to writing my controller code after doing the view:
A few questions regarding the Select Menus:
How do I validate the select menu - I do not want them to be able to select “Please Select”
Can I still use $this->form_validation->set_rules('','','required');
How do I send the correct answer into the controller?
<label for="hostingRequired">Hosting Required:</label>
<select name="hostingRequired">
<option value="pleaseSelect"> Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<label for="domainRequired">Domain Registration: </label>
<select name="domainRequired">
<option value="pleaseSelect">Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<div id="domainToBeReged">
<label for="domainToBeReged">Domain:</label><input name="domainToBeReged" type="text" placeholder="http://www." />
<label for="domainToBeReged0">Domain:</label><input name="domainToBeReged0" type="text" placeholder="http://www." />
</div>
You could do the following
then use the following validation rule
$this->form_validation->set_rules('','','required|callback_is_default');
function is_default($array)
{
foreach($array as $element)
{
if($element == 'pleaseSelect')
{
return FALSE;
}
}
return TRUE;
}
If you didnt find validation rule that you want, extend Validation library and write your own rule. Look example how to do this.
In your validation rules add:
$this->form_validation->set_rules('hostingRequired','Hosting Required','required|callback__check_select_yes_or_no');
$this->form_validation->set_rules('domainRequired','Domain registration','required|callback__check_select_yes_or_no');
Also, add this additional method to your controller as a validation rule callback:
function _check_select_yes_or_no($str)
{
if ($str != 'yes' && $str != 'no')
{
$this->form_validation->set_message('_check_select_yes_or_no', 'Please select yes or no for the %s field');
return FALSE;
}
else
{
return TRUE;
}
}
This will work for 'Yes' or 'No' options - if you would like to add more adjust the callback, or invert the check to fail if the user selects 'Please select'.
Alternatively, a better way to do this may be to use radio buttons for 'Yes' and 'No' and not have one 'checked' when the form loads.
<fieldset><legend> Hosting required:</legend>
<label><input type="radio" name="hostingRequired" value="yes">Yes</label><br />
<label><input type="radio" name="hostingRequired" value="no">No</label>
</fieldset>
The correct way of doin this is as follows:
first you set the value of the default option to empty
second you use a simple validation rule
So here goes the code
<label for="hostingRequired">Hosting Required:</label>
<select name="hostingRequired">
<option value=""> Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<label for="domainRequired">Domain Registration: </label>
<select name="domainRequired">
<option value="">Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<div id="domainToBeReged">
<label for="domainToBeReged">Domain:</label><input name="domainToBeReged" type="text" placeholder="http://www." />
<label for="domainToBeReged0">Domain:</label><input name="domainToBeReged0" type="text" placeholder="http://www." />
</div>
.
$this->form_validation->set_rules('hostingRequired','HostingRequired','required');
$this->form_validation->set_rules('domainRequired','DomainRequired','required');

Resources