0
votes

I am using cakephp 1.3, and want to set form inputs as "required" explicitly instead of relying on model validation. A form input example:

<div class="input text required">
  <label for="ClaimClaimantFirstName">First Name</label>
  <input name="data[ClaimClaimant][first_name]" type="text" id="ClaimClaimantFirstName" /> 
</div>

I also want to maintain the FormHelper naming instead of using custom form helper names. Example:

$this->Form->input(...)

The solution I came up with is

  1. implement MyFormHelper, extending from FormHelper and overriding the input method. Specifically, around line 804 of the FormHelper, replacing

    if (
      isset($this->fieldset[$modelKey]) 
      && in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
    ) {
      $divOptions = $this->addClass($divOptions, 'required');
    }
    

    with

    if (isset($options['required'])) {
      if ($options['required'] === true) {
        $divOptions = $this->addClass($divOptions, 'required');
      } elseif ($options['required'] === false) {
        // do not add class 'required'
      }
    } elseif (
      isset($this->fieldset[$modelKey]) 
      && in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
    ) {
      $divOptions = $this->addClass($divOptions, 'required');
    }
    

    This ensures the presence of $options['required'] takes precedence before we rely on the model validation.

  2. use Joe Beeson's analogue plugin, to alias MyForm to Form:

    public $helpers = array(
      'Analogue.Analogue' => array(
        array(
          'helper' => 'MyForm',
          'rename' => 'Form'
    ) ) )
    
  3. then, specifying the form input as required looks like:

    $this->Form->Input(
      'SomeModel.SomeField',
      array('required' => true)
    )
    

Is there some other better approach than this, or potential issues with this solution?

1
so you want to just have the class required in your div? - Colby Guyer
for a better solution try $this->Form->input('foo', array('div'=>array('class'=>'required'))); but really why go to this trouble when it's in cake out of the box? What are you trying to achieve. - Ross
@Ross using 'div' => array('class' => 'required') does not keep the other classes such as 'text' and 'input' - EricC
@ColbyGuyer I want to have an explicit way to specify whether a form input is required or not (expressed as class required in div). The explicit way overrides the model validation way of determining if an input is required. - EricC
it sounds like you just want a way for the field to stand out as required. Why not apply your own css. do the 'class'=>'requiredclass' or even an 'after'=>'*' to denote required. - Colby Guyer

1 Answers

0
votes

The original question specifies 1.3, but here is an update for those looking for a way in 2.0

For Cake 2.0 or greater you can specify the 'required'=>true keypair value in your input options array to enable html5 browser validation on the element.

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#creating-form-elements