简体   繁体   中英

Conditional validation with Phalcon

There's any way to do something like this using Phalcon's validation feature:

$validation->add('telephone', new PresenceOf( [
    'message'       => 'The telephone is required',
    'onFail'        => //Validate this,
    'onSuccess'     => //Validate that
]));

EDIT:

Well, I asking for this because I have some validations that should run only if 2 or 3 conditions are matched in any order , so the cancelOnFail property wouldn't help me that much here.

Maybe if I create a custom validation group like $validation->add('stuff', new ComplexValidation()) but the order still a problem. The way that this validation system works is quite linear. Taking theses considerations, how can I implement more complex validations with Phalcon?

Yes. Phalcon has a validation system which works similarly. The following code would be used to ensure the 'telephone' field is supplied:

$this->validate(new PresenceOf(
    array(
            "field"   => "telephone",
            "message" => "The telephone is required"
         )
));

This validator is called on the model in the validation() function. According to the documentation , the PresenceOf validator is automatically added for fields marked as 'not null' in the model's table. If I understand this correctly, you actually do not need to insert this code if your field is 'not null', though you can do so if you want to override the default 'validation failed' message for this specific form. You can modify the default validation message (and supply translations) by overriding the getMessages() function in your model. There's additional information on Phalcon's validation message system here .

The failure message is aggregated with other messages from other field validations (such as Uniqueness , for example). For each failed validation, you can access the message, field, type (and originating model). The following code would output all the information contained in the message, for each failed validation:

if ($form->save() == false) {
    foreach ($form->getMessages() as $message) {
        echo "Message: ", $message->getMessage();
        echo "Field: ", $message->getField();
        echo "Type: ", $message->getType();            
        echo "Model: ", $message->getModel();
    }
}

I've noticed two different approaches to informing users of validation messages: placed together before the form (as shown here) or placed near the form input itself. If you want to output the message close to the input, you may need to iterate over each message (from getMessages() ) and check for the field name which matches your input, then echo the message using getMessage() .

I'm very new to Phalcon myself, so I haven't explored all the functionality of validators, yet. There is an event system that you can use to insert code beforeValidation and afterValidation . That may be one place to look for outputting a successful validation message.

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