public function beforeValidate(Model $model, $options = array()) { if (array_key_exists($model->alias, $model->request->data)) { if (array_key_exists('contact_type_id', $model->data[$model->alias])) { $model->validator()->remove('value'); switch ($model->data[$model->alias]['contact_type_id']) { // 1 -> Mobile, 2 -> Phone, 3 -> Fax, 4 -> Email, 5 -> Other case '1': case '2': case '3': $model->validator()->add('value', 'required', array('rule' => 'Numeric', 'message' => $model->getErrorMessage('valuePhone'))); break; case '4': $model->validator()->add('value', 'required', array('rule' => array('email', true), 'message' => $model->getErrorMessage('valueEmail'))); break; default: // 5 -> Other $model->validator()->add('value', 'required', array('rule' => 'notEmpty', 'message' => $model->getErrorMessage('value'))); break; } } } $model->validator()->remove('name'); $model->validator()->add('name', 'required', array('rule' => 'notEmpty', 'message' => $model->getErrorMessage('value'))); $model->validator()->remove('contact_type_id'); $model->validator()->add('contact_type_id', 'required', array('rule' => 'notEmpty', 'message' => $model->getErrorMessage('contact_type_id'))); return parent::beforeValidate($model, $options); }
/** * {@inheritdoc} */ public function beforeValidate(Model $Model, $options = array()) { $ModelValidator = $Model->validator(); foreach ($Model->data[$Model->alias] as $field => $value) { if (!preg_match('/^([a-z0-9_]+)_confirm$/i', $field, $match)) { continue; } if (!array_key_exists($match[1], $Model->data[$Model->alias])) { continue; } if (!($Ruleset = $ModelValidator->getField($match[1]))) { $Ruleset = new CakeValidationSet($match[1], array()); } $ruleset = array(); foreach ($Ruleset->getRules() as $name => $Rule) { $ruleset[$name] = (array) $Rule; foreach (array_keys($ruleset[$name]) as $key) { if (!preg_match('/^[a-z]/i', $key)) { unset($ruleset[$name][$key]); } } } $ModelValidator->add($field, new CakeValidationSet($field, array())); $ModelValidator->getField($field)->setRule('confirmed', array('rule' => 'isConfirmed', 'message' => __d('common', "No match."))); } return true; }
/** * Before validate method. * * @param Model $Model Model using this behavior * @param array $options Options passed from Model::save(). * @return mixed False or null will abort the operation. Any other result will continue. */ public function beforeValidate(Model $Model, $options = array()) { // Adds the validation rule for confirming the passwords match if (isset($options['confirmPassword']) && $options['confirmPassword'] === true) { $Model->validator()->add($this->settings['confirmPassword']['field'], array($this->settings['confirmPassword']['ruleName'] => array('rule' => array('confirmPassword'), 'message' => $this->settings['confirmPassword']['ruleMessage']))); } return true; }
public function setup(Model $model, $settings = array()) { if (!isset($this->settings[$model->alias])) { $this->settings[$model->alias] = array('fields' => array()); } $this->settings[$model->alias] = array_merge($this->settings[$model->alias], (array) $settings); // Let's add the validator to the model. foreach ($this->settings[$model->alias]['fields'] as $field) { $model->validator()->add($field, 'valid_interval', array('rule' => 'validateInterval', 'message' => 'Invalid interval')); } }
/** * * @param Model $Model * @param unknown $data * * @return boolean|Ambigous <multitype:, NULL> */ protected function _tokenizeResetEmail(Model $Model, $data = array()) { $Model->validator()->remove('password'); $Model->validator()->remove('captcha'); $Model->validator()->add('password', array('notempty' => array('rule' => array('notempty'), 'message' => 'Password is required'), 'exists' => array('rule' => array('fieldExists', array($Model->alias . '.id' => AuthComponent::user('id'))), 'message' => 'Password doesnt match'))); $Model->validator()->add('captcha', array('captcha' => array('rule' => array('captcha'), 'message' => 'Captcha is wrong'))); $Model->set($data); if ($Model->validator()->errors(array('fieldList' => array('password', 'captcha')))) { return false; } $user = $Model->find('first', array('conditions' => array($Model->alias . '.' . $Model->primaryKey => AuthComponent::user('id')))); $id = $user[$Model->alias][$Model->primaryKey]; $Model->Tokenization->deleteAll(array($Model->Tokenization->alias . '.user_id' => $id, $Model->Tokenization->alias . '.field' => 'email', $Model->Tokenization->alias . '.action' => 'reset')); $data = array($Model->Tokenization->alias => array('user_id' => $id, 'token' => Security::hash(String::uuid()), 'expire' => date('Y-m-d H:i:s', strtotime($this->settings[$Model->alias]['expire'])), 'action' => 'reset', 'field' => 'email')); $Model->Tokenization->create(); if ($Model->Tokenization->save($data)) { $Model->getEventManager()->dispatch(new CakeEvent("Model.{$Model->alias}.afterTokenize", $Model, array('id' => $id, 'field' => 'email', 'action' => 'reset'))); return $id; } return false; }
/** * Adds the multiColumnUnique data validation rule dynamically * * Loops through the field groups and their fields which need to be unique. * First removes the multiColumnUniqueness rule from each unique field. * If the 'onlyOnce' option is set to false, * it adds the rule to each field of the field group. * Otherwise, by default, it adds the rule only to the first of the * relevant fields found in the data array. * * @param Model $model Model using this behavior * @param array $options Options passed from Model::save() (unused). * @return bool True if validate operation should continue, false to abort */ public function beforeValidate(Model $model, $options = array()) { $fieldGroupCount = count($this->settings[$model->alias]['fields']); for ($groupNr = 0; $groupNr < $fieldGroupCount; $groupNr++) { $uniqueFieldGrp = $this->settings[$model->alias]['fields'][$groupNr]; $fieldGroupName = 'multiColumnUniqueness-group_' . ($groupNr + 1); foreach ($uniqueFieldGrp as $uniqueField) { if ($model->validator()->getField($uniqueField)) { $model->validator()->remove($uniqueField, $fieldGroupName); } } foreach ($uniqueFieldGrp as $uniqueField) { if (isset($model->data[$model->name]) && array_key_exists($uniqueField, $model->data[$model->name])) { $model->validator()->add($uniqueField, $fieldGroupName, array('rule' => array('multiColumnUniqueness', $uniqueFieldGrp), 'message' => $this->settings[$model->alias]['errMsg'][$groupNr])); if ($this->settings[$model->alias]['onlyOnce']) { break; } } } } return parent::beforeValidate($model, $options); }
public function setup(Model $model, $config = array()) { if (!isset($this->_methods[$model->name])) { $this->_methods[$model->name] = $model->validator()->getMethods(); } }
/** * Adding validation rules * also adds and merges config settings (direct + configure) * * @return void */ public function setup(Model $Model, $config = []) { $this->_validationRules = ['formField' => ['between' => ['rule' => ['between', PWD_MIN_LENGTH, PWD_MAX_LENGTH], 'message' => __d('tools', 'valErrBetweenCharacters %s %s', PWD_MIN_LENGTH, PWD_MAX_LENGTH), 'allowEmpty' => null, 'last' => true]], 'formFieldRepeat' => ['validateNotEmpty' => ['rule' => ['notBlank'], 'message' => __d('tools', 'valErrPwdRepeat'), 'allowEmpty' => true, 'last' => true], 'validateIdentical' => ['rule' => ['validateIdentical', 'formField'], 'message' => __d('tools', 'valErrPwdNotMatch'), 'allowEmpty' => null, 'last' => true]], 'formFieldCurrent' => ['notBlank' => ['rule' => ['notBlank'], 'message' => __d('tools', 'valErrProvideCurrentPwd'), 'allowEmpty' => null, 'last' => true], 'validateCurrentPwd' => ['rule' => 'validateCurrentPwd', 'message' => __d('tools', 'valErrCurrentPwdIncorrect'), 'allowEmpty' => null, 'last' => true]]]; $defaults = $this->_defaultConfig; if ($configureDefaults = Configure::read('Passwordable')) { $defaults = $configureDefaults + $defaults; } $this->settings[$Model->alias] = $config + $defaults; // BC comp if ($this->settings[$Model->alias]['allowEmpty']) { $this->settings[$Model->alias]['require'] = false; } $formField = $this->settings[$Model->alias]['formField']; $formFieldRepeat = $this->settings[$Model->alias]['formFieldRepeat']; $formFieldCurrent = $this->settings[$Model->alias]['formFieldCurrent']; if ($formField === $this->settings[$Model->alias]['field']) { throw new CakeException('Invalid setup - the form field must to be different from the model field (' . $this->settings[$Model->alias]['field'] . ').'); } $rules = $this->_validationRules; foreach ($rules as $field => $fieldRules) { foreach ($fieldRules as $key => $rule) { $rule['allowEmpty'] = !$this->settings[$Model->alias]['require']; if ($key === 'between') { $rule['rule'] = ['between', $this->settings[$Model->alias]['minLength'], $this->settings[$Model->alias]['maxLength']]; $rule['message'] = __d('tools', 'valErrBetweenCharacters %s %s', $this->settings[$Model->alias]['minLength'], $this->settings[$Model->alias]['maxLength']); } $fieldRules[$key] = $rule; } $rules[$field] = $fieldRules; } // Add the validation rules if not already attached if (!isset($Model->validate[$formField])) { $Model->validator()->add($formField, $rules['formField']); } if (!isset($Model->validate[$formFieldRepeat])) { $ruleSet = $rules['formFieldRepeat']; $ruleSet['validateIdentical']['rule'][1] = $formField; $Model->validator()->add($formFieldRepeat, $ruleSet); } if ($this->settings[$Model->alias]['current'] && !isset($Model->validate[$formFieldCurrent])) { $Model->validator()->add($formFieldCurrent, $rules['formFieldCurrent']); if (!$this->settings[$Model->alias]['allowSame']) { $Model->validator()->add($formField, 'validateNotSame', ['rule' => ['validateNotSame', $formField, $formFieldCurrent], 'message' => __d('tools', 'valErrPwdSameAsBefore'), 'allowEmpty' => !$this->settings[$Model->alias]['require'], 'last' => true]); } } elseif (!isset($Model->validate[$formFieldCurrent])) { // Try to match the password against the hash in the DB if (!$this->settings[$Model->alias]['allowSame']) { $Model->validator()->add($formField, 'validateNotSame', ['rule' => ['validateNotSameHash', $formField], 'message' => __d('tools', 'valErrPwdSameAsBefore'), 'allowEmpty' => !$this->settings[$Model->alias]['require'], 'last' => true]); } } // Add custom rule(s) if configured if ($this->settings[$Model->alias]['customValidation']) { $Model->validator()->add($formField, $this->settings[$Model->alias]['customValidation']); } }
/** * Adding validation rules * also adds and merges config settings (direct + configure) * * @return void */ public function setup(Model $Model, $config = array()) { $defaults = $this->_defaultConfig; if ($configureDefaults = Configure::read('Passwordable')) { $defaults = $configureDefaults + $defaults; } $this->settings[$Model->alias] = $config + $defaults; // BC comp if ($this->settings[$Model->alias]['allowEmpty']) { $this->settings[$Model->alias]['require'] = false; } $formField = $this->settings[$Model->alias]['formField']; $formFieldRepeat = $this->settings[$Model->alias]['formFieldRepeat']; $formFieldCurrent = $this->settings[$Model->alias]['formFieldCurrent']; if ($formField === $this->settings[$Model->alias]['field']) { throw new CakeException('Invalid setup - the form field must to be different from the model field (' . $this->settings[$Model->alias]['field'] . ').'); } $rules = $this->_validationRules; foreach ($rules as $field => $fieldRules) { foreach ($fieldRules as $key => $rule) { $rule['allowEmpty'] = !$this->settings[$Model->alias]['require']; if ($key === 'between') { $rule['rule'][1] = $this->settings[$Model->alias]['minLength']; $rule['message'][1] = $this->settings[$Model->alias]['minLength']; $rule['rule'][2] = $this->settings[$Model->alias]['maxLength']; $rule['message'][2] = $this->settings[$Model->alias]['maxLength']; } $fieldRules[$key] = $rule; } $rules[$field] = $fieldRules; } // Add the validation rules if not already attached if (!isset($Model->validate[$formField])) { $Model->validator()->add($formField, $rules['formField']); } if (!isset($Model->validate[$formFieldRepeat])) { $ruleSet = $rules['formFieldRepeat']; $ruleSet['validateIdentical']['rule'][1] = $formField; $Model->validator()->add($formFieldRepeat, $ruleSet); } if ($this->settings[$Model->alias]['current'] && !isset($Model->validate[$formFieldCurrent])) { $Model->validator()->add($formFieldCurrent, $rules['formFieldCurrent']); if (!$this->settings[$Model->alias]['allowSame']) { $Model->validator()->add($formField, 'validateNotSame', array('rule' => array('validateNotSame', $formField, $formFieldCurrent), 'message' => 'valErrPwdSameAsBefore', 'allowEmpty' => !$this->settings[$Model->alias]['require'], 'last' => true)); } } elseif (!isset($Model->validate[$formFieldCurrent])) { // Try to match the password against the hash in the DB if (!$this->settings[$Model->alias]['allowSame']) { $Model->validator()->add($formField, 'validateNotSame', array('rule' => array('validateNotSameHash', $formField), 'message' => 'valErrPwdSameAsBefore', 'allowEmpty' => !$this->settings[$Model->alias]['require'], 'last' => true)); } } }
public function setupField(Model $model, $field, $config) { $model->validator()->add($field, 'captchaNotEmpty', array('rule' => 'notEmpty', 'message' => __('Please enter the captcha code')))->add($field, 'captchaValidate', array('rule' => array('validateCaptcha'), 'message' => __('Captcha code not valid'))); }
public function __construct() { //encryption require_once "encryption.php"; // $data['invalidEmailPass'] = "******"; //Authentication logic // $error = []; // $data['unErrorFirstName'] = "<p><p/>"; // $data['unErrorLastName'] = "<p><p/>"; // $data['unErrorEmail'] = "<p><p/>"; // $data['unErrorPassword'] = "******"; // $data['unFormFirstName'] = ""; // $data['unFormLastName'] = ""; // $data['unFormEmail'] = ""; $data['errorFirstName'] = "<p></p>"; $data['errorLastName'] = "<p></p>"; $data['errorEmail'] = "<p></p>"; $data['errorPassword'] = "******"; // $data['formFirstName'] = ""; // $data['formLastName'] = ""; // $data['formEmail'] = ""; if (isset($_POST["signup"])) { // $hasErrors = FALSE; $form["first_name"] = isset($_POST["first_name"]) ? $_POST["first_name"] : null; $form["last_name"] = isset($_POST["last_name"]) ? $_POST["last_name"] : null; $form["email"] = isset($_POST["email"]) ? $_POST["email"] : null; $form["password"] = isset($_POST["password"]) ? $_POST["password"] : null; $form["repassword"] = isset($_POST["repassword"]) ? $_POST["repassword"] : null; $modelObj = new Model(); $countErrors = 0; if (!$modelObj->validator($form["first_name"], 'name')) { // $hasErrors = TRUE; $data['errorFirstName'] = 'Invalid first name! Please insert at least 4 characters!'; $countErrors++; } if (!$modelObj->validator($form["last_name"], 'name')) { $data["errorLastName"] = 'Invalid last name! Please insert at least 4 characters!'; $countErrors++; } if (!$modelObj->validator($form["email"], 'email')) { $data["errorEmail"] = 'Invalid email format!'; $countErrors++; } // check for duplicate emails - the beginning $sql = 'SELECT email FROM logins WHERE email = "' . $form['email'] . '"'; $result = $modelObj->makeSql($sql, TRUE); $db_email = null; foreach ($result as $row) { $db_email = isset($row['email']) ? $row['email'] : null; } if ($db_email === $form['email']) { $data["errorEmail"] = "Sorry! The email already exists!"; $countErrors++; } // check for duplicate emails - the end if (!$modelObj->validator($form["password"], 'password')) { $data["errorPassword"] = '******'; $countErrors++; } if ($form["password"] !== $form["repassword"]) { //Validate pass contain at least one digit $data["errorPassword"] = "******"; $countErrors++; } if ($countErrors == 0) { //TODO insert user (email) and pass into db $sql_insert = 'INSERT INTO logins (email, password, first_name, last_name) VALUES ("' . $form["email"] . '", "' . encrypt_password($form["password"]) . '", "' . $form["first_name"] . '", "' . $form["last_name"] . '")'; $modelObj->makeSql($sql_insert, FALSE); // echo "New record created successfully"; // redirect to LoginPage header("Location: http://188.166.119.187/workspace/ilear/MVC/part4/index.php?page=login"); } // else { // $data['condition'] = (isset($error["first_name"])); // $data['errorFirstName'] = $error["first_name"]; // $data['unErrorFirstName'] = "<p></p>"; // $data['condition'] = (isset($error["last_name"])); // $data['errorLastName'] = $error["last_name"]; // $data['unErrorLastName'] = "<p></p>"; // $data['condition'] = (isset($error["email"])); // $data['errorEmail'] = $error["email"]; // $data['unErrorEmail'] = "<p></p>"; // $data['condition'] = (isset($error["password"])); // $data['errorPassword'] = $error["password"]; // $data['unErrorPassword'] = "******"; // $data['errorFirstName'] = $error["first_name"]; // $data['errorLastName'] = $error["last_name"]; // $data['errorEmail'] = $error["email"]; // $data['errorPassword'] = $error["password"]; // $data['condition'] = (isset($form["first_name"])); // $data['formFirstName'] = $form["first_name"]; // $data['unFormFirstName'] = ""; // $data['condition'] = (isset($form["last_name"])); // $data['formLastName'] = $form["last_name"]; // $data['unFormLastName'] = ""; // $data['condition'] = (isset($form["email"])); // $data['formEmail'] = $form["email"]; // $data['unFormEmail'] = ""; // $data['formFirstName'] = $form["first_name"]; // $data['formLastName'] = $form["last_name"]; // $data['formEmail'] = $form["email"]; // } } // $data['condition'] = (isset($_SESSION['logged']) && $_SESSION['logged'] === TRUE); // $data['logged'] = "You are logged in!"; // $data['unlogged'] = ""; $data['logged'] = isset($_SESSION['logged']) && $_SESSION['logged'] === TRUE ? "You are logged in!" : ""; $data['formFirstName'] = isset($form["first_name"]) ? $form["first_name"] : ""; $data['formLastName'] = isset($form["last_name"]) ? $form["last_name"] : ""; $data['formEmail'] = isset($form["email"]) ? $form["email"] : ""; $data['title'] = "SignupPage"; // $data['mailSent'] = "Note that only phone number is optional!"; $this->render('views/top.php', $data); $this->render('views/menu.php', $data); $this->render('views/signup.php', $data); $this->render('views/bottom.php', $data); }
/** * {@inheritdoc} */ public function beforeValidate(Model $Model, $options = array()) { if (empty($Model->data[$Model->alias])) { return true; } $ModelValidator = $Model->validator(); foreach ($this->settings[$Model->alias]['fields'] as $field => $options) { if (isset($Model->stateableValidation) && false === $Model->stateableValidation || !array_key_exists($field, $Model->data[$Model->alias]) || $ModelValidator->getField($field)) { continue; } $ModelValidator->add($field, new CakeValidationSet($field, array('valid' => array('rule' => 'isValidState', 'message' => __d('common', "Invalid %s.", $field))))); } return true; }