Welcome Guest, Not a member yet? Register   Sign In
How validate this rules
#1

(This post was last modified: 05-21-2024, 07:52 PM by ozornick. Edit Reason: Format code )

I have form, if checkbox in my form (print) is checked (has value 'yes') i need validate email, if not dont validate

My rules like that


Code:
'print' => [
'label' => ' ',
'rules' => 'permit_empty|in_list[yes]',
],

'email' => [
'label' => ' ',
'rules' => 'required_with[print]|max_length[30]|valid_email'
]


When i valudate without print i got error from valid_email
Reply
#2

It seems not enough to do with rule params:

You can try this, maybe this is not the best way.  as follow:

Put that rules in your model:

PHP Code:
// Validation
    protected $validationRules = [
         'print' => [
             'label' => ' ',
             'rules' => 'permit_empty|in_list[yes]',
          ],

         'email' => [
             'label' => ' ',
             'rules' => 'required_with[print]|max_length[30]|valid_email'
          ]
     
    
]; 

then, in your controller method add exception:
        
PHP Code:
        $data  $this->request->getVar();        
        
        $rulesException 
= [];

        if (empty($data['print'])) {
            $rulesException [] = 'email';
        }

        // Checks whether the submitted data passed the validation rules.
        if (!$this->validateData($data$model->getValidationRules(['except' => $rulesException]))) {
            // The validation fails, so returns the form.
            // do something .. 
        
@xxxx[{::::::::::::::::::::::::::::::::>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB