/var/www/vhosts/ihelp.ro/httpdocs/src/Model/Table
Edit: /var/www/vhosts/ihelp.ro/httpdocs/src/Model/Table/ProfilesTable.php (3594B)
setTable('profiles');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Josegonzalez/Upload.Upload', [
'image_path' => [
'path' => '{DS}webroot{DS}img{DS}uploads{DS}images{DS}{model}{DS}',
'nameCallback' => function ($table, $entity, $data, $field, $settings) {
$ext = pathinfo($data->getClientFilename(), PATHINFO_EXTENSION);
return uniqid() . '_' . rand(1, 9999) . '_' . date('Ymd_Hms') . '.' . $ext;
},
]
]);
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER',
]);
}
/**
* Default validation rules.
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->scalar('first_name')
->maxLength('first_name', 255)
->requirePresence('first_name', 'create')
->notEmptyString('first_name', __('Câmpul este obligatoriu.'))
->add('first_name', [
'length' => [
'rule' => ['minLength', 3],
'message' => 'Prenumele trebuie să aibă cel puțin 3 caractere.',
]
]);
$validator
->scalar('last_name')
->maxLength('last_name', 255)
->requirePresence('last_name', 'create')
->notEmptyString('last_name', __('Câmpul este obligatoriu.'))
->add('last_name', [
'length' => [
'rule' => ['minLength', 3],
'message' => 'Numele trebuie să aibă cel puțin 3 caractere.',
]
]);
$validator->setProvider('upload', \Josegonzalez\Upload\Validation\UploadValidation::class);
$validator
->allowEmptyString('image_path')
->add('image_path', 'isBelowMaxSize', [
'rule' => ['isBelowMaxSize', 1024 * 1024 * 1],
'message' => __('Max 1MB'),
'provider' => 'upload'
])
->add('image_path', 'isAllowedType', [
'rule' => function ($value, $context) {
$allowed = ['image/jpeg', 'image/jpg', 'image/png'];
return in_array($value->getClientMediaType(), $allowed) ? true : false;
},
'message' => __('Accepted formats: .jpg, .jpeg, .png'),
]);
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn(['user_id'], 'Users'), ['errorField' => 'user_id']);
return $rules;
}
/**
* beforeMarshal
*/
// public function beforeMarshal(Event $event, ArrayObject $data)
// {
// $data['last_name'] = trim($data['last_name']);
// $data['first_name'] = trim($data['first_name']);
// }
}