New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yii\widgets\ActiveForm #1350

Closed
vova07 opened this Issue Nov 28, 2013 · 14 comments

Comments

Projects
None yet
10 participants
@vova07
Contributor

vova07 commented Nov 28, 2013

At this moment in ActiveForm is only attribute beforeSubmit, but there no afterSubmit, that usualy is really necessary.
For example we wont disable submit button for validation time request. Now we can't do this without additional JS code.
Code example:

$form = ActiveForm::begin([
    ...
    'beforeSubmit' => new JsExpression('function($form) {
        $form.find(":submit").attr("disabled", true);
        return true;
    }'),
    'afterSubmit' => new JsExpression('function($form) {
        $form.find(":submit").attr("disabled", false);
    }')
    ...
]);

And one more. Will be really nice to add another attribute, like ajaxData (or more ajax settings - just for ajax submit), for additional params that will be sent each time with form data.
In this case we'll not need ajaxButton or something like this for full ajax form send and validation.
One of live example for this case, can be the sortable form items. For these ones we need to send some extra params. To do this we'll need to write own JS for ajax form submit, but with ajaxData we can do it more easy:

$form = ActiveForm::begin([
    'ajaxData' => [
        'ordering' => new JsExpression('$(.myClass).sortable("toArray");')
    ],
]);

Thanx!

@olegchir

This comment has been minimized.

Show comment
Hide comment
@olegchir

olegchir Jan 14, 2014

PR for olegchir@5b53fc8

Add ability to submit forms with ajax, process post-submit hooks and send additional data, excatly as in #1350.

I use http://jquery.malsup.com/form/ (BSD license, pretty mature) as a default implementation of .ajaxSubmit.

It is my first PR for this project, please review this.

$form = ActiveForm::begin(['id' => 'contact-form',
'withAjaxSubmit' => true,

'afterSubmit' => new JsExpression('function($form, data, status, xhr) {
console.log("afterSubmit");
console.log(data);
console.log(status);
console.log(xhr);
}'),

'afterSuccessfulSubmit' => new JsExpression('function($form, data, status, xhr) {
console.log("afterSuccessfulSubmit");
console.log(data);
console.log(status);
console.log(xhr);
}'),

'afterUnsuccessfulSubmit' => new JsExpression('function($form, data, status, xhr) {
console.log("afterUnsuccessfulSubmit");
console.log(data);
console.log(status);
console.log(xhr);
}'),

'immediatelyAfterSubmit' => new JsExpression('function($form) {
console.log("immediatelyAfterSubmit");
}'),

'ajaxData' => [ 'ordering' => new JsExpression('2*2') ],
]);

olegchir commented Jan 14, 2014

PR for olegchir@5b53fc8

Add ability to submit forms with ajax, process post-submit hooks and send additional data, excatly as in #1350.

I use http://jquery.malsup.com/form/ (BSD license, pretty mature) as a default implementation of .ajaxSubmit.

It is my first PR for this project, please review this.

$form = ActiveForm::begin(['id' => 'contact-form',
'withAjaxSubmit' => true,

'afterSubmit' => new JsExpression('function($form, data, status, xhr) {
console.log("afterSubmit");
console.log(data);
console.log(status);
console.log(xhr);
}'),

'afterSuccessfulSubmit' => new JsExpression('function($form, data, status, xhr) {
console.log("afterSuccessfulSubmit");
console.log(data);
console.log(status);
console.log(xhr);
}'),

'afterUnsuccessfulSubmit' => new JsExpression('function($form, data, status, xhr) {
console.log("afterUnsuccessfulSubmit");
console.log(data);
console.log(status);
console.log(xhr);
}'),

'immediatelyAfterSubmit' => new JsExpression('function($form) {
console.log("immediatelyAfterSubmit");
}'),

'ajaxData' => [ 'ordering' => new JsExpression('2*2') ],
]);
@tonydspaniard

This comment has been minimized.

Show comment
Hide comment
@tonydspaniard

tonydspaniard Feb 5, 2014

Contributor

is only attribute beforeSubmit, but there no afterSubmit, that usually is really necessary.

If you look at #1958 you realize that you can control that easily within the beforeSubmit function. Including the additional parameters you wish to add to it.

Contributor

tonydspaniard commented Feb 5, 2014

is only attribute beforeSubmit, but there no afterSubmit, that usually is really necessary.

If you look at #1958 you realize that you can control that easily within the beforeSubmit function. Including the additional parameters you wish to add to it.

@vova07

This comment has been minimized.

Show comment
Hide comment
@vova07

vova07 Feb 6, 2014

Contributor

When I opened this issue, #1958 wasn't merged. But however at the moment Yii2 ActiveForm need improvements at least like in Yii1. Now beforeSubmit will be call just if model validation will pass successful, and to make work that simple example that I write in my first comment we need an extra js code.

I see a really good logic for ActiveForm somethink like that:

  • beforeValidation function for each attr (At the moment it work).
  • afterValidation function for each attr (At the moment it work).
  • beforeValidation function for all attr (I can do some global change. Like in Yii1).
  • afterValidation function for all attr (I just need to know if all is good. Like in Yii1).
  • beforeSubmit that will be call every time before form submit, even if the validation fails.
  • afterSubmit that wiil be call every time after form submit.
    If for some reason we need afterSuccessSubmit and afterFailSubmit maybe it also can be added, but it not so important.

With this 6 function, Yii2 ActiveForm will give all possibility to controll form submit, without needed to write to much extra js code.
I think that for the new FW like Yii2, this simple but really useful functionality is required.

Contributor

vova07 commented Feb 6, 2014

When I opened this issue, #1958 wasn't merged. But however at the moment Yii2 ActiveForm need improvements at least like in Yii1. Now beforeSubmit will be call just if model validation will pass successful, and to make work that simple example that I write in my first comment we need an extra js code.

I see a really good logic for ActiveForm somethink like that:

  • beforeValidation function for each attr (At the moment it work).
  • afterValidation function for each attr (At the moment it work).
  • beforeValidation function for all attr (I can do some global change. Like in Yii1).
  • afterValidation function for all attr (I just need to know if all is good. Like in Yii1).
  • beforeSubmit that will be call every time before form submit, even if the validation fails.
  • afterSubmit that wiil be call every time after form submit.
    If for some reason we need afterSuccessSubmit and afterFailSubmit maybe it also can be added, but it not so important.

With this 6 function, Yii2 ActiveForm will give all possibility to controll form submit, without needed to write to much extra js code.
I think that for the new FW like Yii2, this simple but really useful functionality is required.

@kartik-v

This comment has been minimized.

Show comment
Hide comment
@kartik-v

kartik-v May 26, 2014

Contributor

Just one enhancement request. Thinking loud, if we could have a separate JS event raised whenever a client validation error is encountered (irrespective of if its during field change, blur, keyup, after submit or before submit). This could be useful for extending the ActiveField much easier by hooking up to one single event rather than hooking up to several events. Couple of events could be yii.beforeValidationError and yii.afterValidationError. If this is difficult just raise one event yii.onValidationError.

Could pass in the attribute validation error message and the related event which triggers this, for example:

$("#input-id").on('yii.beforeValidationError', function(e, msg, parentEvent) {
    // do any stuff 
});
Contributor

kartik-v commented May 26, 2014

Just one enhancement request. Thinking loud, if we could have a separate JS event raised whenever a client validation error is encountered (irrespective of if its during field change, blur, keyup, after submit or before submit). This could be useful for extending the ActiveField much easier by hooking up to one single event rather than hooking up to several events. Couple of events could be yii.beforeValidationError and yii.afterValidationError. If this is difficult just raise one event yii.onValidationError.

Could pass in the attribute validation error message and the related event which triggers this, for example:

$("#input-id").on('yii.beforeValidationError', function(e, msg, parentEvent) {
    // do any stuff 
});

@kartik-v kartik-v referenced this issue May 27, 2014

Closed

Validation error #3

@thiagotalma

This comment has been minimized.

Show comment
Hide comment
@thiagotalma

thiagotalma May 27, 2014

Contributor

Hello everybody!
You could help this discussion to rethink the validation Yii2. Today it is too uptight, it could be much better but would need a total change in architecture. Good coders Javascript can greatly help in this matter.
#3410

Contributor

thiagotalma commented May 27, 2014

Hello everybody!
You could help this discussion to rethink the validation Yii2. Today it is too uptight, it could be much better but would need a total change in architecture. Good coders Javascript can greatly help in this matter.
#3410

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Aug 4, 2014

Member

related: 594b578

Member

cebe commented Aug 4, 2014

related: 594b578

@qiangxue

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Sep 5, 2014

Member

@kartik-v Could you elaborate the usage of onValidationError or create a PR about it? This is the only remaining enhancement in this issue. Others are solved by 594b578

Move to 2.0.1

Member

qiangxue commented Sep 5, 2014

@kartik-v Could you elaborate the usage of onValidationError or create a PR about it? This is the only remaining enhancement in this issue. Others are solved by 594b578

Move to 2.0.1

@qiangxue qiangxue closed this Sep 5, 2014

@qiangxue qiangxue reopened this Sep 5, 2014

@qiangxue qiangxue modified the milestones: 2.0.1, 2.0 RC Sep 5, 2014

@kartik-v

This comment has been minimized.

Show comment
Hide comment
@kartik-v

kartik-v Sep 8, 2014

Contributor

Created PR #4955 for the onValidationError event.

Contributor

kartik-v commented Sep 8, 2014

Created PR #4955 for the onValidationError event.

@martin-tk

This comment has been minimized.

Show comment
Hide comment
@martin-tk

martin-tk Oct 1, 2014

Hello,

I have a few input fields on my form but some of them are disabled (via html disabled="disabled" attribute). Those disabled input fields are still validated on client side, and since they're empty, validation results in being unable to submit the form. Disabled inputs aren't submitted so why validate them?

One of the disabled fields is a checkbox. I had to add 'unckeck' => null to its options so the hidden input wouldn't be generated. Perhaps a disabled checkbox should generate a hidden input that's also disabled so it doesn't end up in POST?

martin-tk commented Oct 1, 2014

Hello,

I have a few input fields on my form but some of them are disabled (via html disabled="disabled" attribute). Those disabled input fields are still validated on client side, and since they're empty, validation results in being unable to submit the form. Disabled inputs aren't submitted so why validate them?

One of the disabled fields is a checkbox. I had to add 'unckeck' => null to its options so the hidden input wouldn't be generated. Perhaps a disabled checkbox should generate a hidden input that's also disabled so it doesn't end up in POST?

@tadaszelvys

This comment has been minimized.

Show comment
Hide comment
@tadaszelvys

tadaszelvys Oct 2, 2014

Contributor

@martin-tk your issue is with scenarios, check yii2 guide Input Validation and read about scenarios. You have validation rules for attributes which you disabled, and if you're not excluding those attributes from validation they are still being validated. You could create scenarios or use when in those rules to apply required validation only when conditions are met.

Contributor

tadaszelvys commented Oct 2, 2014

@martin-tk your issue is with scenarios, check yii2 guide Input Validation and read about scenarios. You have validation rules for attributes which you disabled, and if you're not excluding those attributes from validation they are still being validated. You could create scenarios or use when in those rules to apply required validation only when conditions are met.

@qiangxue qiangxue closed this Oct 2, 2014

@cebe cebe added this to the 2.0 GA milestone Oct 3, 2014

@cebe cebe removed this from the 2.0.1 milestone Oct 3, 2014

@mayankbha

This comment has been minimized.

Show comment
Hide comment
@mayankbha

mayankbha Oct 14, 2014

Hello All,

I have a major issue in yii2. Before I was using beforeSubmit method to post form via ajax and manage validation according to it. But yesterday I run composer.phar update command and now its showing error that calling unknown method beforeSubmit and I checked ActiveForm file and this method is gone so I just want to ask that is this methos still occurs or its gone permanent.

And also please suggest me now how to submit form via ajax and how to manage client side validation. Really I am stuck here and unable to find any solution of my problem.

Thanks in advance.

mayankbha commented Oct 14, 2014

Hello All,

I have a major issue in yii2. Before I was using beforeSubmit method to post form via ajax and manage validation according to it. But yesterday I run composer.phar update command and now its showing error that calling unknown method beforeSubmit and I checked ActiveForm file and this method is gone so I just want to ask that is this methos still occurs or its gone permanent.

And also please suggest me now how to submit form via ajax and how to manage client side validation. Really I am stuck here and unable to find any solution of my problem.

Thanks in advance.

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Oct 14, 2014

Member

check the UPGRADE file on what has changed.

Member

cebe commented Oct 14, 2014

check the UPGRADE file on what has changed.

@mayankbha

This comment has been minimized.

Show comment
Hide comment
@mayankbha

mayankbha Oct 15, 2014

I have checked file beforeSubmit and afterSubmit method has been removed from file so now my problem is that how to achieve same thing with new file.

mayankbha commented Oct 15, 2014

I have checked file beforeSubmit and afterSubmit method has been removed from file so now my problem is that how to achieve same thing with new file.

@qiangxue

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Oct 15, 2014

Member

Please check the UPGRADE file which explains this change.

Member

qiangxue commented Oct 15, 2014

Please check the UPGRADE file which explains this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment