i am trying to show dynamic form error validations using below code but i am getting exception inside ng-class expressions. what is my mistake and how can i solve this?
.html
<form name="myForm" class="form-horizontal" role="form" ng-submit="submitForm()">
<div ng-repeat="field in entity.fields">
<ng-form name="form">
<!-- TEXT FIELDS -->
<div ng-if="field.type=='text'" class="form-group" ng-class="{ 'has-error' :
form.+field.name+.$dirty && form.+field.name+.$error.required,
'has-success': form.+field.name+.$valid}">
<label class="col-sm-2 control-label">{{field.label}}</label>
<div class="col-sm-6">
<input type="{{ field.type }}" dynamic-name="field.name" id="{{field.name}}" ng-model="field.data"
class="form-control" required />
<p class="help-block" ng-show="{{'form.'+field.name+'.$dirty && form.'+field.name+'.$error.required'}}">Required</p>
</div>
</div>
</ng-form>
</div>
</form>
.controller
routerApp.controller('DynamicController1', ['$scope', function ($scope) {
// we would get this from the api
$scope.entity = {
name: "Course",
fields:
[
{ type: "text", name: "firstname", label: "Name", required: true, data: "" },
{ type: "radio", name: "color_id", label: "Colors", options: [{ id: 1, name: "orange" }, { id: 2, name: "pink" }, { id: 3, name: "gray" }, { id: 4, name: "cyan" }], required: true, data: "" },
{ type: "email", name: "emailUser", label: "Email", required: true, data: "" },
{ type: "text", name: "city", label: "City", required: true, data: "" },
{ type: "password", name: "pass", label: "Password", min: 6, max: 20, required: true, data: "" },
{ type: "select", name: "teacher_id", label: "Teacher", options: [{ name: "Mark" }, { name: "Claire" }, { name: "Daniel" }, { name: "Gary" }], required: true, data: "" },
{ type: "checkbox", name: "car_id", label: "Cars", options: [{ id: 1, name: "bmw" }, { id: 2, name: "audi" }, { id: 3, name: "porche" }, { id: 4, name: "jaguar" }], required: true, data: "" }
]
};
}]);
https://codepen.io/rama-krishna-the-selector/pen/PXOwQp?editors=1010
angular.js:15536 Error: [$parse:syntax] http://errors.angularjs.org/1.7.5/$parse/syntax?p0=%2B&p1=is%20not%20a%20valid%20identifier&p2=119&p3=%7B%20'has-error'%20%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20form.firstname.%24dirty%20%26%26%20form.firstname.%24error.required%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'has-success'%3A%20form.%2Bfield.name%2B.%24valid%7D&p4=%2Bfield.name%2B.%24valid%7D
The only correct way should be
<div ng-repeat="field in entity.fields">
<ng-form name="form">
<div ng-if="field.type=='text'" class="form-group" ng-class="{ 'has-error' :
form[field.name].$dirty && form[field.name].$error.required,
'has-success': form[field.name].$valid}">
<label class="col-sm-2 control-label">{{field.label}}</label>
<div class="col-sm-6">
<input type="{{ field.type }}" dynamic-name="field.name" id="{{field.name}}" ng-model="field.data"
class="form-control" required />
<p class="help-block" ng-show="form[field.name].$dirty && form[field.name].$error.required">Required</p>
</div>
</div>
</ng-form>
codeopen
The point is to access form object property, that stored in a variable. This can be done only this way form[field.name], but what you did - you tried to merge object properties and names into a single string, that wont work that way
Related
I am trying to get selected radio button value using below code but i am getting undefined value. what is my mistake?
html
<form name="form" class="form-horizontal" role="form">
<!-- RADIO BUTTONS -->
<div class="form-group">
<label class="col-sm-2 control-label">Clouds</label>
<div class="col-sm-6">
<label class="radio-inline" ng-repeat="option in entity.fields">
<input type="radio" ng-model="data" name="option.name" value="{{option.id}}">
{{option.name}}
</label>
</div>
</div>
<br />
<button type="submit" id="submit" ng-click="save(data)">Submit</button>
<br />
</form>
controller
routerApp.controller('DynamicController2', ['$scope', function ($scope) {
// we would get this from the api
$scope.entity = {
name: "my favorite fruits",
fields:
[{ id: 1, name: "orange" }, { id: 2, name: "pink" }, { id: 3, name: "gray" }, { id: 4, name: "cyan" }]
};
$scope.save = function (data) {
alert(data);
}
}]);
The variable 'data' used in ng-modal is not defined in the controller. You need to define 'data' variable as $scope. Then there's no need to pass the variable to the function save.
here how your controller should be
routerApp.controller('DynamicController2', ['$scope', function ($scope) {
// we would get this from the api
$scope.data;
$scope.entity = {
name: "my favorite fruits",
fields:
[{ id: 1, name: "orange" }, { id: 2, name: "pink" }, { id: 3, name: "gray" }, { id: 4, name: "cyan" }]
};
$scope.save = function () {
alert($scope.data);
}
}]);
And your button in html should be like :
<button type="submit" id="submit" ng-click="save()">Submit</button>
Hopefully this will solve your problem.
I am trying to generate a HTML form with angularjs ngRepeat. I load a JSON file that contains the fields of desired form and save this object in my $scope. I have a ngRepeat element that generates form inputs based on the JSON object.
The problem is when I print $scope.<myFormName> it just have one field name "{{header.name}}" as I expect some fields based on my JSON model.
Actually I see the form generated in HTML properly but the same thing does not happen in angular scope.
angular.module("myApp",[])
.controller("myCtrl",["$scope",function($scope){
// In this funtion I Set fields of form and make my model empty
$scope.getHeaders = function () {
console.info('getHeaders');
$scope.headersForCreation = [
{
name: 'givenName',
type: 'string',
caption: "nameAndFamily"
},
{
name: 'userName',
type: 'string',
caption: "userName"
},
{
name: 'password',
type: 'password',
caption: "password"
},
{
name: 'passwordConfirm',
type: 'password',
caption: "passwordConfirm"
},
{
name: 'mail',
type: 'string',
caption: "email"
},
{
name: 'mobile',
type: 'string',
caption: "mobile",
regex: '0[1-9][0-9]{9}'
}
];
$scope.userInputsInCreation = {};
angular.forEach($scope.headersForCreation, function (headerItem, key) {
$scope.userInputsInCreation[headerItem.name] = '';
});
}; // end of get header
$scope.getHeaders();
console.log($scope.createForm)
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl" id="add-container-access" ng->
<form class="form-horizontal" name="createForm" init="getHeaders()">
<div class="form-group" ng-repeat="header in headersForCreation" >
<label for="id-{{header.name}}" class="col-sm-4 control-label">
{{header.caption}}: </label>
<div class="col-sm-8"
ng-class="{'has-error': createForm.{{header.name}}.$error.$invalid }">
<input type="header.type" class="form-control"
name="{{header.name}}" ng-required="{{header.notNull}}"
id="id-{{header.name}}"
ng-model="userInputsInCreation[header.name]" >
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<button class="btn btn-default" >
ADD
<i class="glyphicon glyphicon-plus-sign"></i>
</button>
</div>
</div>
</form>
</div>
Because your controller initialized before AngularJS form directive.
angular.module("myApp",[])
.controller("myCtrl",["$scope","$timeout",function($scope, $timeout){
// In this funtion I Set fields of form and make my model empty
$scope.getHeaders = function () {
console.info('getHeaders');
$scope.headersForCreation = [
{
name: 'givenName',
type: 'string',
caption: "nameAndFamily"
},
{
name: 'userName',
type: 'string',
caption: "userName"
},
{
name: 'password',
type: 'password',
caption: "password"
},
{
name: 'passwordConfirm',
type: 'password',
caption: "passwordConfirm"
},
{
name: 'mail',
type: 'string',
caption: "email"
},
{
name: 'mobile',
type: 'string',
caption: "mobile",
regex: '0[1-9][0-9]{9}'
}
];
$scope.userInputsInCreation = {};
angular.forEach($scope.headersForCreation, function (headerItem, key) {
$scope.userInputsInCreation[headerItem.name] = '';
});
}; // end of get header
$scope.getHeaders();
$timeout(function(){ console.log($scope.createForm); });
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl" id="add-container-access" ng->
<form class="form-horizontal" name="createForm" init="getHeaders()">
<div class="form-group" ng-repeat="header in headersForCreation" >
<label for="id-{{header.name}}" class="col-sm-4 control-label">
{{header.caption}}: </label>
<div class="col-sm-8"
ng-class="{'has-error': createForm.{{header.name}}.$error.$invalid }">
<input type="header.type" class="form-control"
name="{{header.name}}" ng-required="{{header.notNull}}"
id="id-{{header.name}}"
ng-model="userInputsInCreation[header.name]" >
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<button class="btn btn-default" >
ADD
<i class="glyphicon glyphicon-plus-sign"></i>
</button>
</div>
</div>
</form>
</div>
Below is my data source which needs to be binded to two kendo drop down list. One is for Program and another for IForm. I need the filteration to be done in the dropdown itself.
$scope.programIFormList = new kendo.data.DataSource({
data: [
{
Name: "Program1",
IsProgram: "true",
ProgramId: 1,
Status: 0,
PatientId: 1,
StartDate:"",
EndDate:""
},
{
Name: "Program2",
IsProgram: "false",
ProgramId: 2,
Status: 0,
PatientId: 1,
StartDate:"",
EndDate:""
}
]
});
Below is html code:
<div class="col-lg-2 col-md-4 col-sm-4 col-xs-4">
<select id="patient" kendo-drop-down-list k-data-text-field="'Name'" k-data-value-field="'PatientId'" k-data-source="patientList" >
</select>
</div>
<div class="col-lg-2 col-md-4 col-sm-4 col-xs-4">
<select kendo-drop-down-list k-data-text-field="'Name'" k-data-value-field="'ProgramId'" k-data-source="programIFormList | filter:{IsProgram: true}" k-cascade-from="'patient'">
</select>
</div>
<div class="col-lg-2 col-md-4 col-sm-4 col-xs-4">
<select kendo-drop-down-list k-data-text-field="'Name'" k-data-value-field="'ProgramId'" k-data-source="programIFormList | filter:{IsProgram: false}" k-cascade-from="'patient'">
</select>
</div>
Here, I am trying to apply angular filter. But it's not working. How can this be achieved using Kendo Angular?
You can use this approach
angular.module("MyApp", [ "kendo.directives" ])
.controller("FilterController", function($scope){
$scope.programIFormList = new kendo.data.DataSource({
data: [
{
Name: "Program3",
IsProgram: "true",
ProgramId: 1,
Status: 0,
PatientId: 1,
StartDate: "",
EndDate: ""
},
{
Name: "Program1",
IsProgram: "false",
ProgramId: 2,
Status: 0,
PatientId: 1,
StartDate: "",
EndDate: ""
}
],
filter: [
{ field: "IsProgram", operator: "eq", value: "true" }
]
});
});
How to binding select with radio buttons in ng-repeat?
I wona get tax from selected radio button in inner select.
When user picks town and price, radio button must be changed accordingly.
Help!
jsfiddle.net/hanze/pqh4eq96
html
<h1>Select </h1>
<div ng-app="" ng-controller="OrderCtrl">
<div class="radio" ng-repeat="delivery in deliveries">
<label>
<input type="radio" name="radioDelivery" ng-value="delivery" ng- model="$parent.selectedDelivery">
{{delivery.name}}. {{delivery.desc}}
<select>
<option ng-repeat="tax in delivery.tax" ng-value="tax" ng- model="$parent.selectedTax">{{tax.town}} {{tax.price}} $ </option>
</select>
</label>
</div>
<br>Delivery: {{selectedDelivery.name}}
<br>Tax delivery: {{ }} /// ??</div>
js
OrderCtrl = function ($scope) {
$scope.deliveries = [{
name: "RussianPOST",
tax: [{
town: "Moscow",
price: 10,
}, {
town: "Izhevsk",
price: 30,
}]
}, {
name: "DHL",
tax: [{
town: "Moscow",
price: 50,
}, {
town: "Izhevsk",
price: 100,
}]
}
];
$scope.selectedDelivery = $scope.deliveries[0];
}
In your script:
OrderCtrl = function ($scope) {
$scope.deliveries = [{
name: "RussianPOST",
tax: [{
town: "Moscow",
price: 10,
}, {
town: "Izhevsk",
price: 30,
}]
}, {
name: "DHL",
tax: [{
town: "Moscow",
price: 50,
}, {
town: "Izhevsk",
price: 100,
}]
}
];
$scope.selectedDelivery = $scope.deliveries[0];
$scope.TaxSelect=function(tax){
$scope.selectedTax = tax;
}
}
Your option (I am not sure if ng-model is needed here, but i am letting it stay):
<option ng-repeat="tax in delivery.tax" ng-value="tax" ng-model="$parent.selectedTax" ng-click="TaxSelect(tax)">{{tax.town}} {{tax.price}} $</option>
Your display:
Tax delivery: {{ selectedTax.town}}, {{ selectedTax.price}}
Fiddle DEMO
i wona get price and name of chosen radio buttons. its easy with simple html tags.
But I stacked when i trying generate radio buttons via angularjs from array (items)
Help!
http://jsfiddle.net/hanze/j9x23apu/
html
<h1>Select </h1>
<div ng-app="" ng-controller="OrderCtrl">
<div ng-repeat="item in items">
<div class="radio">
<label>
<input type="radio" name="item" ng-model="item" ng-checked="{{item.checked}}">
{{item.name}} +{{item.price}} $.</label>
</div>
</div>
Your choice: {{}} **what i must write here?**
<br>
Price: {{}} **and here?**
</div>
js
OrderCtrl = function ($scope) {
$scope.items = [{
name: 'None',
value: "no",
price: 0,
checked: true
}, {
name: 'Black',
value: "black",
price: 99,
checked: false
}, {
name: 'White',
value: "white",
price: 99,
checked: false
}, {
name: 'Barhat',
value: "barhat",
price: 49,
checked: false
}, {
name: 'Barhat',
value: "cream",
price: 49,
checked: false
}]
You can look at the angularjs documentaion about radio buttons here. You don't need to use ng-checked here. Use ng-value to set the value when redio is selected.
I changed your jsfiddle post.
Your are missing the closing tag for your input. And when you have ng-repeat you have a seperate scope. In this case you need $parent.selectedItem to hold your selected elements.
I have updated the JSFiddle with a working state solution.
<div ng-app="" ng-controller="OrderCtrl">
<div ng-repeat="item in items" style="text-indent: 30px">
<input type="radio" name="itemRadio" ng-model="$parent.selectedItem" ng-value="item.name"/>
{{item.name + '-' + item.price}}$.
</div>
Your choice: {{selectedItem}}
</div>
jsFiddle: http://jsfiddle.net/j9x23apu/16/
html
<div ng-app="" ng-controller="OrderCtrl">
<div ng-repeat="item in items" style="text-indent: 30px">
<label>
<input type="radio" name="item" ng-checked="{{item.checked}}" ng-click="change_item($event)" item-name="{{item.name}}" item-price="{{item.price}}" /> {{item.name}} + {{item.price}}
</label>
</div>
Your choice: {{selectedName}}
<br />
Price: {{selectedPrice}}
</div>
js
OrderCtrl = function ($scope) {
$scope.change_item = function(e) {
$scope.selectedName = e.target.attributes['item-name'].value;
$scope.selectedPrice = e.target.attributes['item-price'].value;
};
$scope.items = [{
name: 'None',
value: "no",
price: 0,
checked: true
}, {
name: 'Black',
value: "black",
price: 99,
checked: false
}, {
name: 'White',
value: "white",
price: 99,
checked: false
}, {
name: 'Barhat',
value: "barhat",
price: 49,
checked: false
}, {
name: 'Barhat',
value: "cream",
price: 49,
checked: false
}]
}