Development

#1411 (Changing the "required" functionality in validation)

You must first sign up to be able to contribute.

Ticket #1411 (closed defect: invalid)

Opened 1 year ago

Last modified 1 year ago

Changing the "required" functionality in validation

Reported by: sostrow Assigned to:
Priority: major Milestone: 1.0.0
Component: Version: 1.0.0-rc1
Keywords: validate form Cc:
Qualification:

Description

After talking on the irc room for a while I think I've found either a bug or missing functionality.

First I'll explain the problem and use. I need to be validate 3 fields to check that one of the three is filled in. My case:

I have telephone[home], telephone[business], and telephone[cellular]

One of the three of these must be filled in.

What I've tried

  • I tried all the required and pre-built validations, non seem to perform this function.
  • Next it was on to custom validators
    • I created a custom validator called Atleast which is given a number of fields, the field names, and error message and should check to see if atleast one of the fields is validated
    • The problem is that the "execute" function is not called unless the field is filled in. I need it to fail validation if all three are not filled in.
  • I tried the group functionality
    • This seems to be the exact opposite functionality
      • If one of the fields is filled in, it gives the other two fields an error message "required"
      • If none are filled in I receive no errors.

Here are the hasbins of what I was trying to do:

Note: I'm putting this as a major priority because I believe this is a major functionality that will be needed. Thanks if for any help. Please let me know if the functionality already exists and I'm just missing something.

Change History

02/06/07 00:50:25 changed by sostrow

  • status changed from new to closed.
  • resolution set to invalid.

So this is already handled in symfony. For anyone looking at this and needing the same thing here's the VERY EASY and CLEAN answer to the problem.

Three fields:

  • telephone[cellular]
  • telephone[home]
  • telephone[business]

YAML validation file (../register/validate/index.yml):

fields:
  telephone:
    required:
      msg: You must fill in atleast one.

PHP form template (../register/templates/indexError.php):

<!-- BEGIN: Displays a general group validation error -->
<?php if (form_has_error('telephone')): ?>
      <?php echo form_error('telephone'); ?>
<?php endif; ?>
<!-- END: Displays a general group validation error -->

<?php if (form_has_error('telephone{business}')): ?>
      <?php echo form_error('telephone{business}'); ?>
<?php endif; ?>
      <?php echo label_for('telephone[business]', "Business:") ?>
      <?php echo input_tag('telephone[business]', null, 'title="Enter business telephone number" class="Form-medtext"') ?><br />

<?php if (form_has_error('telephone{home}')): ?>
      <?php echo form_error('telephone{home}'); ?>
<?php endif; ?>
      <?php echo label_for('telephone[home]', "Home:") ?>
      <?php echo input_tag('telephone[home]', null, 'title="Enter home telephone number" class="Form-medtext"') ?><br />

<?php if (form_has_error('telephone{cellular}')): ?>
      <?php echo form_error('telephone{cellular}'); ?>
<?php endif; ?>
      <?php echo label_for('telephone[cellular]', "Cellular:") ?>
      <?php echo input_tag('telephone[cellular]', null, 'title="Enter cellular telephone number" class="Form-medtext"') ?><br />