Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validation optional embed attributes #716

Closed
gimler opened this issue Aug 15, 2016 · 3 comments
Closed

validation optional embed attributes #716

gimler opened this issue Aug 15, 2016 · 3 comments

Comments

@gimler
Copy link

gimler commented Aug 15, 2016

i try to validate a optional embed object in my main object. But it doesn't work.

  • embed is optional
  • embed is set then it must have a required value

validation code

v::attribute('name', v::length(1, 256))
    ->attribute('embed', v::attribute('required', v::length(1, 256))
            ->attribute('optional', v::length(1, 256), false)
    , false);
@batusa
Copy link
Member

batusa commented Aug 16, 2016

use Respect\Validation\Validator as v;

class Main
{
    public $embed;
}

class Required
{
    public $required;
}

$requiredTrue = new Required();
$requiredTrue->required = 'here we go';

$mainTrue1 = new Main();

$mainTrue2 = new Main();
$mainTrue2->embed = $requiredTrue;

$requiredFalse = new Required();

$mainFalse = new Main();
$mainFalse->embed = $requiredFalse;

$validator = v::attribute(
    'embed', 
    v::optional(
        v::attribute(
            'required', 
            v::length(1, 256)
        )
    )
);

var_dump($validator->validate($mainTrue1)); // bool(true)
var_dump($validator->validate($mainTrue2)); // bool(true)
var_dump($validator->validate($mainFalse)); // bool(false)

I hope this is what you're looking for

@gimler
Copy link
Author

gimler commented Aug 16, 2016

I found that i must use the mandatory paramter because i encode json data. So the property is not set. In the testcase it works, so i must look why it doesn't work in my bigger example ;( thx

use Respect\Validation\Validator as v;

$requiredTrue = new stdclass();
$requiredTrue->required = 'here we go';


$mainTrue1 = new stdclass();


$mainTrue2 = new stdclass();
$mainTrue2->embed = $requiredTrue;


$requiredFalse = new stdclass();

$mainFalse = new stdclass();
$mainFalse->embed = $requiredFalse;

$validator = v::attribute(
    'embed',
    v::optional(
        v::attribute(
            'required',
            v::length(1, 256)
        )
    ), false
);

var_dump($validator->validate($mainTrue1)); // bool(true)
var_dump($validator->validate($mainTrue2)); // bool(true)
var_dump($validator->validate($mainFalse)); // bool(false)

@henriquemoody
Copy link
Member

I'm closing issues that are too old, and I'm not really looking at them all. If this is still relevant, please comment here and I will reopen this issue.

Thanks for all your comments! 🐼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants