Yii2 validation rule for array - php

I've an attribute in model which I want to validate in such a way that - It must be an array and must have 3 element, also every element inside array must be a string. Currently I'm using.
['config', 'each', 'rule' => ['string']]

You could simply use a custom validator, e.g. :
['config', function ($attribute, $params) {
if(!is_array($this->$attribute) || count($this->$attribute)!==3){
$this->addError($attribute, 'Error message');
}
}],
['config', 'each', 'rule' => ['string']]
Read more about creating validators.

You can add a custom validation rules like below:
public function rules()
{
return ['config','checkIsArray'];
}
public function checkIsArray($attribute, $params)
{
if (empty($this->config)) {
$this->addError('config', "config cannot be empty");
}
elseif (!is_array($this->config)) {
$this->addError('config', "config must be array.");
}
elseif (count($this->config)<3) {
$this->addError('config', "config must have 3 elements");
}
else {
foreach ($this->config as $value) {
if (!is_string($value)) {
$this->addError('config ', "config should have only string values.");
}
}
}
}

Related

Laravel form validation: same as field a or b

I have 3 fields: player1, player2 and winner. I want to validate that winner is equal to either player1 or player 2.
I tried looking for a way to add an 'or' clause to the validation but couldn't find anything, only workarounds for different problems.
You can use a custom validation rule to do this. There's nothing built in that allows comparison to one of many other fields. The closest is the same check, but it only checks against one other field.
(I've added dd() to dump data, you can remove them)
The $values will be what comes from your input.
The $validationRules can be adjusted for your needs.
Validator::extend('equals_one_of', function($attribute, $value, $parameters, \Illuminate\Validation\Validator $validator) {
$fields = $validator->getData(); // all posted values
foreach($parameters as $param) { // this is each of the comma separated fields in the validationRules array
if ($value == $fields[$param]) {
dd("matched");
return true;
}
}
dd("no match");
return false;
});
$values = [
'player1' => 'test1',
'player2' => 'test2',
'winner' => 'test1'
];
$validationRules = [
'player1' => 'required',
'player2' => 'required',
'winner' => 'required|equals_one_of:player1,player2'
];
$validate = Validator::make($values, $validationRules);
// use your validator as normal.
dd($validate->validate());
Actually this rule should be registered with extendDependent to correctly resolve parameter names when used with arrays.
Final code:
class OneOfRule
{
public function validate($attribute, $value, $parameters, $validator) {
if(is_null($value)) return true;
foreach($parameters as $param) {
$other = Arr::get($validator->getData(), $param);
if ($value === $other) {
return true;
}
}
return false;
}
}
public function boot()
{
Validator::extendDependent('one_of', 'App\Http\Validation\Rules\OneOfRule#validate');
}

Passing multiple callback in Code Igniter form validation rules

I want to pass multiple callbacks in codeigniter form validation rules.... but only one of the callbacks work
I am using this syntax in my contoller
$this->form_validation->set_rules(
array(
'field' => 'field_name',
'label' => 'Field Name',
'rules' => 'callback_fieldcallback_1|callback_fieldcallback_2[param]',
'errors' => array(
'fieldcallback_1' => 'Error message for rule 1.',
'fieldcallback_2' => 'Error message for rule 2.',
)
),
);
and the callback functions are....
function fieldcallback_1 (){
if(condition == TRUE){
return TRUE;
} else {
return FALSE;
}
}
function fieldcallback_2 ($param){
if(condition == TRUE){
return TRUE;
} else {
return FALSE;
}
}
Someone please help me out with this problem.... any other solutions regarding passing multiple callbacks in form validation rules are also appreciated...
All validation routines must have at least one argument which is the value of the field to be validated. So, a callback that has no extra arguments should be defined like this.
function fieldcallback_1($str){
return ($str === "someValue");
}
A callback that requires two arguments is defined like this
function fieldcallback_2 ($str, $param){
//are they the same value?
if($str === $param){
return TRUE;
} else {
$this->form_validation->set_message('fieldcallback_2', 'Error message for rule 2.');
//Note: `set_message()` rule name (first argument) should not include the prefix "callback_"
return FALSE;
}
Maybe like this?
$this->form_validation->set_rules(
array(
'field' => 'field_name',
'label' => 'Field Name',
'rules' => 'callback_fieldcallback_1[param]'),
);
// Functions for rules
function fieldcallback_1 ($param){
if(condition == TRUE){
return fieldcallback_2($param);
} else {
$this->form_validation->set_message('callback_fieldcallback_1', 'Error message for rule 1.');
return FALSE;
}
}
function fieldcallback_2 ($param){
if(condition == TRUE){
return TRUE;
} else {
$this->form_validation->set_message('callback_fieldcallback_1', 'Error message for rule 2.');
return FALSE;
}
}

Laravel Validation between not converting comma separated string to array?

Just trying to do some tags validation. First checking for number of tags (as comma separated list of id's).
class SomePostRequest extends Request
{
public function rules()
{
return [
'tags' => 'between:2,5'
];
}
public function all()
{
$input = parent::all();
$input['tags'] = explode(',', #$input['tags']);
return $input;
}
}
It keeps spititing out the message as
The tags must be between 2 and 5 characters.
Instead of the proper array message:
The :attribute must have between :min and :max items.
Try to use custom validation rule for you requirement:
$this->app['validator']->extend('tag', function ($attribute, $value, $parameters)
{
$tags = explode(',', $value);
if(count($tags) >= 2 || count($tags) <= 5)){
return false;
}
});
and then rules would be
public function rules(){
return [
'tags' => ['tag']
];
}
and message can update by
public function messages() {
return [
'tags.tag' => 'The :attribute must have between :min and :max items.'];
}
I hope, you get basic idea to achieve this requirement, let me know if its helps.

Custom Callback Validation Function to Achieve a Custom Error Message

I have a CI form with a field requiring a decimal number. Currently when the field fails validation the user gets an unhelpful message. "The field must be decimal". This is a poor user experience for a user that feels they should be able to use a leading period. e.g. ".4". I am trying to create a Custom callback validation function to achieve a custom error message. Here is my controller (simplified)...
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('expenses', 'Expenses', 'trim|max_length[50]|callback_decimalcustom|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$parent_data = array('country' => $countrydata, 'currency' => $currencydata, 'tour' => $tourdata, 'riders' => $ridersdata, 'measurement' => $measurementdata, 'tourdistance' => $tourdistance);
$this->load->view('myform', $parent_data);
}
else
{
$sql= array (
'expenses'=>$this->input->post('expenses'),
);
$ins = $this->db->insert('donations',$sql);
$this->load->view('formsuccess');
}
}
public function decimalcustom($str) //Custom decimal message
{
if (preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str))
{
$this->form_validation->set_message('decimalcustom', 'The %s field is required in 0.00 format.');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
When testing, the error is not thrown, ever since I changed the validation from decimal to decimal custom. Am I missing something?
preg_match() returns TRUE when it's a valid number but you're trying to throw an error. Do the oposite.. (note the exclamation mark before preg_match)
if ( !preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str) ) {
$this->form_validation->set_message('decimalcustom', 'The %s field is required in 0.00 format.');
return FALSE;
}
else {
return TRUE;
}

Adding custom callback to Codeigniter Form Validation

I want to limit my registration to emails with #mywork.com I made the following in My_Form_validation.
public function email_check($email)
{
$findme='mywork.com';
$pos = strpos($email,$findme);
if ($pos===FALSE)
{
$this->CI->form_validation->set_message('email_check', "The %s field does not have our email.");
return FALSE;
}
else
{
return TRUE;
}
}
I use it as follows. I use CI rules for username and password and it works, for email it accepts any email address. Any I appreciate any help.
function register_form($container)
{
....
....
/ Set Rules
$config = array(
...//for username
// for email
array(
'field'=>'email',
'label'=>$this->CI->lang->line('userlib_email'),
'rules'=>"trim|required|max_length[254]|valid_email|callback_email_check|callback_spare_email"
),
...// for password
);
$this->CI->form_validation->set_rules($config);
The problem with creating a callback directly in the controller is that it is now accessible in the url by calling http://localhost/yourapp/yourcontroller/yourcallback which isn't desirable. There is a more modular approach that tucks your validation rules away into configuration files. I recommend:
Your controller:
<?php
class Your_Controller extends CI_Controller{
function submit_signup(){
$this->load->library('form_validation');
if(!$this->form_validation->run('submit_signup')){
//error
}
else{
$p = $this->input->post();
//insert $p into database....
}
}
}
application/config/form_validation.php:
<?php
$config = array
(
//this array key matches what you passed into run()
'submit_signup' => array
(
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required|max_length[255]|valid_email|belongstowork'
)
/*
,
array(
...
)
*/
)
//you would add more run() routines here, for separate form submissions.
);
application/libraries/MY_Form_validation.php:
<?php
class MY_Form_validation extends CI_Form_validation{
function __construct($config = array()){
parent::__construct($config);
}
function belongstowork($email){
$endsWith = "#mywork.com";
//see: http://stackoverflow.com/a/619725/568884
return substr_compare($endsWith, $email, -strlen($email), strlen($email)) === 0;
}
}
application/language/english/form_validation_lang.php:
Add: $lang['belongstowork'] = "Sorry, the email must belong to work.";
Are you need validation something like this in a Codeigniter callback function?
$this->form_validation->set_rules('email', 'email', 'trim|required|max_length[254]|valid_email|xss_clean|callback_spare_email[' . $this->input->post('email') . ']');
if ($this->form_validation->run() == FALSE)
{
// failed
echo 'FAIL';
}
else
{
// success
echo 'GOOD';
}
function spare_email($str)
{
// if first_item and second_item are equal
if(stristr($str, '#mywork.com') !== FALSE)
{
// success
return $str;
}
else
{
// set error message
$this->form_validation->set_message('spare_email', 'No match');
// return fail
return FALSE;
}
}
A correction to Jordan's answer, the language file that you need to edit should be located in
system/language/english/form_validation_lang.php
not application/.../form_validation_lang.php. If you create the new file under the application path with the same name, it will overwrite the original in the system path. Thus you will lose all the usage of the original filters.

Categories