yii rules validate from database - php

I have a table of places each place has:
id|name|work_start|work_end
The user can make a reservation in a certain place and he needs to input the time he wants it. I want to validate if the time is between work's start and work's end.
I'm a new to yii, I can do it easily in the controller but I want the best practice to do it. Can I put a rule in the ActiveRecord or is there any other method to do it?

You could use a couple of compare rules
['fromDate', 'compare', 'your_date_attribute' => 'toDate', 'operator' => '<',
'enableClientValidation' => false],
['toDate', 'compare', 'your_date_attribute' => 'toDate', 'operator' => '>',
'enableClientValidation' => false],
see this for some suggestion http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html

Related

Don't make the slug column unique for all the users (authors)

I have the User and Post models in the project. I want to make the slug column from Post unique but not when updating the post. I found this URL https://laravel.com/docs/5.2/validation#rule-unique from one of the answers.
I found a solution from the above URL 'slug' => 'unique:posts,slug,'.auth()->id().',user_id' but this is not exactly what I want. That makes the column not unique only for one user with provided id.
In my case, I want all the admin users to be able to edit editors' or authors' posts without changing the slugs whenever they do.
Validation codes inside update method.
$request->validate([
'title' => 'required|string|max:255',
'slug' => 'required|string|max:255|unique:posts,slug,'.auth()->id().',user_id',
'featured_img' => 'nullable|max:1024|mimes:png,jpg,jpeg',
'excerpt' => 'nullable|string|max:225',
'body' => 'nullable',
]);
Use the Post rather than a User when specifying the criteria for unique.
// include a use statement for Rule
use Illuminate\Validation\Rule;
'slug' => [
'required',
'string',
'max:255',
Rule::unique('posts')->ignore($post->id)
]
Where $post in the above is the $post object you're working with in the update function.
The above obviously doesn't scope the rule to just admins, but you generally have separate routes/controllers for admin related functionality. Alternatively, you can conditionally include the rule.

Is there any edit option in sonata_type_model?

I have a sonata_type_model field which you can see in the screenshot and in the code below. The form allows me to add or remove elements to the multiple model field. Is there any way where I can get a link to edit those models in the dropdown/autocomplete field?
->add('items', 'sonata_type_model', ['multiple' => true, 'by_reference' => false, 'required' => false])
If you want to use only the possibility of a sonata, this is impossible. It is necessary to customize the type. But if you a lucky man, try to add 4th argument ->add('items', 'sonata_type_model', ['multiple' => true, 'by_reference' => false, 'required' => false], array('edit' => 'inline')) This is not work for me, but I don't had any errors. You can try to use sonata_type_model_list if relation is toOne between entities.
To add editing capabilities you should use sonata_type_collection.
The Collection Type is meant to handle creation and editing of model collections. Rows can be added and deleted, and your model abstraction layer may allow you to edit fields inline.
See: Form Types

Yii: validation rules that always apply except one scenario

I know that you can have a validation rule that applies only for one scenario:
array('username', 'exist', 'on' => 'update'),
Now i would like to know if it's possible to do the opposite: a rule that applies everytime except for a given scenrio?
The only solution that is see right now is list all the others scenario, but it's not pretty if we need to add some news scenarios later.
array('username', 'exist', 'on' => array('create', 'search', ...),//all the scenarios except update
As of Yii 1.1.11 you can use except keyword:
array('username', 'exist', 'except' => 'update'),
Take a look at this page. There is a little example there.
Doc link
Work the same way in Yii 2.0.
['username', 'required', 'except' => 'update']
Every key in the array before the name of the validator is a property for the Validator Class itself. You can see the available properties in https://www.yiiframework.com/doc/api/2.0/yii-validators-validator
I know is an old question but every time I forgot that yii2 have an except property in the validator class.
https://www.yiiframework.com/doc/guide/2.0/en/input-validation for more advanced technics

Problem setting custom validation rules in Cakephp

I creating a custom validation rule in my cakephp model.
'article' => array(
'rule' => '/^[a-z0-9#.,&; ]{2,255}$/i',
'required' => true,
'allowEmpty' => false,
'message' => 'Alphabets and numbers only(3,255).'
),
This works fine. But It stops working, throws a error in model, when I add forward slash [/]. I can't understand why forward causes a problem.
I appreciate any help.
Thanks.
As stated in your other question, read about preg_match() patterns in the php manual. This function is used internally in the framework.

How do I exclude records from an associated model in CakePHP?

I'm creating a Q&A application in CakePHP, and I want to exclude my associations in some cases. Imagine the following:
I'm listing all questions on the first page using $this->Question->findAll();. Since I have the following association in my model:
public $hasMany = array('Answer' =>
array('className' => 'Answer',
'order' => 'Answer.created DESC',
'foreignKey' => 'post_id',
'dependent' => true,
'exclusive' => false,
)
);
All answers will get selected at the start page, which is not optimal. How could i do to exclude the answers in this particular method?
Thanks
I quick look at the CakePHP API reveals that you've got an unbindModel method on the Model.
So in you example you can do this:
$this->Question->unBindModel(array('hasMany' => array(’Answer’)))
Alternatively, you can use the Containable behaviour to only select the pieces from MySQL that you require for the current page view.
If you are using CakePHP 1.2 you should think about Containable Behaviour. See http://cakebaker.42dh.com/2008/05/18/new-core-behavior-containable/ for details

Categories