sfValidatorRegex not working - validation

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'

Related

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 updateOrInsert with 'OR' or 'AND' operator in first arguments?

While it is possible to have multiple arguments for the updateOrInsert in Laravel query builder and what is the operator used by default.
For example in the documentation it is mentioned:
DB::table('users')
->updateOrInsert(
['email' => 'john#example.com', 'name' => 'John'],
['votes' => '2']
);
Does that mean that email && name are checked or does it mean email || name is checked? How can we control it for one or the other if required?
Please forgive me if this is a silly question or if it is not worded as per the correct vocabulary, as I am new to Laravel. I couldn't find this information in the documentation or API.
updateOrInsert() method is used to update an existing record in the database if matching the condition or create if no matching record exists. Its return type is Boolean.
Syntax :
DB::table('blogs')->updateOrInsert(
[Conditions],
[fields with value]
);
In your query :
DB::table('users')->updateOrInsert(
['email' => 'john#example.com', 'name' => 'John'],
['votes' => '2']
);
It will check if email == 'john#example.com' & name == 'john', then it will update votes=2.

Any way to except empty field from $request->all() in laravel?

I want to except empty value field from $request->all();
Array ( [first_name] => Dev 1 [password] => [last_name] => [phone] => 123456 [password_confirmation] => )
I got the array like this but I want to except field from above array like last_name, password which has no value.
Any way to do this without for loop.
I mean laravel provide any default method for that ?
Thanks
array_filter will remove empty elements:
$filtered = array_filter($request->all());

Yii Validation; combining validators with OR logic

Yii built-in validation returns invalid if one of the set of validators in the array fails,
e.g.
array(
array('A', 'required'),
array('B', 'required')
)
will return invalid if either A or B is empty. (AND logic)
I would like to combine these validators so that it will return invalid only if all individual validators return invalid (OR logic).
The example above would return invalid only in the case that both A and B are empty.
Is there a generic built-in way to do this?
If not I am expecting to have to extend the library to handle something like the following:
array(
array('', 'COrValidator',
array('A', 'required'),
array('B', 'required')
)
)
You may want to create an AnyValidValidator so that you can do something like this:
array(
array('attribute1, attibute2', 'anyValid', array('required')),
);
The problem with
array(
array('', 'COrValidator',
array('A', 'required'),
array('B', 'required')
)
)
is that, without significant modification of the validator functionality, the COrValidator validator will not be applied to any attributes (no attributes specified).

uri_to_assoc issue

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

Resources