PDA

View Full Version : [CLOSED] MVC RAZOR VALIDATION help



zwf
Oct 12, 2012, 10:10 AM
Hi there,

I definitely need some examples or even possibilities on how to do
validation with MVC RAZOR. Anything would be nice. In the SVN 2.1
examples there's nothing I could find. So, I take everything related to
this. I have to do a massive validation thing for a client over the weekend.
So, if anyone could help me, I'll send you some german beer ;)

Regards,
Holger
PS: MVC4, RAZOR, EF5, ext.net 2.1 SVN

Daniil
Oct 12, 2012, 11:39 AM
Hi Holger,

Please provide more details about the requirement.

"Validations" sounds too general.

zwf
Oct 13, 2012, 1:36 PM
Hi Holger,

Please provide more details about the requirement.

"Validations" sounds too general.

That's quite true ;) so let's define what I'm thinking about:

I have some fields let's say field1_id, field_2_id and field 3_id that
are simple textfields with these id's.

I want to validate with DirectEvent or at least s.th similar, because I have
to do some database operations in the background (searching, if the value
exists and so on).

So, normal DirectEvent validation looks like the following (I know, that I
shouldn't use control ;)



.Control(c =>
{
c.IsRemoteValidation = true;
c.RemoteValidation.Url = "/Home/is_field3_id_Valid";
c.RemoteValidation.Method = Ext.Net.HttpMethod.POST;
c.RemoteValidation.EventOwner = ValidationEventOwner.Field;
c.RemoteValidation.ValidationEvent = "select";
c.MsgTarget = MessageTarget.Under;
}



And that's what I want to achieve :
I want to pass several values from several textfields or grids or datefields to pass to the logic, that decides,
if the value is 'valid'.
So, I'm quite sure, this is possible with Javascript. To concatenate values with a Javascript function and pass
them to ...... what ? And how ? Or is there a possibility to do it simpler ?



public JsonResult is_field_3_id_Valid(string valueFromField_1_id,string valueFromField_2_id, string valueFromField_3_id)
{
JsonResult result = new JsonResult();
if((valueFromField_1_id == "blaa") && (valueFromField_2_id == "blubbbb") && (valueFromField_3_id == "blaa"))
......................
return result;
}



The other question is, how can I collect all validation warnings in one field.

Regards,
Holger

Daniil
Oct 15, 2012, 8:06 AM
Thank you for the details.

Not sure, but, maybe, you need a FieldContainer with CombineErrors.

Please look at the example.It is on WebForm, but the same is possible on Razor. Just to demonstrate the idea. Try to cause validation of the both fields, you will see a combined error message.

Example

<%@ Page Language="C#" %>

<%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %>

<script runat="server">
protected void CheckField(object sender, RemoteValidationEventArgs e)
{
TextField field = (TextField)sender;

if (field.Text == "Valid")
{
e.Success = true;
}
else
{
e.Success = false;
e.ErrorMessage = "'Valid' is valid value only";
}

System.Threading.Thread.Sleep(1000);
}

protected void CheckCombo(object sender, RemoteValidationEventArgs e)
{
ComboBox combo = (ComboBox)sender;

if (combo.SelectedItem.Value == "2")
{
e.Success = true;
}
else
{
e.Success = false;
e.ErrorMessage = "'Valid' is valid value only";
}

System.Threading.Thread.Sleep(1000);
}
</script>

<!DOCTYPE html>

<html>
<head runat="server">
<title>Remote Validation - Ext.NET Examples</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />

<ext:FormPanel
runat="server"
Title="Remote Validation Form"
MonitorValid="true"
Width="300"
DefaultAnchor="100%"
Height="150">
<Items>
<ext:FieldContainer runat="server" CombineErrors="true" MsgTarget="Under">
<Items>
<ext:TextField runat="server" FieldLabel="TextField" IsRemoteValidation="true">
<RemoteValidation OnValidation="CheckField" />
</ext:TextField>

<ext:ComboBox
runat="server"
AllowBlank="false"
Editable="false"
FieldLabel="ComboBox"
IsRemoteValidation="true">
<RemoteValidation OnValidation="CheckCombo" ValidationEvent="select" EventOwner="Field" />
<Items>
<ext:ListItem Text="Invalid" Value="1" />
<ext:ListItem Text="Valid" Value="2" />
</Items>
</ext:ComboBox>
</Items>
</ext:FieldContainer>
</Items>
<Buttons>
<ext:Button ID="Button1" runat="server" Text="Submit" Disabled="true" />
</Buttons>
<Listeners>
<ValidityChange Handler="#{Button1}.setDisabled(!valid);" />
</Listeners>
</ext:FormPanel>
</form>
</body>
</html>

Daniil
Oct 15, 2012, 9:52 AM
There is also the ModelStateStore class in MVC that you can use. Please look at the example in SVN.
[CODE]Ext.Net.MVC.Examples\Models\Model_State\/CODE]