-
Notifications
You must be signed in to change notification settings - Fork 27.4k
$submitted is not set on sub ng-form #10071
Comments
I don't think we should expect the inner |
Hi I create wizard interface, .directive('bkErrorText', function () {
return {
require: '^form',
template: '<span></span>',
restrict: 'E',
link: function ($scope, $element, $attrs, ctrl) {
var inputCtrl = ctrl[$attrs.bkInputName];
if (!inputCtrl) {
throw 'Can\'t find input ' + $attrs.bkInputName + ' into form.';
}
function showErrorText(newValue, oldValue) {
if (inputCtrl.$invalid && (ctrl.$submitted || ctrl.$$parentForm.$submitted)) {
var i, key, e, etext = [], error = inputCtrl.$error;
for (i in error) {
if (error[i]) {
key = 'bkMsg' + angular.uppercase(i.charAt(0)) + i.substr(1);
e = $attrs[key] || (key + ' no message text!');
etext.push(e);
etext.push(' ');
}
}
$element.html(etext.join(''));
$element.removeClass('hidden');
}
else {
if (!$element.hasClass('hidden')) {
$element.addClass('hidden');
$element.html('');
}
}
};
$scope.$watchCollection(function () { return inputCtrl.$error; }, showErrorText);
$scope.$watch(function () {
return inputCtrl.$invalid && (ctrl.$submitted || ctrl.$$parentForm.$submitted);
}, showErrorText);
}
};
})
.directive('bkHasError', function () {
return {
require: '^form',
restrict: 'A',
scope: {
bkHasError: '@',
bkHasErrorClass: '@'
},
link: function ($scope, $element, $attrs, ctrl) {
var cssClass = $scope.bkHasErrorClass || 'has-error',
inputCtrl = ctrl[$scope.bkHasError];
$scope.$watch(function () {
return inputCtrl.$invalid && (ctrl.$submitted || ctrl.$$parentForm.$submitted);
}, function (newValue, oldValue) {
if (newValue) {
$element.addClass(cssClass);
}
else {
$element.removeClass(cssClass);
}
});
}
};
}) <form class="form-horizontal" role="form" novalidate="novalidate" ng-submit="submitForm()">
<tabset>
<tab select="tabs['common']=true" deselect="tabs['common']=false" active="tabs['common']">
<tab-heading ng-class="{'text-danger':tabsValid['common']===false}">
Общи
</tab-heading>
<ng-form name="common" bk-submit-valid="tabsValid['common']">
<div class="form-group" bk-has-error="amountBuy">
<label class="col-md-2 control-label" for="amountBuy">amountBuy</label>
<div class="col-md-3">
<input class="form-control bk-number-lg" id="amountBuy" name="amountBuy" type="text"
ng-model="model.amountBuy"
bk-number="15,2"
bk-number-range="0.01,999999.99"
ng-model-options="{ debounce: 300 }" />
<bk-error-text class="help-block"
bk-input-name="amountBuy"
bk-msg-required="Data is required ..."
bk-msg-number="Number(15,2) ..."
bk-msg-number-range="Number range from 0.01 to 999 999.99 ..." />
</div>
</div>
<!--
others input fields
-->
</ng-form>
</tab>
<!--
others tabs
-->
</tabset>
<div class="col-md-offset-2 col-md-6">
<button class="btn btn-default" type="button" ng-click="prevTab()">Prev</button>
<button class="btn btn-default" type="button" ng-click="nextTab()">Next</button>
<button class="btn btn-primary" type="submit" bk-disable-pristine>Save</button>
</div>
</form> |
…tted. FormController will now call $setSubmitted on its child forms when it is submitted. Closes angular#10071
…tted. FormController will now call $setSubmitted on its child forms when it is submitted. Closes angular#10071
Another approach is using css: .ng-submitted .ng-invalid {
.... styles styles styles ...
} or looping through all parent forms: while (form) form = form.$$parentForm; |
👍 I would like to see that too My rl scenario is that I have a form where I have directive that uses ng-form and inside it I also have another directive with some input fields. That second (most inner) directive requires form controller in link function and it is the form controller from the ng-form directive. I have validation messages showing on submitted form but the only form that gets $submitted flag is the most outer form. The ng-form controller has $submitted set to false and the validation messages are not shown. |
@kwypchlo does the css I posted above solve your issue? |
real life use-case: some form: <form ng-form="form1" ng-submit="save(form1)">
<custom-fieldset model="model.firstname" required></custom-fieldset>
<custom-fieldset model="model.lastname" required></custom-fieldset>
<button>save</button>
</form>
<fieldset ng-form="form">
<input name="name" required ng-model="ctrl.model">
<ng-messages role="alert"
for="(form.$submitted || form.name.$touched) && form.name.$error">
<ng-message when="required">please enter a name</ng-message>
</ng-messages>
</fieldset> |
@stryju This is similar to the use case I had. |
@awerlang nope, I need this functionality in js, inside my directive. The workaround with $$parentForm may work but sure doesn't look elegant :) |
👍 for including this. I have a setup similar to @stryju 's above. I added a watch to the child forms for now to watch the parent form's $submitted property, but It does feel hacky. |
I ran into this issue as well. I have a subform that I reuse in many forms, there it is excluded to refer to parent form name to know if it was submitted or not. For example, when I submit my user form, obviouly my subform for user address is submitted as well. However, userForm.$submitted becomes true, but userForm.addressForm.$submitted stays to false. user-form.html: <form name="userForm" novalidate>
<div ng-class="{ 'has-error' : userForm.$submitted && userForm.firstName.$invalid}">
<label>First name</label>
<input type="text" ng-model="user.firstName" name="firstName" required>
</div>
<div ng-class="{ 'has-error' : userForm.$submitted && userForm.lastName.$invalid}">
<label>Last name</label>
<input type="text" ng-model="user.lastName" name="lastName" required>
</div>
<fieldset ng-form="addressForm" class="address" ng-include="'address/address-fields.html'"></fieldset>
</form> address-fields.html: <div ng-class="{ 'has-error' : addressForm.$submitted && addressForm.postcode.$invalid}">
<label>Postcode</label>
<input type="text" ng-model="address.postcode" ng-required="true" name="postcode">
</div>
<div ng-class="{ 'has-error' : addressForm.$submitted && addressForm.city.$invalid}">
<label>City</label>
<input type="text" ng-model="address.city" ng-required="true" name="city">
</div>
<div ng-class="{ 'has-error' : addressForm.$submitted && addressForm.country.$invalid}">
<label>Country</label>
<input type="text" ng-model="address.country" ng-required="true" name="country">
</div> |
+1 |
Yes please! |
quick workaround I've found somewhere and modified: // sets all children ng-forms submitted (no such default functionality)
function setSubmitted(form) {
form.$setSubmitted();
angular.forEach(form, function(item) {
if(item && item.$$parentForm === form && item.$setSubmitted) {
setSubmitted(item);
}
});
} so ie. instead of scope.form.$setSubmitted(); use: setSubmitted(scope.form); |
A workaround: http://stackoverflow.com/a/30138961/2075423 Note this only works if |
I have thesame issue. I made the following workaround based on awerlang's suggestion... In my directive.
In the view...
I do agree that this feels a bit hacky though, but it works... |
will this issue be resolved before Trump takes office? |
nope... |
"bump"
FYI: This doesn't actually work -- the root Note, Angular doesn't expose a static binding for |
Another workaround I used was a watch:
|
My team got into a situation where we really needed the change from #15778, so what we did was to create a copy of our installed angular.js (say, angular-mod.js), applied the change from the PR and used npm's postinstall to overwrite the original angular.js file in node_modules (a simple Worked like a charm. |
…tted. Closes angular#10071 BREAKING CHANGE: Forms will now set $submitted on child forms when they are submitted. For example: ``` <form name="parentform" ng-submit="$ctrl.submit()"> <ng-form name="childform"> <input type="text" name="input" ng-model="my.model" /> </ng-form> <input type="submit" /> </form> Submitting this form will set $submitted on "parentform" and "childform". Previously, it was only set on "parentform".
…tted. Closes angular#10071 BREAKING CHANGE: Forms will now set $submitted on child forms when they are submitted. For example: ``` <form name="parentform" ng-submit="$ctrl.submit()"> <ng-form name="childform"> <input type="text" name="input" ng-model="my.model" /> </ng-form> <input type="submit" /> </form> Submitting this form will set $submitted on "parentform" and "childform". Previously, it was only set on "parentform". This change was introduced because mixing form and ngForm does not create logically separate forms, but rather something like input groups. Therefore, child forms should inherit the submission state from their parent form.
…tted Closes angular#10071 BREAKING CHANGE: Forms will now set $submitted on child forms when they are submitted. For example: ``` <form name="parentform" ng-submit="$ctrl.submit()"> <ng-form name="childform"> <input type="text" name="input" ng-model="my.model" /> </ng-form> <input type="submit" /> </form> Submitting this form will set $submitted on "parentform" and "childform". Previously, it was only set on "parentform". This change was introduced because mixing form and ngForm does not create logically separate forms, but rather something like input groups. Therefore, child forms should inherit the submission state from their parent form.
Hi,
I have <form> with inner <ng-form>
when form is submitted inner <ng-form>.$submitted is not set to true only parent
The text was updated successfully, but these errors were encountered: