0

I want to apply validation rules only for the input fields that are not empty (not required) for the URL

For example, If I submit a form and the input is empty, I got a validation error "Instagram Link must be valid URL.", However, I want it without requiring, and if the input is not empty, I want to apply the rule "valid_url"

How can we fix it?

if (!$this->validate([
        'instagram' => [
            'rules' => 'valid_url',
            'errors' => [
                'valid_url' => 'Instagram Link must be valid url.',
            ],
        ],
    ])){
        return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
    }

I tried with a permit_link rule, but if I submit it (with an input value like 'mylink' (which is not a valid_url)), it will accept it, but it should not.

Please check the following images and the code: A form HTML Result after clicking on edit button

<?= form_open('/settings/edit/1', ['id' => 'setting-form']); ?>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <?= form_label('Instagram'); ?>
                <?= form_input(['name' => 'instagram', 'class' => 'form-control', 'id' => 'instagram', 'placeholder' => 'Enter instagram link', 'value' => old('instagram', $setting->instagram)]); ?>
            </div>
        </div>
    </div>
    <button type="submit" class="btn btn-dark">Edit</button>
    <?= form_close(); ?>


   public function edit($id)
{

    if (!$this->validate([
        'instagram' => [
            'rules' => 'permit_empty|valid_url',
            'errors' => [
                'valid_url' => 'Instagram Link must be valid url.',
            ],
        ],
    ])) {
        return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
    }

    die('submitted');
}

It should display "Instagram Link must be valid url" and not "submitted",

hanafa
  • 3
  • 3
  • 1
    Use [`'permit_empty|valid_url'`](https://codeigniter4.github.io/userguide/libraries/validation.html?highlight=validation#available-rules) – steven7mwesigwa Apr 08 '22 at 04:37
  • Does this answer your question? [Codeigniter 4 Validation for optional values](https://stackoverflow.com/questions/67115299/codeigniter-4-validation-for-optional-values) – steven7mwesigwa Apr 08 '22 at 04:39
  • I tried with permit_empty|valid_url but it won’t valid if the input is valid link or not. – hanafa Apr 08 '22 at 09:40
  • What do you consider a "*valid link*" in your point of view? – steven7mwesigwa Apr 08 '22 at 11:15
  • “mylink” is invalid link while “https://www.mylink.com” is valid. – hanafa Apr 08 '22 at 11:19
  • Well, that's your own perspective. With request to native PHP's [`FILTER_VALIDATE_URL`](https://www.php.net/manual/en/filter.filters.validate.php) filter and CodeIgniter 4's [`valid_url`](https://github.com/codeigniter4/CodeIgniter4/blob/622d50aa2976b85f7cb1479faec7558cd9ec2341/system/Validation/FormatRules.php#L290) validation rule, a valid link **MUST** have a [protocol](https://en.wikipedia.org/wiki/URL) prefix (**http** OR **https**). – steven7mwesigwa Apr 08 '22 at 11:38
  • If that isn't part of your requirements, create a [custom validation rule](https://codeigniter4.github.io/userguide/libraries/validation.html?highlight=validation#creating-custom-rules) instead. – steven7mwesigwa Apr 08 '22 at 11:39
  • `mylink.com` is an invalid link. `https://mylink.com` and `http://mylink.com` are considered valid links. – steven7mwesigwa Apr 08 '22 at 11:43
  • mylink without .com is considered invalid, with this, permit_empty|valid_url will accept (and it should not) – hanafa Apr 08 '22 at 11:53
  • I think you're contradicting yourself. You just previously agreed [in a comment](https://stackoverflow.com/questions/71785960/docker-networking-via-ssl?noredirect=1#comment126875668_71785960) that `mylink` alone should be invalid. – steven7mwesigwa Apr 08 '22 at 12:23
  • Please check again the last lines in my updated post, thank you. – hanafa Apr 08 '22 at 12:47

2 Answers2

0

first

Use separate variable $rules like this :

$rules = ['data' => 'require']

Then

check if $this->request->getVar("instagram"); is empty / is true or etc.. then set it on the $rules

finally

Do Something like this :

$rules = ['extra_data' => 'require'];

if(!empty($this->request->getVar("instagram"))    
$rules["instagram"] = "valid_url";

if ($this->validate(rules){
    //do something ...
}else {
    //do something ...
}
Ali Sheykhi
  • 95
  • 11
0

I just noticed that the following examples: "mylink", "mylink.com", "https://www.mylink.com" will consider correct for the rule valid_url in Codeigniter (No errors), While: "https:://www.mylink.com", "mylink@" will apply the validation and the error is applied.

hanafa
  • 3
  • 3