cakephp how to create data validation for hasMany model? - cakephp-2.1

My user hasMany address. I wish to collect all the address from same view file.
So i named the fields as [model][0][field_name], [model][1][field_name] .... , [model][n][field_name].
I use saveMany function to add all the data at once.
The validation code i created in model works for only [model][0] fields alone.
How to make the data validation for all the set of fields?

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.

How to use same model with less fields for another view in mvc5

My Question is not belonging to how to use two models on same razor view ! basically I have a user table in which i have fields like(userid,name,email,password,gender,country,department,IsActive) and my form is working fine i am able to insert update and delete i have not use EF , what i did i create the table in sql server and create the model in my model folder , in my view i have put the required field validator for all these columns and they are mandatory to input while inserting or updating.
Now I want to have another view with another controller where i do not want to show all 8 fields instead want to show just these four columns( username,email,gender,IsActive)
when i am using the same model for the other controller and view then it loads the record correctly on index view ,but when i update the required it fires validation error as all my fields are mark as required so it ask to input the rest of four fields value as well.
I have tried to remove these un-necessary fields from model in controller code before saving using Bind([Exclude]"""") but it did not work.
I have tried modelstate.remove("") this approach works fine for all fields but my password field is still throwing validation error . someone says you need to use viewmodel and in viewmodel you have to put both of your model like the full model and small model, I want to ask how my small model would be mapped to my user table (as table["tableName"] this cannot be applied to two models and pointing to same table without primary foriegn key relation .
Share example please i am confused
modelState.Remove("Password")
This remove all model values which are un-necessary but not removing the password field which gives error while updating the
You have required fields missing data or some fields are not null when being saved. What you are trying to do is completely ok. You are basically using a Virtual Model. This is what I do. In the "get", you fix your model and send it to the view. You work with it and then when you submit, you receive the model in the "post", but it is missing several REQUIRED fields. So what you need to do is, retrieve the same record from the database, update the 3 or 4 fields from the view model and then save that model that you retrieved with the new data. This is one way. (VM = view model)
1. send VM to view
2. add data in the view
3. submit
4. receive VM in POST Controller
5. get same record from DB
6. update the particular fields using the VM data
7. save the updated record to database. This is the record you retrieved in 5.
Another way is to have all the missing fields in the model but not showing them in the view. However you need to mention them in the view, otherwise they will not post. so you need to use
#Html.Hidefor( x=> x.NameOfField)
This will ensure that the field is sent back for posting. Any field with a [Required]... is required. You cannot miss it!
How will your small model be mapped to the Database. It will not. You will transfer the data from the small model to the model that is mapped in the database... with ALL the fields. Either you get the missing values from the database or from the view by using Hidefor.
example
dbmodel.Field1 = vm.field1;
dbmodel.Field2 = vm.field2;
dbmodel.Field3 = vm.field3;
I hope this helps

Cakephp one model different form validation

I have a model name "User", their I added a validation for login. But I need to validate registration page also. Fields for both forms are different.
Can someone please tell me how to manage different form validation with 1 model.
You can validate as many fields as you want inside your User model, it does not matter in which View or in which form you input them.
So just add the fields from your registration page to the User's $validate inside your User model.
If all forms share similar fieldnames but require different validation rules you can use:
http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model
If the duplicate fields validate the same on all forms you can just add them all to the Model, it will only validate the ones present on the form.
Remember to NOT use 'required' => true, setting this key to true will make the field always required and it has to be present in the data array even if it's not on your form

Magento: Where would I place that functionality?

I have a Magento form with a button and some fields. The button click calls a controller and in the controller I query the DB (using fetchAll) and create a csv file from the results.
Whats the best place to store the DB action and the CSV file creation? In a model maybe?
Thanks!
I would create a model, a resource model and a resource model collection for the data you need to export. In the collection I would define functionality that queries data from one or many (if necessary) tables . In the model itself I would create a function that fetches that collection and saves it into a CSV file. The controller action would contain only the model initialization and a call to the export function.

Validating unique field with HABTM join table

I'm struggling to work out how to validate a field in Model A that should be unique when combined with a field from Model B.
Here's an example to clarify my question:
Page hasMany SitesPage
SitesPage belongsTo Page, belongsTo Site
Page has a slug field which should be unique across a site. Pages can be attached to any site.
Page.id
Page.slug
SitesPage.id
SitesPage.site_id
SitesPage.page_id
I have a checkUniqueSlug() custom validation method in my Page model but can't validate the slug is unique to the site as the site_id is stored in SitesPage which isn't available in the Page model validate method ($this->data only contains Page model data).
I can't do the validation in the SitesPage model as SitesPage doesn't have a slug field and I can't see the Page post is SitesPage.
How do I create a custom validation to check the slug is unique to the site?
One solution is to move the slug into the SitesPage model but we need all shared pages to have the same slug. i.e. A shared "About Us" page must have an "about_us" slug irrespective of which site the page is attached to.
Another solution is to perform the validation in the controller before I save which would work but that feels wrong as the validation should be done in the model.
As there have been no answers and someone else might be looking for help, here's how I ended up with a solution:
As the controller is the only place where both model data is present in the data array, the call to validate has to be done there.
I offloaded the validation code to the model and called it with the following:
if (!empty($this->request->data)) {
if ($this->Model->specialMultiModelValidate($this->request->data) && $this->Model->save($this->request->data)) {
// model has validated and saved
}
else {
// model has failed to validate and save
  }
}
Hope someone finds this useful.

Resources