/** * Check if data are valide * * @return boolean */ public function validate() { // use validator library require_once dirname(__FILE__) . '/../datavalidator.lib.php'; // new validator $validator = new DataValidator(); $dataList = array('name' => $this->name, 'description' => $this->description); $validator->setDataList($dataList); $validator->addRule('name', get_lang('Name is missing'), 'required'); // $validator->addRule('description',get_lang('Description is missing'),'required'); if ($validator->validate(DATAVALIDATOR_STRICT_MODE)) { return true; } else { $this->errorList = $validator->getErrorList(); return false; } }
/** * Validate user form. * * @author Hugues Peeters <*****@*****.**> * @author Frederic Minne <*****@*****.**> * @param string $mode 'registration' or 'profile' or 'admin_user_profile' * @param array $data to fill the form * @param int $userId (optional) id of the user account currently edited * @return array with error messages */ function user_validate_form($formMode, $data, $userId = null) { require_once dirname(__FILE__) . '/datavalidator.lib.php'; if (empty($userId) || claro_is_platform_admin()) { $editableFields = array('name', 'official_code', 'login', 'password', 'email', 'phone', 'language', 'picture', 'skype'); if (claro_is_platform_admin()) { $editableFields[] = 'authSource'; } } else { // $editableFields = get_conf('profile_editable'); $editableFields = AuthProfileManager::getUserAuthProfile($userId)->getEditableProfileFields(); } $validator = new DataValidator(); $validator->setDataList($data); if (in_array('name', $editableFields)) { $validator->addRule('lastname', get_lang('You left some required fields empty'), 'required'); $validator->addRule('firstname', get_lang('You left some required fields empty'), 'required'); } if (in_array('login', $editableFields)) { $validator->addRule('username', get_lang('You left some required fields empty'), 'required'); $validator->addRule('username', get_lang('Username is too long (maximum 60 characters)'), 'maxlength', 60); } if (in_array('email', $editableFields) && !get_conf('userMailCanBeEmpty')) { $validator->addRule('email', get_lang('You left some required fields empty'), 'required'); } if (in_array('official_code', $editableFields) && !get_conf('userOfficialCodeCanBeEmpty')) { $validator->addRule('officialCode', get_lang('You left some required fields empty'), 'required'); } if (in_array('password', $editableFields) && (array_key_exists('password', $data) || array_key_exists('password_conf', $data))) { if ($formMode != 'registration' && $formMode != 'admin_user_profile') { $userProperties = user_get_properties($userId); $validator->addRule('old_password', get_lang('You left some required fields empty'), 'required'); $validator->addRule('old_password', get_lang('Old password is wrong'), 'user_check_authentication', array($userProperties['username'])); } if (get_conf('SECURE_PASSWORD_REQUIRED')) { $validator->addRule('password', get_lang('This password is too simple or too close to the username, first name or last name.<br> Use a password like this <code>%passProposed</code>', array('%passProposed' => generate_passwd())), 'is_password_secure_enough', array(array($data['username'], $data['officialCode'], $data['lastname'], $data['firstname'], $data['email']))); } $validator->addRule('password', get_lang('You typed two different passwords'), 'compare', $data['password_conf']); } $validator->addRule('email', get_lang('The email address is not valid'), 'email'); if ('registration' == $formMode) { $validator->addRule('password_conf', get_lang('You left some required fields empty'), 'required'); $validator->addRule('officialCode', get_lang('This official code is already used by another user.'), 'is_official_code_available'); $validator->addRule('username', get_lang('This username is already taken'), 'is_username_available'); $validator->addRule('password', get_lang('You left some required fields empty'), 'required'); } else { // FIX for the empty password issue if (!empty($data['password']) || !empty($data['password_conf'])) { $validator->addRule('password', get_lang('You left some required fields empty'), 'required'); } $validator->addRule('officialCode', get_lang('This official code is already used by another user.'), 'is_official_code_available', $userId); $validator->addRule('username', get_lang('This username is already taken'), 'is_username_available', $userId); } if ($validator->validate()) { return array(); } else { return array_unique($validator->getErrorList()); } }