1

I am a newbie in zendframework. I am using zendframework version 2.

I have a ClientForm class which is inheriting from Zend\Form\Form.

class Client extends Form
{
    public function __construct($name = null, $options = array())
    {
        if (null == $name) $name = 'ClientFrom';
        parent::__construct($name, $options);

        $this->add(array(
            'name' => 'clientName',
            'type' => 'Text'
        ));

        $this->add(array(
            'name' => 'address1',
            'type' => 'Text'
        ));
    }
}

I need to implement validation and filtering for the above form.

Rules for Validation

'clientName' => required, min = 3, max = 25

Rules for Filter

'clientName' => [a-zA-Z0-9_ ]

Questions

  1. Can I implement this rules and filter in the same class as Form (without creating a new input filter class)
  2. Please show me example on how to implement above rules on my above class.

Thank you.

0

3 Answers 3

2

Try this:

    $this->add(array(
        'name' => 'clientName',
        'type' => 'text',
        'required' => true,             
        'validators' => array(
             new Validator\RegexValidator('/^#[a-zA-Z0-9_ ]$/'),
             new Validator\StringLength(array('min'=>3 ,'max' => 25))
         )             
    )); 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm curious, does this actually work? I can't see this going by the source alone Oo
1

There is the Zend\InputFilter-Component that you'd use for this kind of task. There's a lot of examples out there, some programatical ones like here or some configurational ones like here or here.

The later two being examples used for specific Doctrine-Validators, but you can use them for any normal Zend\InputFilter, too.

3 Comments

I went through several example all are abstract. Can you just implement and show me for the above code?
I'm at work right now and not yet in the mood. Please see the later links. They document VERY WELL how the Zend\Form component should be used. It will help you a great deal! PS: Your "Rules for Filter" is actually a "Rule for Validation" ;) Filters are things like StringTrim, StripTags, etc..
If the downvoters would care to explain there vote, that'd be great. The resources I gave are the most suggested way to implement InputFilters for Forms. Everything else is basically non-OOP-workarounds.
0

I suppose you have no code yourself yet ... well.

'clientName' => required, min = 3, max = 25

This one isnt too difficult, heres some easy code ... ill write it extremely simple and readable, you can shorten the if-statements if you want:

if(!empty($clientName)) {
    if(!($clientName > 25)) {
        if(!($clientName < 3)) {
            echo "valid name!";
        }
    }
} else {
    return false;
}

'clientName' => [a-zA-Z0-9_ ]

Either you use RegEx or string-functions where you only allow these specific characters.

5 Comments

I know, but i suppose that its the easiest one to understand.
i can do this way for normal php project. instead zend provide some fine grained solutions for validation and filtering. so i need help on that pattern.
Thats what i even wrote: "you can shorten the if-statements if you want". Ofc you can write all of it in one if statement, but if the TE asks for such a code, i suppose this code is easier than some difficult to read code.
@user247947 Woops, Zend-Framework ... i completely ignored that. Got no experience with Zend. Ill retreat in shame. :>
mhh... put your hand on zf and see .. its awesome!

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.