user3538235
user3538235

Reputation: 2009

Display the validations_error() in codeigniter with bootstrap

Right now there is a form with ~10 fields , and the validation alert is too long, refer to the screen cap

enter image description here

The code is like this

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('description', 'Description', 'required');
    $this->form_validation->set_rules('title_tw', 'Title(繁體)', 'required');
    $this->form_validation->set_rules('description_tw', 'Description(繁體)', 'required');
    $this->form_validation->set_rules('title_cn', 'Title(简体)', 'required');
    $this->form_validation->set_rules('description_cn', 'Description(简体)', 'required');
    $this->form_validation->set_rules('pose', 'Pose', 'required');
    $this->form_validation->set_rules('style', 'Style', 'required');
    $this->form_validation->set_rules('level', 'Level', 'required');
    $this->form_validation->set_rules('type', 'Type', 'required');
    $this->form_validation->set_rules('teacher', 'Teacher', 'required');
    $this->form_validation->set_rules('start_date', 'Start Date', 'required');

    if ($this->input->post('type') != false && $this->input->post('type')[0] == 1) {
        $this->form_validation->set_rules('price', 'Price (USD)', 'required|numeric|greater_than[0]');
        $this->form_validation->set_rules('price_cn', 'Price (RMB)', 'required|numeric|greater_than[0]');
    }

    $this->form_validation->set_error_delimiters('<div class="alert alert-danger"><a class="close" data-dismiss="alert">x</a><strong>', '</strong></div>');

and frontend:

<?= validation_errors; ?>

I would like to reduce the size by using the accordion in bootstrap, so are there any way to override the display of validation?

And How to check if there are e.g. more than 3 error , than show e.g. total 10 errors and use accordion to display the error message?

Thanks a lot.

Upvotes: 0

Views: 1905

Answers (3)

Haritsinh Gohil
Haritsinh Gohil

Reputation: 6272

You have displayed each error separately so it takes so much space in between and don't look so good.

so here i will show you how to display it properly so it looks and feels good but bootstrap is required for it and you are using it so you can use code below in codeigniter.

And You can also close it by pressing close button on right top of the div.

<?php if (!empty(validation_errors())): ?>
    <div class="alert alert-danger">
        <a class="close" data-dismiss="alert" title="close">x</a>
        <ul><?php echo (validation_errors('<li>', '</li>')); ?></ul>
    </div>
<?php endif; ?>

if you are want to see how will it looks then i have a demo output image for you: enter image description here

Upvotes: 0

parth
parth

Reputation: 1868

as per my understanding, your problem is totel height of error, as each error are displayed in different line.

u can display error in one line using below code

<?php if(validation_errors()) { ?>
    <div class="alert alert-danger">
        <a class="close" data-dismiss="alert">x</a>
        <strong><?php echo strip_tags(validation_errors()); ?></strong>
    </div>
<?php } ?>

Upvotes: 1

evrim oku
evrim oku

Reputation: 229

error_array() function returns an array of error messages. So you can count how many error messages there, like;

$errors= error_array();
$numOfErrors = count($errors);

Also you can use form_error() function to show errors respectively to their fields.

<?php echo form_error('title'); ?>
<input type="text" name="title">
<?php echo form_error('title_tw'); ?>
<input type="text" name="title_tw">
...

Upvotes: 1

Related Questions