本文整理汇总了PHP中Phalcon\Validation类的典型用法代码示例。如果您正苦于以下问题:PHP Validation类的具体用法?PHP Validation怎么用?PHP Validation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Validation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
/**
* Executes the validation
*
* @param Phalcon\Validation $validator
* @param string $attribute
* @return boolean
*/
public function validate(\Phalcon\Validation $validator, $attribute)
{
$flag = true;
$value = $validator->getValue($attribute);
$errorCode = $this->getOption('code');
if (!is_array($value)) {
$message = '参数必须是数组';
$validator->appendMessage(new Message($message, $attribute, 'Nums'));
return false;
}
$countVal = count($value);
$ruleMin = $this->getOption('min');
$ruleMax = $this->getOption('max');
if ($ruleMin == $ruleMax) {
if ($countVal < $ruleMin) {
$flag = false;
}
} else {
if ($countVal < $ruleMin || $countVal > $ruleMax) {
$flag = false;
}
}
if (!$flag) {
$message = $this->getOption('message');
if (!$message) {
$message = 'The num is not valid';
}
$validator->appendMessage(new Message($message, $attribute, 'Nums'));
return false;
}
return true;
}
示例2: validation
/**
* @param Validation $validator
* @return Validation
*/
protected function validation(Validation $validator)
{
if ($this->getEmail()) {
$validator->add('email', new Email(['message' => 'The e-mail is not valid']));
}
return $validator;
}
示例3: validate
/**
* Executes the validation
*
* @package base-app
* @version 2.0
*
* @param object $validation Phalcon\Validation
* @param string $field field name
*
* @return boolean
*
* @throws \Phalcon\Validation\Exception
*/
public function validate(\Phalcon\Validation $validation, $field)
{
$value = $validation->getValue($field);
$model = $this->getOption("model");
$attribute = $this->getOption("attribute");
if (empty($model)) {
throw new \Phalcon\Validation\Exception("Model must be set");
}
if (empty($attribute)) {
$attribute = $field;
}
if ($except = $this->getOption('except')) {
$number = $model::count(array($attribute . "=:value: AND " . $attribute . "!= :except:", "bind" => array("value" => $value, 'except' => $except)));
} else {
$number = $model::count(array($attribute . "=:value:", "bind" => array("value" => $value)));
}
if ($number) {
$label = $this->getOption("label");
if (empty($label)) {
$label = $validation->getLabel($field);
if (empty($label)) {
$label = $field;
}
}
$message = $this->getOption("message");
$replacePairs = array(":field" => $label);
if (empty($message)) {
$message = $validation->getDefaultMessage("Uniqueness");
}
$validation->appendMessage(new \Phalcon\Validation\Message(strtr($message, $replacePairs), $field, "Uniqueness"));
return false;
}
return true;
}
示例4: validation
public function validation()
{
$validator = new Validation();
$validator->add('start_time', new DatetimeValidator('开始时间格式有误'));
$validator->add('end_time', new DatetimeValidator('结束时间格式有误'));
return $this->validate($validator);
}
示例5: validate
/**
* @param \Phalcon\Validation $validation
* @param string $field
*
* @return boolean
*
* @throws \Phalcon\Validation\Exception
*/
public function validate(\Phalcon\Validation $validation, $field)
{
if (!is_string($field)) {
throw new \Phalcon\Validation\Exception("Field name must be a string");
}
$label = $this->getOption("label");
if (empty($label)) {
$label = $validation->getLabel($field);
}
$value = $validation->getValue($field);
if ($this->isSetOption("allowEmpty") && empty($value)) {
return true;
}
// http://stackoverflow.com/questions/1418423/the-hostname-regex
if (!preg_match('/^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\\.?$/', $value)) {
$message = $this->getOption("message");
if (empty($message)) {
$message = "Field :field is not a valid hostname";
}
$replacePairs = [":field" => $label];
$validation->appendMessage(new \Phalcon\Validation\Message(strtr($message, $replacePairs), $field, "Hostname"));
return false;
}
return true;
}
示例6: createAction
public function createAction()
{
$response = new Response();
$response->setHeader('Content-Type', 'application/json');
if ($this->request->isPost()) {
$validation = new Validation();
$validation->add('profilesId', new PresenceOf(array('message' => '请选择一个角色类型')));
$messages = $validation->validate($_POST);
$datas = array();
if (count($messages)) {
foreach ($messages as $message) {
$datas[] = $message->getMessage();
}
$response->setJsonContent(array('status' => 'error', 'messages' => $datas));
} else {
$user = new Users();
$user->assign(array('name' => $this->request->getPost('name', 'striptags'), 'email' => $this->request->getPost('email', 'email'), 'profilesId' => $this->request->getPost('profilesId', 'int')));
if (!$user->save()) {
foreach ($user->getMessages() as $message) {
$datas[] = $message->getMessage();
}
$response->setJsonContent(array('status' => 'error', 'messages' => $datas));
} else {
$response->setJsonContent(array('status' => 'success', 'messages' => '用户添加成功'));
}
}
}
return $response;
}
示例7: validation
public function validation()
{
$validator = new Validation();
$validator->add('departure_address', new StringLength(['max' => 200, 'messageMaximum' => '出发位置的长度不能超过200字']));
$validator->add('remark', new StringLength(['max' => 400, 'messageMaximum' => '备注的长度不能超过400字']));
return $this->validate($validator);
}
示例8: validate
/**
* Executes the validation
* Note: Explicitly supply `conditions` and `bind` options if model PK is not `id`
* @param mixed $validation
* @param string $attribute
* @return bool
*/
public function validate(Validation $validation, $attribute)
{
$value = $validation->getValue($attribute);
$model_name = $this->getOption('model', null);
$conditions = $this->getOption('conditions', null);
$bind = $this->getOption('bind', []);
$show_messages = $this->getOption('show_messages', true);
/** @var \Phalcon\Mvc\Model $model */
$model = $this->getModel($model_name);
if (is_null($conditions)) {
if (is_null($value)) {
return false;
}
$data = $model::findFirst(["id = ?0", "bind" => $value]);
} else {
$data = $model::findFirst(['conditions' => $conditions, 'bind' => $bind]);
}
if (!$data) {
if ($show_messages) {
$this->addMessageToValidation($validation, 'Invalid :field supplied', $attribute, 'Model');
}
return false;
}
return true;
}
示例9: validate
/**
* {@inheritdoc}
*
* @param Validation $validation
* @param string $attribute
*
* @return bool
* @throws Exception
*/
public function validate(Validation $validation, $attribute)
{
$value = $validation->getValue($attribute);
$field = $this->getOption('label');
if (empty($field)) {
$validation->getLabel($attribute);
}
if (false === $this->hasOption('places')) {
throw new Exception('A number of decimal places must be set');
}
if ($this->hasOption('digits')) {
// Specific number of digits
$digits = '{' . (int) $this->getOption('digits') . '}';
} else {
// Any number of digits
$digits = '+';
}
if ($this->hasOption('point')) {
$decimal = $this->getOption('point');
} else {
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
}
$result = (bool) preg_match(sprintf('#^[+-]?[0-9]%s%s[0-9]{%d}$#', $digits, preg_quote($decimal), $this->getOption('places')), $value);
if (!$result) {
$message = $this->getOption('message');
$replacePairs = [':field' => $field];
if (empty($message)) {
$message = ':field must contain valid decimal value';
}
$validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, 'Decimal'));
return false;
}
return true;
}
示例10: validation
/**
* Validate user's email
* @return bool
*/
public function validation()
{
$validator = new Validation();
$validator->add('email', new Email(['message' => 'Invalid email supplied']));
$validator->add('email', new Uniqueness(array('message' => 'Sorry, The email has been used by another user')));
return $this->validate($validator);
}
示例11: validate
/**
* {@inheritdoc}
*
* @param Validation $validation
* @param string $attribute
*
* @return bool
*/
public function validate(Validation $validation, $attribute)
{
$secret = $this->getOption('secret');
$value = $validation->getValue($attribute);
$request = $validation->getDI()->get('request');
$remoteIp = $request->getClientAddress(false);
if (!empty($value)) {
$curl = curl_init(self::RECAPTCHA_URL);
curl_setopt_array($curl, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => ['secret' => $secret, 'response' => $value, 'remoteip' => $remoteIp]]);
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
}
if (empty($response['success'])) {
$label = $this->getOption('label');
if (empty($label)) {
$label = $validation->getLabel($attribute);
}
$message = $this->getOption('message');
$replacePairs = [':field', $label];
if (empty($message) && !empty($response['error-codes'])) {
$message = $this->messages[$response['error-codes']];
}
if (empty($message)) {
$message = $validation->getDefaultMessage('ReCaptcha');
}
$validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, 'ReCaptcha'));
return false;
}
return true;
}
示例12: validation
public function validation()
{
$validator = new Validation();
$validator->add('email', new EmailValidator(['model' => $this, 'message' => 'Sorry, The email was registered by another user']));
$validator->add('username', new UniquenessValidator(['model' => $this, 'message' => 'Sorry, That username is already taken']));
return $this->validate($validator);
}
示例13: validation
public function validation()
{
$validator = new Validation();
$validator->add('name', new PetnameValidator());
$validator->add('birthday', new DateValidator(['message' => '您填写的动物生日的格式有误']));
return $this->validate($validator);
}
示例14: validate
/**
* Executes the validation
*
* @param Phalcon\Validation $validator
* @param string $attribute
* @return boolean
*/
public function validate(\Phalcon\Validation $validator, $attribute)
{
$flag = true;
$reqType = $validator->getValue($attribute);
// 待校验的文件集合
$fileType = $this->getOption('filetype');
// 合法的文件类型集合
$errorCode = $this->getOption('code');
foreach ($reqType as $file) {
$extArr = explode('.', $file);
$ext = array_pop($extArr);
if (!in_array($ext, $fileType)) {
$flag = false;
break;
}
}
if (!$flag) {
$message = $this->getOption('message');
if (!$message) {
$message = 'The filetype is not valid';
}
$validator->appendMessage(new Message($message, $attribute, 'filetype'));
return false;
}
return true;
}
示例15: validate
public function validate(Validation $validation, $attribute)
{
$value = $validation->getValue($attribute);
$model = $this->getOption('model');
$except = $this->getOption("except");
if (empty($model)) {
throw new \Exception("Model must be set");
}
if (empty($attribute)) {
throw new \Exception("arrtibute must be set");
}
if ($except) {
$number = $model::count(sprintf($attribute . " = %s AND " . $attribute . " != %s", $value, $except));
} else {
$number = $model::count(sprintf($attribute . " = %s", $value));
}
if (!$number) {
$message = $this->getOption('message');
if (empty($message)) {
$message = '字段对应的值不存在';
}
$validation->appendMessage(new Message($message, $attribute, "Existence"));
return false;
}
return true;
}