Validating unique field with HABTM join table - validation

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.

Related

defining routes with parameters in laravel

in Laravel i want to do a page with a search box and a form (the route could be /products)
I want to retrieve information using search box typing id from a db and populate the form.
I request datas with the route for example /products/{id}
But in the controller i use the same function products($Request request) with if the id exists do something if no do other things, or there are two different functions?
Thx
Please go through the Laravel Resource Controllers.
To show list of products i.e. /products, create a index() method. To show a specific product i.e. /product/{id}, create show() method.
Probably is better use the same function, if id exist, return the page with the values filling the form, if not returns the same page but with a message showing that product doesn't exist

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

cakephp how to create data validation for hasMany model?

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?

Modeling a form with an Auto-Suggest Lookup in Backbone.js

I have report UI with a small form at top where the user looks up a person by name using an Auto-Suggest textbox, and I set a hidden ID field when they select one. They then enter a start and end date, and hit submit to load a report below. The report data is fetched using the Person's ID, and the date range as a Backbone route. I can also show the person's name in the report header since I have it from the Auto-Suggest lookup.
The problem is, if someone bookmarks a report (a nice feature to have), I'd like to repopulate the form (which shows the person's name) and the report header.
So, currently I have one route ('id/startdate/to/enddate') that sometimes is triggered by an already populated form model, and sometimes is triggered by a bookmark/refresh and needs to repopulate the form model from route data and server-side data.
How would you model this? I was going to have a model bound to the form:
{ id: 234, name: 'Bill', startDate: '1/1/2011', endDate: '1/1/2012' }
But I am struggling with this idea of sometimes needing to fetch the name and populate the form, and sometimes already having a populated form (and name). Feels like there should be a better design for my Backbone views/models/routes.
You can populate your Model from the router by triggering a message with the name, startDate etc parameters that the Model will listen to.
The same thing can be done in the View that sets the data on the Model. So, regardless of where you get the information from (View, Router), your Model would correctly hold the state.
Then your Form View could listen on changes to its Model, re-rendering itself with pre-filled information on Model change.
Hope it helps.

Saving table relationships with Datamapper ORM

I am new to Object Model Mapping and just read the documentation of the CodeIgniter DataMapper. I have tried the examples and I am quite impressed in how little time one can achive a lot.
But I don't seem to get one problem solved:
Lets say I have two tables: customer and address. A customer can have many addresses but am address only one customer.
Now, I have a registration form where a new customer has to enter his personal data which is stored in the customer table and his address which is stored in the address table.
Validation is done in the specific models. My problem is how can I ensure that the data is valid for both tables prior to saving them to database.
At the moment it is possible that the customer enters the correct data for the customer table which validates and gets saved to db but the address data does not validate and is not saved.
In short: I only want to save the data of any table to db if the data for all tables validates else I want to get the error messages.
Thanks for any help in advance.
First way is to use transactions, second - validate both address and customer data, and if both is valid - store.
It's not clear from the documentation, but you can call the validate() method prior to calling save():
public function test()
{
// Customers require an email address
$cust = new Customer();
$cust->first_name = "Me";
$cust->validate();
if ($cust->error->string)
{
echo $cust->error->string;
// outputs The Email Address field is required.
}
else
{
// validation passed, save customer and related address to db
$cust->save(array($address));
}
}
If you have validation rules defined, you don't need to explicitly call validate(), the validation rules will also be checked when you call save().
In case validation failed, save() will return FALSE, after which you can check $this->valid to see if a validation error caused the save to fail.

Resources