-->

Zend Form Validator Callback: How to exclude a use

2020-08-04 04:15发布

问题:

I have written one function in User Mapper class userExist($username) which return true if same username does not exist in my database table. Now I want to use this function in the Add_User_Form class which extends Zend_Form.

I want to use:

$username = $this->createElement('text','username');
$username->setLabel('Username:')
         ->addValidator('Callback', true, array('callback'=>userExist???));

Can I use mapper class userExist($username) function as callback function? Please let me know if I am doing something wrong with my implementation. I just want to show error messages if a user is already registered and prevent form submission.

Thank you.

回答1:

Instead of using a callback validator, you can use a standard validator:

$element->addValidator('Db_NoRecordExists', true, array('users', 'username'))

Db_NoRecordExists allows you to check whether a record exists in a specific table of your database.

If a user want to edit his profile, you might want to check if a record exists except itself. Then you should use the same validator as follow:

$where = array('users', 'username', array('field' => 'username', 'value' => $username));
$element->addValidator('Db_NoRecordExists', true, $where)

In these example, users is the name of your database table. username is your column name and $username the username you want to exclude.



回答2:

Bit late but this may be a possible answer to your question.

Date Element example:

I created a date element with a custom Callback validator that calls a method in the same form class.

// Date picker from
$datePickerFrom = new Zend_Form_Element_Text('date_picker_from_date');
$datePickerFrom->setRequired(true)
    ->setDecorators($this->elementDecorators)
    ->setLabel('Valid From')
    ->addFilter('StripTags')
    ->addFilter('StringTrim')
    ->setValue(date("d/m/Y",strtotime(Zend_Date::now())))
    ->addValidator('Callback',true, array('callback' => array($this, 'dateCheck'));
$datePickerFrom->getValidator('Callback')->setMessage("'%value%' Cannot be in the past.");
$this->addElement($datePickerFrom);

If you have your own class with custom validators in it then you can do this:

$myValidatorClass = new \SomeValidaotrClass();
->addValidator('Callback',true, array('callback' => array($myValidatorClass, 'dateCheck'));

In the same form class I created the custom validation method:

public function dateCheck($value, $elements, $option)
{
    $now = new DateTime();
    $fromDate = new DateTime(str_replace('/', '-', $elements['date_picker_from_date']));        

    if ($fromDate < $now) {
        $this->getElement('date_picker_from_date')->getValidator('Callback')->setMessage("'%value%' Cannot be in the past.");
        return false;
    } else (SomethingElse) {
           $this->getElement('date_picker_from_date')->getValidator('Callback')->setMessage("Some other message.");
    }

    return true;
}

Or if you are using your own class:

class SomeValidaotrClass
{
    public function dateCheck($value, $elements, $option)
    {
        // Validation code...
    }
}

If the methods aren't public the Callback will not find them.