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.

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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

2 Comments

How do you get the error messages from $userValidator after your assert (if there are any)?
@Shackrock if the assert() fails, it throws an exception of type Respect\Validation\Exceptions\NestedValidationException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.