angular schema-form modal....problems with $scope
Asked Answered
C

1

6

Plunker

This plunker allows you to edit rows in a grid. I have created a new method based on RowEditCtrl to insert a new row but am having trouble with the validation.

When I insert a new row, the form is "pristine and valid". In the insert method, I need to call $scope.$broadcast('schemaFormValidate') which will validate the form and form.$valid will be false. Ideally, I would like to call this check from ng-show on the save button so the button does not appear until the form is ok.

The problem is, I don't understand or know how to get the schema-form $scope in this RowEditCtrl method and cannot get the form to be invalid before the user has typed anything.

function RowEditCtrl($modalInstance, PersonSchema, grid, row) {
  var vm = this;
  
  vm.schema = PersonSchema;
  vm.entity = angular.copy(row.entity);
  vm.form = [
    'name',
    'company',
    'phone',
    {
      'key': 'address.city',
      'title': 'City'
    },
  ];
  
  vm.save = save;
  
  function save() {
    // Copy row values over
    row.entity = angular.extend(row.entity, vm.entity);
    $modalInstance.close(row.entity);
  }
}
Choriamb answered 27/9, 2018 at 6:38 Comment(4)
Can you provide more explanation, please?Giuseppinagiustina
In the documentation for schema-form it says you can validate the form at any time by calling $scope.$broadcast('schemaFormValidate'). In the Plunker I gave, I want to validate the form inside the RowEditCtrl function before "save()" is called, however, there is no $scope for the form here and I cannot figure out how to get the scope there or how to call "$broadcastt('schemaFormValidate')" to get the form to validate.Choriamb
You could always validate the form in save method, if you pass the form as a parameter, from the HTML and implement your own validation methods.Giuseppinagiustina
I have actually solved the problem another way but I want to understand how I can make the $broadcast within the scope work.Choriamb
D
0

I was able to access $scope in RowEditCtrl by passing $scope into editRow's $modal.open controller. See my fork of the original the plunkr here: http://next.plnkr.co/edit/NiwF12BLJQyD6Cku

Here are the relevant changes:

function editRow(grid, row) {
    $modal.open({
      templateUrl: 'edit-modal.html',
      controller: ['$scope', 

...

function RowEditCtrl($scope, 

...

function save() {
    console.log($scope.$broadcast('schemaFormValidate'));

When I do this console.log of $scope.$broadcast('schemaFormValidate') within the modal's save function it seems to be working. Or, at least $scope is defined and $broadcast callable. Apologies if this does not address the question well enough, I am not familiar with schemaFormValidate.

Dover answered 5/10, 2018 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.