Ad

OctoberCMS Validation Issue For Required But Dependent Field With Asterisk

I am using OctoberCMS and I have used Builder plugin to create one of my plugins called as Properties which works fine so far.

The Thing is, I have 2 fields called as authority and auction_date.

authority is a drop-down field which has options like auction and etc.. whereas auction_date is simple a date field.

auction_date field depends on authority field and it is required only if authority field has a selected value called as auction and authority field is not required at the same time. Hence I put below code in my Property plugin's model file.

Model File - Property.php

public $rules = [
        'auction_date' => 'required_if:authority,==,auction',
    ];

This works well, I am able to validate my auction_date field if authority drop-down's value selected auction from the list.

However, here basically auction_date is not a required field by default. It is dependent field of authority.

Yet the field showing asterisk(*) beside this field when I load the page. Here below how it looks like.

enter image description here

I have tried to update my plugin's fields.yaml file my putting below code.

auction_date:
            label: 'Auction Date:'
            oc.commentPosition: ''
            mode: date
            span: auto
            type: datepicker
            tab: 'Address Information'
            required: false 

This code required: false did not work for me.

I came across the solution to update below system library files.

  • modules/backend/classes/FormField.php
  • modules/backend/widgets/Form.php

And doing this, Field definition should override magic.

But frankly, I do not want to mess with any system file here. It would be ideal if I can find some solution which can deal this thing inside one of plugin files.

If someone guide me to accomplish this, it would be awesome.

Thanks in advance.

Ad

Answer

You can wait for the next release or override the method used to determine if something is required.

public function filterFields($fields)
{
    $fields->auction_date->required = false;
}
Ad
source: stackoverflow.com
Ad