miguelnc
(miguelnc)
June 13, 2022, 4:01pm
1
Hi , what is the correct sintaxys for write validation rules ?
by example . . .
array(‘first_name, date_update, date_birthdate ,compliance’,‘length’, ‘max’=>1)
OR
array(‘first_name’, ‘date_update’, ‘date_birthdate’ ,‘compliance’,‘length’, ‘max’=>1)
Both sintaxys are valid ?
or for second sintaxys we need a special configuration of PHP version ?
my php configuration:
PHP Version 5.6.40-0+deb8u12
Linux debiandb 4.9.0-18-amd64 #1 SMP Debian 4.9.303-1
Apache 2.0
Yii Version 1.1.13
Note: with the second sintaxys the app show errors, when we change to the first sintaxys all work good !
tri
(tri - Tommy Riboe)
June 13, 2022, 4:12pm
2
Described here
https://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules
Each rule returned by rules()
must be of the following format:
array(‘AttributeList’, ‘Validator’, ‘on’=>‘ScenarioList’, …additional options)
where AttributeList
is a string of comma-separated attribute names which need to be validated according to the rule;
1 Like
rob006
June 13, 2022, 4:15pm
3
miguelnc:
array(‘first_name’, ‘date_update’, ‘date_birthdate’ ,‘compliance’,‘length’, ‘max’=>1)
It should be:
[['first_name', 'date_update', 'date_birthdate' ,'compliance'], 'length', 'max' => 1]
Not that list of attributes is wrapped in array.
miguelnc
(miguelnc)
June 13, 2022, 4:16pm
4
Thank you for the official reference @tri
miguelnc
(miguelnc)
June 13, 2022, 4:17pm
5
thank you @rob006 , this is just like i was thinking . .
array(array('first_name', 'date_update', 'date_birthdate' ,'compliance'), 'length', 'max' => 1)
tri
(tri - Tommy Riboe)
June 13, 2022, 4:41pm
6
rob006
June 13, 2022, 4:50pm
7
In Yii 1.1 you can pass attributes either as array or string (multiple attributes separated by comma). In Yii 2 you should use array (although you can pass string, but it will work only for single attribute, you can’t pass multiple attributes in that way).
1 Like
tri
(tri - Tommy Riboe)
June 13, 2022, 4:58pm
8
Yes, I just looked it up:
if(is_string($attributes))
$attributes=preg_split(’/\s*,\s*/’,trim($attributes, " \t\n\r\0\x0B,"),-1,PREG_SPLIT_NO_EMPTY);