Check for at least one field using CakePHP 3 check

I am trying to write a validation rule in CakePHP 3 that checks to see if a first name, last name or company name is given.

validators:

$validator
    ->add('prename', 'custom', [
        'rule' => [$this, 'validateName'],
        'message' => __('Prename and lastname OR company name must be set.')
    ]);

$validator
    ->add('lastname', 'custom', [
        'rule' => [$this, 'validateName'],
        'message' => __('Prename and lastname OR company name must be set.')
    ]);

$validator
    ->add('name', 'custom', [
        'rule' => [$this, 'validateName'],
        'message' => __('Prename and lastname OR company name must be set.')
    ]);

Rule Definition:

public function validateName($check, array $context)
{
    if((!empty($context['data']['prename']) && !empty($context['data']['lastname'])) || !empty($context['data']['name'])){
        return true;
    } else {
        return false;
    }
}

But the check does not behave as expected. If I enter the company name, I will receive validation errors for the first and last name, indicating that the fields are required. The same thing, when I enter the name and surname, he says that the name of the company is required.

What am I doing wrong?

+4
source share
5 answers

100%, , . , CakePHP 3.

CustomersTable.php:

public function validationDefault(Validator $validator)
{
    $validator->notEmpty('name', __("Required when prename and lastname is empty."), function ($context) {
        return !$context['data']['prename'] && !$context['data']['lastname'];
    });

    $validator->notEmpty('prename', __('Required when company or lastname is empty.'), function ($context) {
        return !$context['data']['name'];
    });

    $validator->notEmpty('lastname', __('Required if company or prename is empty.'), function ($context) {
        return !$context['data']['name'];
    });

    return $validator;
}

. , , . , required => false:

add.ctp:

echo $this->Form->input('prename', [
   'label' => ['text' => __('Prename (*): ')],
   'required' => false,
]);

echo $this->Form->input('lastname', [
   'label' => ['text' => __('Lastname (*): ')],
   'required' => false,
]);

echo $this->Form->input('name', [
   'label' => ['text' => __('Company name (*): ')],
   'required' => false,
]);
0

cakePhp 3.x.I .

    $validator
        ->notEmpty('company_name', 'Prename and lastname OR company name must be set.', function ($context){      
            if((!empty($context['data']['prename']) && !empty($context['data']['lastname'])) || !empty($context['data']['name'])){

                return false;
            } else {
                return true;
            }
        });

    $validator
        ->notEmpty('prename', 'Prename and lastname OR company name must be set.', function ($context){
            if((!empty($context['data']['prename']) && !empty($context['data']['lastname'])) || !empty($context['data']['name'])){

                return false;
            } else {
                return true;
            }
        });
    $validator
        ->notEmpty('lastname', 'Prename and lastname OR company name must be set.', function ($context){
           if((!empty($context['data']['prename']) && !empty($context['data']['lastname'])) || !empty($context['data']['name'])){
                return false;
            } else {

                return true;
            }
        });

, .

+2

, , :

$validator
    ->add('various_fee_text', 'custom', [
        'rule' => [$this, 'variousFieldsChecker'],
        'message' => __('Udfyld bΓ₯de Tekst og DKK feltet'),
    ])
    ->allowEmpty('various_fee_text');

Vaildation:

/**
 * Method checks whether or not various fee text and various fee prices are either both empty, both set or "dirt",
 * meaning only one of them is set and the other is empty. We want either both to be set or both to be empty.
 *
 * @return bool depending on it OK or not
 */
public function variousFieldsChecker($value, $context)
{
    $varioustext = $context['data']['various_fee_text'];
    $variousPrice = $context['data']['various_fee_price'];

    if (!empty($varioustext) && !empty($variousPrice)) {
        return true;
    } elseif ((empty($varioustext) && empty($variousPrice))) {
        return true;
    }

    return false;
}

, ->allowEmpty(), .

0

To do this, CakePhp provides the creation of custom validation. You need to create two authentication functions in your model class and conditionally call them a controller function. You can create a check function as

    public function validationName($validator){

       $validator
       ->requirePresence('prename')
       ->notEmpty('prename', 'Prename must be set.')

       ->requirePresence('lastname')
       ->notEmpty('lastname', 'Lastname must be set.')

       ->allowEmpty('name');

       return $validator;
   }



public function validationCompany($validator){

       $validator
       ->requirePresence('name')
       ->notEmpty('name', 'Company name must be set.')

       ->allowEmpty('prename')
       ->allowEmpty('lastname');

       return $validator;
   }

In the controller, you can call these functions conditionally, like

$validator = 'company';
if(empty($this->request->data[''])){
  $validator = 'name';
}
$entity = $this->{$this->modelClass}->newEntity();
$entitydata = $this->{$this->modelClass}->patchEntity($entity, $this->request->data, ['validate' => $validator]);

$this->{$this->modelClass}->save($entitydata);
0
source

You need to break through the table name, or check the input identifier in the html view part.

docs cake3.3

-2
source

Source: https://habr.com/ru/post/1663971/


All Articles