Reputation: 4452
In validation class my intiialize
function is like this
public function initialize()
{
$this->add('gender',new InclusionIn(array(
'message' => 'Please enter a valid Gender',
'domain' => array('Male','Female'),
'case_insensitive' => false
)));
}
Problem is, InclusionIn
makes a case-sensitive validation so if user enter "male" application throws an error Please enter a valid Gender. I want this validation should be case-insensitive
but I did not find any way to do it.
Upvotes: 2
Views: 543
Reputation: 21
You can now use filter see phalcon Filtering and Sanitizing https://docs.phalconphp.com/en/latest/reference/filter.html
In controller like this: note filter need to be added to services initialisation
$gender = $this->filter->sanitize($gender, "lower");
OR
In your customValidation class:
class MyValidation extends Validation
{
public function initialize()
{
$this->add('gender',new InclusionIn(array(
'message' => 'Please enter a valid Gender',
'domain' => array('male','female'),
)));
$this->setFilters('gender', 'lower');
}
}
Upvotes: 1
Reputation: 4980
In case of validating model, you can use beforeValidation()
, beforeValidationOnCreate()
or beforeValidationOnUpdate()
to lowercase/uppercase value:
function beforeValidation() {
$this->gender = ucfirst($this->gender);
}
If validating via Forms, you can apply addFilter(lower)
and validate for lowercase eg.
Upvotes: 1
Reputation: 4452
As @M2sh & @honerlawd guided, finally I ended with writing a new validator for 'inclusion` sharing the code here
<?php
namespace library\app\validators;
use Phalcon\Validation;
use Phalcon\Validation\Validator;
use Phalcon\Validation\Exception;
use Phalcon\Validation\Message;
class InclusionIn extends Validator
{
/**
* Executes the validation
*/
public function validate(Validation $validation, $attribute)
{
$value = $validation->getValue($attribute);
if ($this->isSetOption("allowEmpty") && empty($value)) {
return true;
}
/**
* A domain is an array with a list of valid values
*/
$domain = $this->getOption("domain");
if (!is_array($domain)) {
throw new Exception("Option 'domain' must be an array");
}
$refinedDomain = array_map('strtolower', $domain);
$strict = false;
if ($this->isSetOption("strict")) {
if (!is_bool($strict)) {
throw new Exception("Option 'strict' must be a boolean");
}
$strict = $this->getOption("strict");
}
/**
* Check if the value is contained by the array
*/
if (!in_array(strtolower($value), $refinedDomain, $strict)) {
$label = $this->getOption("label");
if (empty($label)) {
$label = $validation->getLabel($attribute);
}
$message = $this->getOption("message");
$replacePairs = ['field' => $label, 'domain' => join(", ", $domain)];
if (empty($message)) {
$message = $validation->getDefaultMessage("InclusionIn");
}
$validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, "InclusionIn"));
return false;
}
return true;
}
}
Upvotes: 2
Reputation: 1509
InclusionIn
uses in_array
which is case sensitive. You would have to write your own Validator
in order to get the functionality you would need. Here is the implementation of the class so you can see what options are available.
Another option is just to format the input before the validator is fired. e.g. if they type in male
or MALE
convert it to Male
so the validation will pass.
Upvotes: 2