uri_to_assoc issue - codeigniter

This is my code
$default = array('location', 'id', 'page');$url_info = $this->uri->uri_to_assoc(3, $default); var_dump($url_info);
If my url is
http://localhost/cidbg/test/uritest/location/india/page/8/id/58
Then my $url_info is ok.
array 'location' => string 'india' (length=5) 'page' => string '8' (length=1) 'id' => string '58' (length=2)
But when my url is
http://localhost/cidbg/test/uritest/location/india/page//id/58
Then my $url_info is like this.
array 'location' => string 'india' (length=5) 'page' => string 'id' (length=2) 58 => boolean false 'id' => boolean false
The page variable is missing there. Actually am expecting page to FALSE. Is there anyway to achive this? I mean if a value is missing, then that name should be false.

Apache will automatically convert double slash to a single slash. So
http://localhost/cidbg/test/uritest/location/india/page//id/58 is same as http://localhost/cidbg/test/uritest/location/india/page/id/58
So you can either search for a complicated way to retaining the double slash using .htaccess (I'm not sure if that's possible) or you can pass an extra param (like 0) instead of empty.
http://localhost/cidbg/test/uritest/location/india/page/0/id/58

Related

Require a field if the value exists in another field, which is an array

Say a model has a status property, that holds an array of strings. If this array contains a string name "Other", the status_other field should be required. I can achieve this with the following rules:
'status' => 'nullable|array',
'status_other' => Rule::requiredIf(in_array('Other', $this->model->status))
Is there a way to write the status_other rule as a string? I tried:
'status_other' => 'required_if:status,in:Other',
and
'status_other' => 'required_if:status,Other',
Both that did not work.
You can compare it by using '==' to match the string in the array.
'status_other' => 'required_if:status,==,Other',

How to check for null in other field laravel FormRequest?

I am trying to exclude a field from being validated when another field has a value (not null). In this case, i want 'deleted_pictures' to be excluded if product_pictures is an array (not null). My problem is that i think exclude_unless:product_pictures,null evaluates null as a string, and not as, well, null.
This is my rule.
ProductRequest.php
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,null', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless product_pictures has a value of 'null'
I tried this to confirm my suspicion and it works like it should.
//test_field = 'some_value'
return [
'test_field' => 'required|string'
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:test_field,some_value', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless test_field has a value of 'some_value'
In my first case, deleted_pictures is excluded because it doesn't detect that product_pictures is 'null' (string)
While on the second case, deleted_pictures is NOT excluded because test_field matches the given value.
My question is, how do you evaluate null value in FormRequest Laravel?
So apparently you can just leave the second parameter blank to evaluate it as null
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,', 'required', new cant_delete_all($max,'1')],
];
I'm not sure if this is how its supposed to be done or intended behavior. But im just gonna leave this answer just in case someone might need it. If someone can suggest the 'proper' way of doing it then I'll accept that instead.

Laravel Validator

I have the following rule in the validator:
'keywords' => 'array|required'
'date_intervals' => 'array|required'
The array needs to be populated with at minimum 1 element (should not be empty).
Is there an existing rule to achieve this, or is it required that I write a custom rule for it?
Does min:1 work with array validation?
Thanks.
No, you'd be better counting the array elements and then passing the count for validation.
$count = count(Input::get('keywords'));
$input = ['keywords' => Input::get('keywords'),'count' => $count];
$rules = ['keywords' => 'required|array', 'count' => 'min:1'];

Yii composite key in relatins with custom params

I want to use composite key in ActiveRecord.
I got two tables.
Quotes and Comments.
Quotes contains pk - id;
Comments pk is composite - module, section, cid
module - module name, where comments come from.
section - section of this module
cid - identificator, in this situaction this is id of quote.
In comments I defined primary key like so.
public function primaryKey()
{
return array('module', 'section', 'cid');
}
Next one, I want to get those records, what related to quotes.
So, in Quotes I declared relation:
'comments' => array(self::HAS_MANY, 'Comment', 'module, section, cid', 'params' => array(
':ypl0' => '"quotes"',
':ypl1' => '"quote"',
':ypl2' => 'id'
)),
The required result is:
SELECT * FROM quotes q
LEFT JOIN comments c ON (c.cid = q.id AND module = "quotes" AND section = "quote")
WHERE c.id IS NULL
SQL is working, relation - not. What I'm doing wrong?
Try the following untested code
'comments' => array(self::HAS_MANY, 'Comment', 'cid','condition'=>'module=:param1 AND section=:param2','params' => array(':param1' => 'quotes',':param2' => 'quote',)),

sfValidatorRegex not working

I am trying validate a username field on a register form so that a username can only be made up of letters. However, when I use the sfValidatorRegex(), it always returns invalid ("johnny" will return invalid, as does "JoHnNy"). Here's the code I'm using:
// From RegisterForm.class.php
$this->validatorSchema['username'] = new sfValidatorRegex(
array(
'pattern' => '[A-Za-z]',
),
array(
'invalid' => 'only lowercase letters',
)
);
What am I missing?
Didn't understand whether you wanted lower or uppercase but try:
Only lowercase: '/^[a-z]*$/'
Lower or uppercase: '/^[A-z]*$/i'

Resources