Running a MVC3 project with DB first model, I have added RequiredAttributes to certain fields using an additional MetaData class.
The model has 3 fields: Color, Number and Email. All fields are required.
On the View, after filling all fields, the Submit button triggers a validation procedure that check if the user selected a color with the right number. If he didn't, a 'confirm' dialog is raised - asking the user if he is sure, if yes, return true and the
form is submitted.
If the user submits the form without the email field filled, an error will appear near the email field, without even running the validation procedure.
Till here all works.
But then I have switched the 'confirm' dialog with a jquery dialog. now this dialog is stateless - the code runs and doesnt 'wait' for teh user's answer, so i had to switch the submit button to a regular button and perform teh submit action from teh javascript,
according to the user's answer on the jquery dialog.
The problem I encounter is that in this scenario, if a field is missing, the submission of the form is not halt and no error is presented to the user. The page just fails later on because of the missing field.
correct, if we want to get some customization in between the submit clicked and submission then we need to handle the submission of form because jquery dialog is not ui thread blocker it will continue to execute
so return false there and the confirm is clicked on dialog there call document.forms[0].submit();
correct, if we want to get some customization in between the submit clicked and submission then we need to handle the submission of form because jquery dialog is not ui thread blocker it will continue to execute
so return false there and the confirm is clicked on dialog there call document.forms[0].submit();
Right, that is what I'm doing, but the problem is if the user didn't fill the email field - no warn will be raised...
In your jquery confirmation dialog, you set the confirm button to submit your form. You should still get all your validation back if something fails the IsValid check
Yes, that is what I have expected but when debugging, the debugger stops in the designer file of the edmx file, on the missing field:
_Email = StructuralObject.SetValidValue(value, false)
saying: "This property cannot be set to a null value."
this is even before getting to the controller's post edit method.
Your database requires the Email property to have a value. Are you not passing a value for Email within your form? Are you naming the Email field differently from what your controller's action is expecting for it's model parameter?
and submit the form only if the above statement is true else just stop and give a proper message. In fact as you mentioned and its very normal, error message is displayed beside the textbox, so your task is done.
and submit the form only if the above statement is true else just stop and give a proper message. In fact as you mentioned and its very normal, error message is displayed beside the textbox, so your task is done.
Great TechFriend, this was exactly what I was missing!
Alon
Member
71 Points
18 Posts
Submitting a form by Javascript jquery dialog does not fire the Required attribute
May 24, 2012 03:45 PM|LINK
Hi,
I didn't thought this is a MVC issue, but I was refer to here from the JScript for the .NET Framework forum...
Running a MVC3 project with DB first model, I have added RequiredAttributes to certain fields using an additional MetaData class.
The model has 3 fields: Color, Number and Email. All fields are required.
On the View, after filling all fields, the Submit button triggers a validation procedure that check if the user selected a color with the right number. If he didn't, a 'confirm' dialog is raised - asking the user if he is sure, if yes, return true and the form is submitted.
If the user submits the form without the email field filled, an error will appear near the email field, without even running the validation procedure.
Till here all works.
But then I have switched the 'confirm' dialog with a jquery dialog. now this dialog is stateless - the code runs and doesnt 'wait' for teh user's answer, so i had to switch the submit button to a regular button and perform teh submit action from teh javascript, according to the user's answer on the jquery dialog.
The problem I encounter is that in this scenario, if a field is missing, the submission of the form is not halt and no error is presented to the user. The page just fails later on because of the missing field.
Any idea?
Thanks.
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Submitting a form by Javascript jquery dialog does not fire the Required attribute
May 24, 2012 03:57 PM|LINK
correct, if we want to get some customization in between the submit clicked and submission then we need to handle the submission of form because jquery dialog is not ui thread blocker it will continue to execute
so return false there and the confirm is clicked on dialog there call document.forms[0].submit();
Alon
Member
71 Points
18 Posts
Re: Submitting a form by Javascript jquery dialog does not fire the Required attribute
May 24, 2012 04:16 PM|LINK
Right, that is what I'm doing, but the problem is if the user didn't fill the email field - no warn will be raised...
JohnLocke
Contributor
3726 Points
800 Posts
Re: Submitting a form by Javascript jquery dialog does not fire the Required attribute
May 24, 2012 04:37 PM|LINK
In your jquery confirmation dialog, you set the confirm button to submit your form. You should still get all your validation back if something fails the IsValid check
$("#confirmDialog").dialog({ modal: true, autoOpen: false, buttons: { "Confirm": function () { $("#formIdName").submit(); }, "Cancel": function () { $(this).dialog("close"); } } });Alon
Member
71 Points
18 Posts
Re: Submitting a form by Javascript jquery dialog does not fire the Required attribute
May 24, 2012 07:24 PM|LINK
Yes, that is what I have expected but when debugging, the debugger stops in the designer file of the edmx file, on the missing field:
JohnLocke
Contributor
3726 Points
800 Posts
Re: Submitting a form by Javascript jquery dialog does not fire the Required attribute
May 25, 2012 12:33 PM|LINK
Your database requires the Email property to have a value. Are you not passing a value for Email within your form? Are you naming the Email field differently from what your controller's action is expecting for it's model parameter?
TechFriend
Participant
955 Points
182 Posts
Re: Submitting a form by Javascript jquery dialog does not fire the Required attribute
May 26, 2012 04:57 AM|LINK
You must check if the page is valid using
$('form').valid()
and submit the form only if the above statement is true else just stop and give a proper message. In fact as you mentioned and its very normal, error message is displayed beside the textbox, so your task is done.
Alon
Member
71 Points
18 Posts
Re: Submitting a form by Javascript jquery dialog does not fire the Required attribute
May 27, 2012 03:22 PM|LINK
Great TechFriend, this was exactly what I was missing!