2

I'm using php respect validation. https://github.com/Respect/Validation

class UserValidator implements iValidator
{

    public function validate($object)
    {

        $userValidator = v::attribute('email', v::email())
            ->attribute('mobile', v::numeric()->length(10,12))
            ->attribute('firstName', v::stringType()->length(5,255))
            ->attribute('lastName', v::stringType()->length(5,255))
            ->attribute('userName', v::stringType()->length(5,255)->unique('users'))
            ->attribute('password', v::stringType()->length(8,16));

        $userValidator->assert($object);

    }

}

and this is my user validation code.

username and password are only required fields. I want them to be required and other field validation should be applied only if user has filled in the input. what should I do ? after all I've implemented my own rule (unique) if it's necessary I'm ready to develop new rules or whatever. also if you know better php package for input validation I'm all ears.

Majid Abdolhosseini
  • 2,191
  • 4
  • 31
  • 59

3 Answers3

4

This is an old question but the optional() method will work.

class UserValidator implements iValidator
{

    public function validate($object)
    {

        $userValidator = v::attribute('email', v::optional(v::email()))
            ->attribute('mobile', v::optional(v::numeric()->length(10,12)))
            ->attribute('firstName', v::optional(v::stringType()->length(5,255)))
            ->attribute('lastName', v::optional(v::stringType()->length(5,255)))
            ->attribute('userName', v::stringType()->length(5,255)->unique('users'))
            ->attribute('password', v::stringType()->length(8,16));

        $userValidator->assert($object);

    }

}

See the docs: https://github.com/Respect/Validation/blob/1.1/docs/Optional.md

PW_Parsons
  • 1,173
  • 2
  • 12
  • 19
1

I have not used this validation package. This may not be the best possible answer, but this is what I get from the package documentations.

I think method oneOf() would help you, as it acts as an OR operator. The other method that would help, is nullType() which validates if the input is null. you can group what you have with nullType() to make the field optional.

class UserValidator implements iValidator
{

    public function validate($object)
    {

        $userValidator = v::attribute('email', v::email())
            ->attribute('mobile', v::oneOf(
                v::numeric()->length(10,12),
                v::nullType
            ))
            ->attribute('firstName', v::oneOf(
                v::stringType()->length(5,255),
                v::nullType
            ))
            ->attribute('lastName', v::oneOf(
                v::stringType()->length(5,255),
                v::nullType
            ))
            ->attribute('userName', v::stringType()->length(5,255)->unique('users'))
            ->attribute('password', v::oneOf(
                v::stringType()->length(8,16),
                v::nullType
            ));

        $userValidator->assert($object);

    }

}

I haven't test it, but I think it'd work.

Behzadsh
  • 851
  • 1
  • 14
  • 30
  • thanks to you I fixed it in a hacky way. by default if no value provided for a field its not part of object so its not null, I did foreach and put null for each field which is not provided. I'll post my code here in your answer. – Majid Abdolhosseini Mar 05 '16 at 20:24
1

I know this is old, but I run into this question and there's an easier way to do what you want:

class UserValidator implements iValidator
{
    public function validate($object)
    {
        $userValidator = v::attribute('email', v::email(), false)
            ->attribute('mobile', v::numeric()->length(10,12), false)
            ->attribute('firstName', v::stringType()->length(5,255), false)
            ->attribute('lastName', v::stringType()->length(5,255), false)
            ->attribute('userName', v::stringType()->length(5,255)->unique('users'))
            ->attribute('password', v::stringType()->length(8,16));

        $userValidator->assert($object);
    }

}

There's an optional third parameter in the attribute() method to make the validation mandatory or not:

https://github.com/Respect/Validation/blob/master/docs/Attribute.md

Lucas
  • 111
  • 5
  • How do you get the error messages from `$userValidator` after your assert (if there are any)? – Shackrock Feb 15 '17 at 22:04
  • @Shackrock if the assert() fails, it throws an exception of type `Respect\Validation\Exceptions\NestedValidationException` – Lucas Feb 16 '17 at 22:58