how to allow text attributes to be empty in Yii model? - validation

I have a model for articles, the article can be written in two languages, English and Arabic.
So there are 2 attributes for each language text, I want the both attributes to be allowed to be empty on create and update, knowing that the attributes type are text not varchar.
How i can write a rule for what i need?

I am not sure what you mean by "..knowing that the attributes type are text not varchar..." but Yii has built in mechanism to define your validation rules.
Refer to the Yii documentation for more details.
#hamed pointed out in his answer how to make fields mandatory. You can add to this by specifying the scenarios under which the validation rules are true. For example.
array('field1, field2', 'required', 'on' => array('create', 'update'));
will apply the 'required' rule only for inserting and updating. On the other hand.
array('field1, field2', 'required', 'except' => array('create', 'update'));
will apply the 'required' rule only for all scenarios except inserting and updating.

http://www.yiiframework.com/doc/api/1.1/CStringValidator
array(
array('text_en','length'),
array('text_ar','length'),
);

When you generate a model from a database table, yii adds something like this inside rule() function:
array('field1, field2, ...', 'required')
This array is based on NOT NULL attribute in the database column. It means, if the database field has NOT NULL property checked, yiis add it in the above array. All you need to do is removing your two fields from above array.

Related

How to set unique for specific field from specific table?

I have this where i have all features from database:
$features = Feature::all();
User can add new additional feature that will be added in this table, but i want to validate so if user enter something that is already in databse to get a message. So name need to be unique. Any suggestion how can i do that?
I tried this but it save it anyway.
$this->validate($request, [
'name' => 'unique:features',
]);
From the docs
unique:table,column,except,idColumn
The field under validation must be unique in a given database table. If the column option is not specified, the field name will be used.
Specifying A Custom Column Name:
'email' => 'unique:users,email_address'
You may need to specify the column to be checked against.
$feauturescheck= Feauture::where('Columname', '=',Input::get('input'))->count();

best way for bulk insertion validation laravel 5

i have problem validating a bulk insertion in laravel 5 the scenario that i have is as following :
a model called department that has many employees when i save a department it may have several employee belongs to it , I'm currently loops the entire employee list and validate each one before insertion is there a way in laravel 5 validator that do this out of the box.
In Laravel 5.2 there's an easy way of taking care of this kind of problems.
Validating array form input fields is much easier in Laravel 5.2. For example, to validate that each e-mail in a given array input field is unique, you may do the following:
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users'
]);
Likewise, you may use the * character when specifying your validation messages in your language files, making it a breeze to use a single validation message for array based fields:
'custom' => [
'person.*.email' => [
'unique' => 'Each person must have a unique e-mail address',
]
],
This info is clearly explained in the docs.
Source: laravel.com
To my knowledge there is not a way to do this in Laravel out of the box. By that I mean there is no method you can call or class you can use that will accomplish your goal.
You can create one though. Abstract the validation out into it's own class and use that.

Can I set a generic yii view string based on model instead of individual widgets

I have set up relationships between several tables. I got the view in tableFoo to look up the Name in tableBar instead of showing barId, by putting the following into the widget:
array( 'label' => $model->Bar->getAttributeLabel('Bar'),
'value' => $model->Bar->Name ),
But I have lots more tables, and I'd prefer to have a single place for setting these things. Is there a way to set anything with a relation to Bar to show Bar->Name? Or do I have to do this separately for all the barIds, bazIds, etc throughout the model?
tldr: Where is a single point of truth for displaying Name instead of ID for all related tables?

Laravel 4 - how to use a unique validation rule / unique columns with soft deletes?

Assume, you are trying to create a new user, with a User model ( using soft deletes ) having a unique rule for it's email address, but there exists a trashed user within the database.
When trying to validate the new user's data, you will get a validation error, because of the existing email.
I made some kind of extra validation within my Controllers, but wouldn't it be nice to have it all within the Model?
Would you suggest creating a custom validation rule?
As I haven't found a clean solution now, I am interessted in how others solved this problem.
You can validate passing extra conditions:
'unique:users,deleted_at,NULL'
This sounds like an issue with your business logic rather than a technical problem.
The purpose of a soft delete is to allow for the possibility that the soft-deleted record may be restored in the future. However, if your application needs uniqueness of email (which is completely normal), you wouldn't want to both create a new user with that email address and be able to restore the old one as this would contravene the uniqueness requirement.
So if there is a soft deleted record with the email address for which you are adding as a new record, you should consider instead restoring the original record and applying the new information to it as an update, rather than trying to circumvent the uniqueness check to create a new record.
This is the best approach
'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL',
It give you this query
select count(*) as aggregate from `users` where `email` = ? and `deleted_at` is null
Laravel offers "Additional Where Clauses".
My url validation rule (from the update model method) looks like this:
$rules['url'] = 'required|unique:pages,url,'.$page->id.',id,deleted_at,NULL';
This means that the url must be unique, must ignore the current page and ignore the pages where deleted_at id not NULL.
Hope this helps.
Your Eloquent model should have the $softDeletes property set. If so, then when you perform a WHERE check, like User::where('username', 'jimbob'), Eloquent will automatically add in the query WHERE deleted_at IS NULL... which excludes soft deleted items.
In laravel 6.2+ you can use
'email' => ['required', Rule::unique('users')->whereNull('deleted_at')]

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

Resources