I don't have any rules for the input fields, (name,place, position) so my model's rules function returns an empty array, but then empty values are getting saved into the database table.
public function rules()
{
return array();
}
Also when I omit rules() function from my model
$model->save()
returns true but DB table get inserted with empty values.
so how can I omit rules() function from my model class ?
So you should set all of them as safe attribute in rules.
public function rules(){
return array
array('id, name, /*list attribute here*/', 'safe')
);
}
You have to define safe attribute in rules.
public function rules(){
return array(
array('id, name, place, postion', 'safe')
);
}
Key Point - Massive Assignment will only be made for fields which have passed some explicit validation rule.
Related
In my Yii2 project, I have related models. Example: A model Customer would have an attribute address_id that relates to another model Address. In the Customer model have the exist validator that checks that the row exists in the address table.
Typically, on create or update, this validation is ignored if address = null. My problem is that sometimes FE would send the address = 0 indicating the absence of an address.
In this case, I need to not only ignore validation, but to set address = null. This can be done beforeSave of course, but I'm trying to check if there's some built-in way through which I can do this
You can use the filter validator for normalization of an input data. For example:
class Customer extends ActiveRecord
{
public function rules()
{
return [
['address_id', 'filter', 'filter' => function ($value) {
return $value == 0 ? null : $value;
}],
// other validators
];
}
}
Suppose I have a field name id and I want the user to enter some integer in the id field but at the same time I want to check that id against the database whether it exists or not. If it exists then it should show some error.
Is there a function to achieve this in Yii 2.0?
If this is from a model, you can have a unique rule to it in the validation rules.
In your model, you have a rules() function:
/**
* #return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('name', 'required'),
array('some_id', 'unique'),
);
}
I get a very unhelpful error when I try and insert a new record in to my db. Does anyone have any idea where to start to solve this error?
user_id
That's all it says. (This is the name of one of my required fields in the table I'm saving to but it's not very descriptive.)
For reference here's my code:
$data = array(
'user_id'=>1,
'post_type'=>'0',
'post_title'=>'blah',
'description'=>'blah');
//it fails on this line
$id = Post::create($data);
Here's what my model looks like:
class Post extends Eloquent{
public static $rules = array(
'user_id'=>'required',
'post_type'=>'required',
'post_title' => 'required|max:25',
'description'=>'required'
);
public static function validate($data){
return Validator::make($data, static::$rules);
}
public function user(){
return $this->hasOne('User', 'id', 'user_id');
}
}
Tried getting rid of all relationships and validation in the model. That didn't work.
This is called mass-assignment in Laravel, what you need to do to get it working on your model is to set a protected array called $guarded to be empty. Add this to your model.
protected $guarded = array();
What this array does, it can prevent attributes to be mass-assigned with an array, if you don't want an attribute to be filled with the Model::create() method, then you need to specify that attribute in the $guarded array.
If instead you want to specify only the fillable attributes, Laravel's Eloquent also provides an array called $fillable where you specify only the attributes that you want to fill via the mass-assigment way.
Further reading:
Mass Assignment:
http://laravel.com/docs/eloquent#mass-assignment
Create Method:
http://laravel.com/docs/eloquent#insert-update-delete
I have a question about Laravel 4 and Validation concept.
I have my mysql table and my model Calass..
We can suppose that we have a field nullable...
If this field is not null how can I validate it? (for example I would an integer value < of my maxvalue)
Unless you add the 'required' rule, validation will always treat fields as optional. Thus, a rule like numeric|max:9000 will be valid if the field is null or empty string, but if it is provided it has to be a number that is smaller than 9000.
To prevent null or empty string from being saved into the database (MySQL will convert this to 0 for an integer field), you can set up a mutator for the attribute. In your model:
public function setNumberAttribute($value)
{
if ($value !== null && is_numeric($value)) {
$this->attributes['number'] = (int) $value;
}
}
http://laravel.com/docs/eloquent#accessors-and-mutators
you can create your validation into your model
Ex:
class User extends Eloquent {
protected $table = 'users';
public $timestamps = false;
public static function validate($input) {
$rules = array(
# place-holder for validation rules
);
# validation code
}
}
for more description about validation laravel book
I have a CActiveRecord model, and I need to change safe attributes list in that model.
I have defined the safeAttributes method inside my model, like the following :
public function safeAttributes()
{
return array(
'name, bio',
);
}
the problem is 'bio' is not being considered in my safe attribute assign. I tried to dump the model safeAttributeNames attribute in my model, and what I've got was completely different from what safeAttributes should return.
Am I doing this in the right way ?
cheers,
Firas
Assuming that you are using Yii 1.0.x then that is the correct way to do it.
If you are using Yii 1.1.x then it's changed. Have another read of the documentation.
public function rules()
{
return array(
array('username, password', 'required'),
array('rememberMe', 'boolean'),
array('password', 'authenticate'),
array('something', 'safe'),
array('someOtherThing', 'unsafe'),
);
}