Validation of phone numbers containing '+' and '-' characters - codeigniter

In CodeIgniter, how do i validate phone numbers containing '+' and '-' symbols?

You cannot enter a number with "-" since you defined integer as the validation rule. Therefore, the validation will fail. You need to work with RegEx so that you can create more complex validation. See the topic on validations in the CI manual for more info.
Your validation rule:
$this->form_validation->set_rules('foo', 'Number', 'callback_number_validation');
Note how the keyword callback_ is used to identify CI your function for validation.
Your callback function:
//$str will be the value you want to verify
function number_validation($str) {
return preg_match("your_regex", $str) ? true: false;
}

Related

How to make a validation rule in laravel similar to if (condition 1) and (condition 2), true

I have the following validation rule in my controller
"holder_name" => 'required_if:paymentMethod,==,credit_card',
That is, if paymentMethod = credit_card then holder_name is required / mandatory.
Now I need one more condition to be checked.
I'll give the example as it would be in pure php, using if
For example I need to check if (paymentMethod = credit_card and card_id! = Null)
I already tried using two required_if:
'required_if:paymentMethod,==,credit_card'|'required_if:card_id,!=,null'
But it does not work.
Use the following rule for your validation.
The required_with rule mandates a field if any of listed ones are non-empty.
'required_if:paymentMethod,credit_card|required_with:card_id'

Salesforce Opportunity Validation Rule

I am working on a validation rule on the Opportunity object. The goal is to bypass a validation rule if an item from a Multi-Select picklist is selected, otherwise the code should fire
Here's what I have so far - everything works except the exception portion - NOT(CONTAINS('Campaign_Tactic__c','Call Monitoring')))
So the validation rule should fire unless the Call Monitoring multi-select is chosen.
The Validation Rule should prevent an Opportunity Save if:
Opportunity Record Type = True
Opportunity Stage = Closed Won or Proposal Request
Landing Page Setup Field = blank/no data
Campaign Tactic Multi-Select field = does not contain the Call Monitoring Option
AND (
RecordType.DeveloperName = "New_Opportunity",
OR (
ISPICKVAL(StageName, "Closed Won"),
ISPICKVAL(StageName, "Proposal Request")
),
AND (
ISBLANK(TEXT(Landing_Page_Setup__c))
),
(!CONTAINS('Campaign_Tactic__c','Call Monitoring'))
)
From the Salesforce Formula Function Reference, it's important to note
The CONTAINS function does not support multi-select picklists. Use INCLUDES to see if a multi-select picklist has a specific value.
Additionally, the first argument you're providing to CONTAINS() is a quoted string literal, not a field reference, and the outer parentheses are extraneous:
(!CONTAINS('Campaign_Tactic__c','Call Monitoring'))
Your final clause should use INCLUDES(), as:
!INCLUDES(Campaign_Tactic__c, 'Call Monitoring')
Note also that AND() with a single argument can be replaced by the argument alone, so
AND (
ISBLANK(TEXT(Landing_Page_Setup__c))
),
reduces to
ISBLANK(TEXT(Landing_Page_Setup__c)),
You need the TEXT() call only if that field is a picklist.

required_without not working with other rules

'person.mail' =>'required_without:person.phone|sometimes|email|unique:persons,mail',
'person.phone' => 'required_without:person.mail|sometimes|regex:/[0-9]/|size:10|unique:persons,phone'
i need to validate phone and mail, one of them is mandatory
when the mail is empty and the phone isn't, the validation fails at the email rule and this goes both ways, when the mail is present and phone empty, it fails at the regex rule
how can i stop validation if value is null?
As the laravel docs state:
In some situations, you may wish to run validation checks against a
field only if that field is present in the input array. To quickly
accomplish this, add the sometimes rule to your rule list.
I get the feeling that you actually do post both person[email] and person[phone], in which case sometimes will instruct validation to continue, since the values will then be empty strings (or maybe null) rather than not present. You can conditionally add rules on other assertions than check whether key x exists by creating your own validator, and use its sometimes() method to create your own assertions:
$v = Validator::make($data, [
'person.email' => 'email|unique:persons,mail',
'person.phone' => 'regex:/[0-9]/|size:10|unique:persons,phone',
]);
$v->sometimes('person.email', 'required', function($input) {
return ! $input->get('person.phone');
});
$v->sometimes('person.phone', 'required', function($input) {
return ! $input->get('person.email');
});
The difference here is that the fields are not by default required. So for example, person.phone may either be empty, or must match your regex. If $input->get('person.email') returns a falsy value, person.phone is required after all.
As a note, I think your regex is wrong. It will pass as soon as any character inside person.phone is a number. I think you're looking for something like this:
'person.phone' => 'regex:/^[0-9]{10}$/|unique:persons,phone'
i worked it around like this, it's not the best way, but it works just fine
after the validation i added
if(empty($request->all()['person']['mail']) && empty($request->all()['person']['phone'])){
$validator->errors()->add('person.mail', 'Mail or phone required');
$validator->errors()->add('person.phone', 'Mail or phone required');
return redirect("admin/people-create")->withInput()->withErrors($validator);
}

How to show in CodeIgniter only the second parameter in form validation message?

E.g. I have this line in my language form_validation file:
$lang['min_length'] = "%s must be at least %s characters in length.";
And I want the output to be like this:
$lang['min_length'] = "This field must be at least %s characters in length.";
However, the problem is that it looks like this when echoed:
This field must be at least Your name characters in length.
which is wrong because it takes the first %s instead of the second %s.
How can I force CI to take the second %s? Is it possible?
Since the min_length is a standard function in the Form Validation Library and treated through this library, you can only do so by changing the core library.
But there is an easier way - using a callback function with the Form Validation Library:
$this->form_validation->set_rules('your_field', 'The Field label', 'callback_my_min_length[10]');
Then within your controller, add this:
public function username_check($str, $min_length)
{
if ( mb_strlen($str) >= $min_length)
{
return TRUE;
}
else
{
$this->form_validation->set_message('my_min_length', 'This field must be at least '.$min_length.' characters in length.');
return FALSE;
}
}
This is how I would do it.

Code Igniter Form Validation Min Max Length count of unicode characters?

Curious if anyone knows if codeigniter's form validation built in max_length[n] and min_length[n] functions count unicode characters as 1 character or the sum of all the characters used symbolize the unicode character?
I noticed when I var_dump the string it counts all the characters, just wondering if code igniter or php has a built in function to count unicode characters?
Thanks.
You can make your own callback validation:
$this->form_validation->set_rules('rule', 'The rule', 'callback_checkUnicode');
And the check the unicode string>
public function checkUnicode($string)
{
if (strlen($string) != strlen(utf8_decode($string)))
{
//is unicode: add your own counter condition here
return true;
}
return false
}
Codeigniter uses php's mb_strlen if it's available on your php installation which allows for an encoding parameter to be passed along, otherwise it defaults to php's basic strlength which doesn't allow you to pass the string encoding along. The trouble is that CI doesn't give you the ability to pass along the possible encoding for max_length[n]...
If you need it to compensate for the encoding, you might be better served rolling your own validation with just raw php.

Resources