Castle Validators how to Validate Is Lesser/Greater Than Or Equal - castle-validators

I see the ValidateIsLesser and ValidateIsGreater attributes. But what if I want to do a ValidateIsLesserOrEqual and/or ValidateIsGreaterOrEqual. Do I just need to write these custom attributes or is there pre-built capabilities for this that I am having a hard time finding?
[ValidateNonEmpty]
[ValidateDate]
[ValidateIsLesser(IsLesserValidationType.Date, "EndDate", "Start Date must be before End Date.")]
public DateTime StartDate { get; set; }
[ValidateNonEmpty]
[ValidateDate]
[ValidateIsGreater(IsGreaterValidationType.Date, "StartDate", "End Date must be after the Start Date.")]
public DateTime EndDate { get; set; }

You can use the ValidateSelf attribute and supply your own validation for the OrEqual comparisons:
[ValidateNonEmpty]
[ValidateDate]
public DateTime StartDate { get; set; }
[ValidateNonEmpty]
[ValidateDate]
public DateTime EndDate { get; set; }
[ValidateSelf]
public void ValidateDate(ErrorSummary errors)
{
if (StartDate >= EndDate)
errors.RegisterErrorMessage("StartDate", "Start date must be earlier than end date.");
}

Related

Adding [ResultColumn] to Database.tt in PetaPoco

I am an avid user of PetaPoco. Is there any way to tweak the Database.tt (for generation of POCO's) to specify a ResultColumn in a specific table?
TIA
Currently, the Database.tt states:
// Tweak Schema
tables["tablename"].Ignore = true; // To ignore a table
tables["tablename"].ClassName = "newname"; // To change the class name of a table
tables["tablename"]["columnname"].Ignore = true; // To ignore a column
tables["tablename"]["columnname"].PropertyName="newname"; // To change the property name of a column
tables["tablename"]["columnname"].PropertyType="bool"; // To change the property type of a column
I do not know how to change the template, other then these instructions (which work very well). I was hoping for a similar statement that could produce a POCO like:
[TableName("phoenix.view_medical_records")]
[ExplicitColumns]
public partial class view_medical_records
{
[Column] public string lastname { get; set; }
[Column] public string firstname { get; set; }
[Column] public string birthdate { get; set; }
[Column] public int? chart_number { get; set; }
[ResultColumn] public DateTime tservice { get; set; }
[Column] public string status { get; set; }
[ResultColumn] public DateTime tcompleted { get; set; }
[Column] public string procedure_description { get; set; }
[Column] public string description { get; set; }
[Column] public string provider { get; set; }
}
Note: the [ResultColumn] attribute being automatically supplied?!
Thanks.
As per my comment on the question, PetaPoco doesn't support result columns via the T4 generator files. However, a workaround would be to ignore the columns
tables["phoenix.view_medical_records"]["tservice"].Ignore = true;
tables["phoenix.view_medical_records"]["tcompleted"].Ignore = true;
And, supply partial classes for the generated one which supply the columns.
public partial Poco1
{
// Generated by PP
}
public partial Poco1
{
// Supplied by the developer (Must be in same namespace)
[ResultColumn] public DateTime tservice { get; set; }
[ResultColumn] public DateTime tcompleted { get; set; }
}

WCF error parameter date equals "01/01/0001"

I am currently sending from SoapUI for my tests the following request:
<caja:GenerarObligacion>
<caja:Obligacion>
<obl:BenCheq>TALLERES ARROYO</obl:BenCheq>
<obl:CodMoneda>S/.</obl:CodMoneda>
<obl:CodProd>VM14</obl:CodProd>
<obl:CodRamo>AUTO</obl:CodRamo>
<obl:CodUsuario>ACSELP</obl:CodUsuario>
<obl:DptoEmi>000101</obl:DptoEmi>
<obl:FecFinVig>2024-02-28T12:00:01</obl:FecFinVig>
<obl:FecIniVig>2025-02-28T12:00:01</obl:FecIniVig>
<obl:FecMod>2026-07-17T13:02:05</obl:FecMod>
<obl:FecNotif>2026-07-17T13:02:05</obl:FecNotif>
<obl:FecOcurr>2025-01-28T08:41:53</obl:FecOcurr>
<obl:FecRecepDocPago>2025-01-28T00:00:00</obl:FecRecepDocPago>
<obl:IdePol>25000293961</obl:IdePol>
<obl:IdeSin>1500013900</obl:IdeSin>
<obl:IndCheque>S</obl:IndCheque>
<obl:ItemOblig>
<fac:BEDetalleObligacion>
<fac:CodCpto>DSCTOS</fac:CodCpto>
<fac:ItemDoc/>
<fac:FecFact>2025-01-28T00:00:00</fac:FecFact>
<fac:MtoCptoEgre>600.0</fac:MtoCptoEgre>
<fac:NroFact>444444444</fac:NroFact>
<fac:NumCpto>1</fac:NumCpto>
<fac:SerieFact>f045</fac:SerieFact>
<fac:TipoRegCompra>01</fac:TipoRegCompra>
</fac:BEDetalleObligacion>
<fac:BEDetalleObligacion>
<fac:CodCpto>ISLV</fac:CodCpto>
<fac:ItemDoc/>
<fac:FecFact>2025-01-28T00:00:00</fac:FecFact>
<fac:MtoCptoEgre>108.0</fac:MtoCptoEgre>
<fac:NroFact>444444444</fac:NroFact>
<fac:NumCpto>2</fac:NumCpto>
<fac:SerieFact>f045</fac:SerieFact>
<fac:TipoRegCompra>01</fac:TipoRegCompra>
</fac:BEDetalleObligacion>
</obl:ItemOblig>
<obl:MtoAprob>708.0</obl:MtoAprob>
<obl:NumAprob>18000493</obl:NumAprob>
<obl:NumPol>2500029396</obl:NumPol>
<obl:StsOblig>VAL</obl:StsOblig>
<obl:StsSin>ACT</obl:StsSin>
<obl:TasaCambio>1.0</obl:TasaCambio>
<obl:TipoAprob>P</obl:TipoAprob>
<obl:TipoBenef>TALR</obl:TipoBenef>
<obl:TipoDocPago>CHQ</obl:TipoDocPago>
</caja:Obligacion>
<caja:Tercero>
<ter:BETercero>
<ter:NumId>650606871</ter:NumId>
<ter:TipoId>J</ter:TipoId>
</ter:BETercero>
</caja:Tercero>
</caja:GenerarObligacion>
All the data arrives well except for the "FecFact" field, as seen in the request this field if it has a date but when arriving at the visual debug the following value "01/01/0001" is displayed:
In spite of sending a value to the FecFact field, it is set with the date that is displayed in the image "01/01/0001" as if the field were arriving null. This is the declaration of my entity.
[DataMember]
public String CodCtpo { get; set; }
[DataMember]
public DateTime FecFact { get; set; }
[DataMember] //IndIGV
public String IndnoGravado { get; set; }
[DataMember] //MtoCptoAprob
public Decimal MtoCptoEgre { get; set; }
[DataMember] // NumCpto - ?uso? NUMDETOBLIG
public Int32 NumCpto { get; set; }
[DataMember]
public String NroFact { get; set; }
[DataMember]
public String SerieFact { get; set; }
[DataMember]
public String TipoRegCompra { get; set; } //Funcional como OB
[DataMember]
public String CodSvcio { get; set; }
DateTime is initialized by default by DateTime.MinValue, which is actually 1/1/0001 12:00:00 AM exactly same as what you are getting.
You should declare nullable field if want null Date like:
DateTime? dt = null;
OR
Compare DateTime variables to DateTime.MinValue & set corresponding value in database query as null or in UI variable as null.
According to your description, I have restored your problem successfully,
The only thing you need to do is try to assign the NameSpace attribute to the DataContract Class.
[DataContract(Namespace ="mydomain")]
public class Product
{
[DataMember]
public int ID { get; set; }
[DataMember]
public DateTime dateTime { get; set; }
}
I hope this information is useful.

Format list elements in viewmodel

In my viewmodel I have SubscriptionExpires formatted as below
public class IndexViewModel
{
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dddd d MMMM yyyy}")]
public DateTime? SubscriptionExpires { get; set; }
public IEnumerable<Device> Devices { get; set; }
}
Device also includes a date property
public partial class Device
{
[Key]
public int DeviceId { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
public string DeviceName { get; set; }
public DateTime UnlockedTo { get; set; }
}
Is there any way of setting the date format for UnlockedTo in IndexViewModel or must formatting for this property be done in the view?
View should be responsible for formatting and showing data. Model is responsible just to deliver those data.

MVC4: DateTime column sorting in asp.net mvc webgrid

I have been trying to sort DateTime column in asp.net mvc webgrid.
Here is my code:
grid.Column(header: "Product Purchased Date",columnName:"PurchasedDate" canSort: true,format: #<text>
#item.PurchasedDate.ToString("MM/dd/yyy")</text>
when i tried using above code i am getting this errors:
cannot convert from 'lambda expression' to
'System.Func'
The best overloaded method match for System.Web.Helpers.WebGrid.Column(string, string, System.Func, string, bool)' has some invalid arguments.
if i apply the above code without using columName i am unable to perform sorting.
Apologize if i missed anything.
I'd recommend that, if you want to sort it as a string, add a function within your Model object that returns the value as a string instead of converting it inline. So there would be a field such as PurchasedDateAsString on the object.
For instance this is something I used in a recent project. The field DateRange is returned as a string and can be sorted upon just like it was a "base" field:
public class PeriodView
{
[Key]
public int PeriodId { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int Year { get; set; }
public string Month { get; set; }
public string DateRange
{
get
{
var start = String.Format("{1}-{0}",
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(StartDate.Month).Substring(0, 3),
StartDate.Day);
var end = String.Format("{1}-{0}",
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(EndDate.Month).Substring(0, 3),
EndDate.Day);
return String.Format("{0} to {1}", start, end);
}
}
}
If you convert DateTime to string and format it, the WebGrid will sort the column as a string, which is not correct for DateTime Format Column expected result.

Using the NHibernate QueryOver, how can you add a type-safe restrictions between dates

Considering the following QueryOver (quarter and centre are variables passed in):
QueryOver.Of<Activity>()
.Where(Restrictions.On<Activity>(a => a.StartDate).IsBetween(quarter.StartDate).And(quarter.EndDate) ||
Restrictions.On<Activity>(a => a.EndDate).IsBetween(quarter.StartDate).And(quarter.EndDate) ||
Restrictions.And(Restrictions.Lt("StartDate", quarter.StartDate), Restrictions.Gt("EndDate", quarter.EndDate))) //TODO: Refactor this to remove magic strings
.And(a => a.Centre == centre)
.OrderBy(a => a.Title).Asc;
This query works perfectly, but I'd like to change the following restriction to remove the magic strings:
Restrictions.And(Restrictions.Lt("StartDate", quarter.StartDate), Restrictions.Gt("EndDate", quarter.EndDate))
Here are the entities (snipped for brevity):
public class Quarter
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public class Activity
{
public string Title { get; set; }
public Centre Centre { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public class Centre
{
public string Name { get; set; }
}
So what is the type-safe way using the new QueryOver extensions that will enable me to remove these magic strings?
This is what you want:
Restrictions.And(
Restrictions.Lt(Projections.Property<Activity>(x => x.StartDate),
quarter.StartDate),
Restrictions.Gt(Projections.Property<Activity>(x => x.EndDate),
quarter.EndDate)))
Sidenote: property names are not magic strings.