I am not able to validate value in model. I am newbie in this can anyone suggest what am I doing wrong. I am working on REST API with excel file upload and validating single row at a time
Here is my data and load data in model
$data = array(
'firstName' => 'asdjqkw',
'lastName' => '',
'email' => 'ansm@',
'companyName' => 'ddq',
'address_1' => '',
'address_2' => 'aas',
'country' => '',
'state' => 'New Brunswick',
'city' => '87875',
'zip' => '484527',
);
$model->load($data);
if (!$model->validate()) {
$validation = $model->errors;
}
And here is my model rules and attribute labels as I defined required, email and maxlength validation in it but still some values is available still it is sending required validation error for that field
public function rules() {
return [
[['firstName', 'lastName', 'email', 'companyName', 'address_1', 'country', 'state', 'city', 'zip'], 'required'],
['email', 'email'],
[['firstName', 'lastName', 'email', 'companyName', 'address_1', 'country', 'state', 'city', 'zip'], 'string', 'max' => 250],
];
}
public function attributeLabels() {
return [
'firstName' => 'First Name',
'lastName' => 'Last Name',
'email' => 'Email',
'companyName' => 'Company Name',
'address_1' => 'Address 1',
'country' => 'Country',
'state' => 'State',
'city' => 'City',
'zip' => 'Zip',
];
}
And I get validation errors after loading this data
"firstName": [
"First Name cannot be blank."
],
"lastName": [
"Last Name cannot be blank."
],
"email": [
"Email cannot be blank."
],
"companyName": [
"Company Name cannot be blank."
],
"address_1": [
"Address 1 cannot be blank."
],
"country": [
"Country cannot be blank."
],
"state": [
"State cannot be blank."
],
"city": [
"City cannot be blank."
],
"zip": [
"Zip cannot be blank."
]