I have a contact form that doesn't use a model. What's the best way to validate its data?
Should I use cake's automagic goodness and have it create a dummy model on the fly a pre-defined dummy model that validates the data according to the rules I add on the fly or is there a better way?
From my experience, it has always been that you create a model for situations like these, and then set:
var $useTable = false;
in the model. You don't have to do it "on-the-fly". It's just a model with validation rules, without an associated table.
Here's a link that should help you with this.
Related
My problem is that I'm using noSql for my database and Laravel for the backend.
I know that we can validate the data types in the request but this validates only the incoming request after that we might calculate or re-arrange the data type of a field and insert it. I want to prevent this insertion by checking the data types. Is there a way to implement this validation in model like fillable [] or casts [].
You could setup an Observer on that model and in the Observer's 'updating' method you could perform any type formatting or cleanup required.
Or if it's a real edge case you could call the Validator facade to define the keys/rules to check for and then pass your changed object to it? If Validator->fails() you could loop through Validator->errors() and apply corrections as needed.
I have a form that accepts data which will be used to create two new database table entries. The form takes both the users details and their address. The user details will be stored using the User::create(Input::all()) method into the users table, and the address details will be stored using the Address::create(Input::all()) method into the addresses table of the database.
The issue I'm currently having is that Eloquent is complaining that street, city, country etc do not exist on the users table. This is true, that data is to be used for the address side of things.
Is there any way to have eloquent ignore irrelevant data in the Input::all() array when it's passed to the create methods?
P.s. I'm aware that mass-assignment isn't a good idea, I'm only using it here to simplify my question.
Sure enough you can use $fillable array in your model to declare fields allowed for mass-assignment. I believe this is the most sufficient solution in your case.
class User extends Eloquent {
protected $fillable = [
'first_name',
'last_name',
'email'
];
}
Have you tried looking at Input::only('field1','field2',...);, or even Input::except('field3')? They should be able to accomplish what you are looking for.
Source: http://laravel.com/docs/requests
You'll have to unguard that model using these http://laravel.com/docs/eloquent#mass-assignment and then manually unset those values before you execute save(). I highly recommend using a form object or something similar to complete this kind of service for you outside of your model since it's safer and usually clearer to intended behavior.
#cheelahim is correct, When passing an array to Model::create(), all extra values that aren't in Model::fillable will be ignored.
I would however, STRONGLY RECOMMEND that you do not pass Input::all() to a model. You really should be validating and verifying the data before throwing it into a model.
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.
I have a question about Jamie Rumbelow's MY_Model and models generally. MY_Model provides a protected variable that holds the name of the table. I want to use it but I want my model to handle 3 tables. So I guess my question is can a model handle more than one table? is it good practice to do this or is it better to have a model per database table?
By default, MY_Model doesn't support multiple tables, however, you can very easily create methods - I like to call them scopes - to link to other tables in an efficient and elegant manner.
Let's say we have a post_model.php that needs to pull in data from the categories table. I'll assume that we want to bring in our category.name based on the post.category_id.
class Post_model extends MY_Model
{
public function with_category()
{
$this->db->join('categories', 'categories.id = post.category_id', 'left');
$this->db->select('categories.name AS category_name');
return $this;
}
}
We can then use our with_category() method (chained alongside all our built-in MY_Model methods) to pull out our category info:
$this->post_model->with_category()
->get_all();
Or with get_by():
$this->post_model->with_category()
->get_by('status', 'open');
Scoping is a cool way of introducing other tables into your models, while still getting to use all the cool stuff MY_Model provides.
If you wish to use Jamie Rumbelow's MY_Model untouched, you have to use only one table for each model, as it gets the table name from the model name. As he introduced it, this is a base CRUD model, you can extend it to fit your situation.
I think the best practice is to use one table per model (not including the join tables if there are any). Although I sometimes skip this in CodeIgniter if some stuff can be added to the same model logically and are not too big to need their own model. For example there is a comment model and you need votes only for the comments. I do this out of laziness - I hate the manual model loading in CI.
I had created a custom model which uses ORM, i tried out "Weblog" example in the link http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics and i was able to retrieve values from db using collection, but i cannot use functions like "->addAttributeToSort('name', 'ASC');" with my collection object.
Are these functions specific only to product model such as "catalog/product"? if so how could i use such filtering capabilities to my custom function?
Thank you very much..!!
Those functions are specific to EAV models. Otherwise you are limited to the 'field' equivalents like addFieldToFilter() and addOrder().
To make your custom model use an EAV resource read the rest of that tutorial, especially part 7.
you can use functionally of setOrder('$attribute','ASC')
to reorder