Using DataAnnonations constraints on a Data Transfer Object(dto) Model to update a Domain Model - asp.net-core-mvc

i have a question.
i have a form which returns a CustomerDto model when submitted, i want to add some constraints like required, minlength etc to perform validation on the form, but the domain model class(Customer) that the customerDto model would map the form data to when saving does not have these data annotations constraints, is it possible for me to
add these constraints in the CustomerDto model only and perform server side validation with them and then map the CustomerDto model to the Customer model(domain) which do not have these constraints?
I do not want to alter the existing database structure by adding constraints to the Domain model customer class, but i still want to use CustomerDto model class to perform server side validation

Related

The right way to receive related data in Laravel?

Here are some models:
UserModel
SpecializationModel
UserSpecializationModel
I need to recieve authorized user's specialization. I can do that:
$specializations = UserSpecialization::where("user_id", Auth::user()->id)->get();
Also I can do this through the UserModel model using relation hasMany() specializations().
When to use first case and the second?
$specializations = Auth::user()->specializations();
Do I need a model UserSpecializationModel?
In general you don't need UserSpecilizatonModel, in most situations you wont access data directly from that table, you'll either do it through user or specialization model.
Check also https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns for accessing data from pivot table.

CakePHP: Why entity validation is on Table class intead of Entity class?

If the CakePHP validations valids entities, why the rules is set on table class intead Entity class?
Because it's the table class that consumes the entities, hence its the tables responsibility to provide the contract that defines what's valid and what's not.

How to submit a form that has data for 2 models on Spring and let it validate them?

Suppose I have 2 tables: users and preferences.
users table has columns username, password and email.
preferences table has columns contact_me_by_email, contact_me_by_phone_call, contact_me_by_sms.
Each table has it's own model (User and Preferences).
I want to present just one page for users so that when they fill in data in the form that data will be validated and will populate both tables.
I'm able to get all parameters from one form by accessing the Request object and manually performing all validations and proceed according to the results I get.
Also, I can successfully use #Validate annotation to validate models when I present 2 pages for user (each page having just one model - first page contains only data for User model and the second one contains only data for preferences).
I'm struggling to discover a way to present one page only with a form that contains all fields from both models above that could be validated by Spring using the #Validate annotation.
Is that possible? Is there any other annotation I'm not aware of that could do it?
Any help is very much appreciated.
Regards.
So, here is your user mode class :
#Entity
#Table(name = "user")
public class User(){
// Fields of your user annotated with #Column
and fields of your preferences annotated #Transient as below :
#Transient
private String favoriteTvShow;
//getters and setters for both
}
This way you need to submit only one form. Enjoy.

Data Validation in MVC

Suppose i have a 'View' for filling up form for renting a DVD , as per MVC architecture , either of 'Controller' or 'Model', who is supposed to validate the form data?
Thanks
You validation should be in Model section of MVC.
As models have various fields, only models can know what combination of inputs make that model valid. It's not just about whether a field is blank, or the input of that field matches some pattern, but sometimes this is a combination of field inputs, or the model's relationship to other models that determine the valid state.
All 3 are usually involved in the validation process if you follow the typical flow.
The model defines validation attributes such as the required or stringlength attributes. The controller checks the validation state of the model via ModelState.IsValid and makes decisions accordingly. The view may additional provide client-side validation for those same attributes. Don't rely solely on js to validate the form.
My suggestion would be to validate in the view with some form of validation binding, and then again in the model before persisting to any data store.

Many to many checkbox and mvc

I'm using Entity Framework 4 and my model relationships are automatically generated from the lookup table. My models consist of Request and Building. A request can have many buildings, and a building can be associated with many requests. I've found a few posts on how DropDownFor automatically selects an item based on model relationships. But the HtmlHelper CheckBoxFor wants an expression that returns bool. My models don't have a bool indicating checked because it is based on the relationship.
Anyone have tips or experience?
Don't pass your EF models to the view. Define view models which are classes specifically tailored to the needs of a given view. You don't need many-to-many recursive relations in the view. So in the case where you want to generate a checkbox you would have a corresponding boolean property on your view model. It's the controller that would query the repository, fetch the EF models, map them to the view model (this task could be simplified with frameworks such as AutoMapper) and finally pass the view model to the view so that in your view you simply:
#Html.CheckBoxFor(x => x.SomeBooleanProperty)
And if you wanted to have a list of checkboxes then your view model would contain a collection property of some type that will hold the boolean property.

Resources