简体   繁体   中英

php respect validation just validate if not empty

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM