Welcome Guest, Not a member yet? Register   Sign In
form_validation, styling question
#1

[eluser]questionmark[/eluser]
Hello@all,


how could I add a class value to an input-field if an error occurs?


view:

Code:
&lt;?  echo form_label('Firstname*','firstname'); ?&gt; <br />
&lt;? $data = array('name' => 'vorname', 'id' => 'vorname', 'maxlength' => '50', 'size' => '50');
echo form_input($data); ?&gt;

controller:

Code:
$this->form_validation->set_rules('firstname', 'Firstname', 'required');

i'd like to have this output, if no input for firstname is given ...

Code:
<label for="vorname">Vorname*</label> <br />
&lt;input type="text" name="vorname" class="form-error" value="" id="vorname" maxlength="50" size="50"  /&gt;

so a class value => form-error



Thanks for your help.
#2

[eluser]Dam1an[/eluser]
FIrst of all, I'm not sure if you really need to, is it not sufficient to just show an individual form error?

If you need to (eg if you want a red border on the field) you could maybe try something like
Code:
&lt;input type="text" name="vorname" &lt;?=(form_error('firstname') != '') ? 'class="form-error"' : ''?&gt; /&gt;

You could probably do the same sort of thing with the input helper, but I don't use it, and not going to embarass myself trying Wink
#3

[eluser]questionmark[/eluser]
Hi! Thanks
ok in this way ...

Code:
$data = array('name' => 'firstname', 'id' => 'firstname', 'maxlength' => '50', 'size' => '50');
if (form_error('firstname') != '') $data['class'] = 'form-error';
else unset($data['class']);
echo form_input($data);

but my form_error('firstname') ... is always '' ... here is my controller code ...
Code:
$this->load->library(array('form_validation'));
        $this->load->helper(array('form'));
        if($this->input->post('send')) {
            $this->form_validation->set_rules('firstname', 'Firstname', 'required');
            if ($this->form_validation->run() == FALSE) {
                $this->session->set_flashdata('message', 'wrong');
                redirect('contact/index');
                
            } else {
                $this->session->set_flashdata('message', 'OK');
                redirect('contact/index');
            }
        }
        $this->load->model('Salutation');
        $data['options'] = $this->Salutation->getSalutations ();
        $header['title'] = "Contactform";
        $this->load->view('header', $header);
        $this->load->view('add', $data);
        $this->load->view('footer');

thanks for your help.
#4

[eluser]TheFuzzy0ne[/eluser]
Code:
$data = array('name' => 'vorname', 'id' => 'vorname', 'maxlength' => '50', 'size' => '50');

if (form_error('vorname')) { $data['class'] = 'form-error'; }

echo form_input($data);
#5

[eluser]questionmark[/eluser]
but my form_error(‘firstname’) ... is always false ... why?
#6

[eluser]questionmark[/eluser]
Ok because of this two lines of code ...

Code:
$this->session->set_flashdata('message', 'wrong');
redirect('contact/index');

is this no good way to display an errormessage in combination with form_validation?
#7

[eluser]TheFuzzy0ne[/eluser]
OK, I'm confused. I don't understand where flashdata comes into this, for starters, but I'm more confused about what the input name attributes are in your view, and what you're using in your validation. I see vorname and firstname, which one are you using?
#8

[eluser]questionmark[/eluser]
I use vorname ... but I wanted to post it in english ... firstname, I mixed it up a little bit ;-) sorry! i use now the german version -> vorname

Nevertheless I've fix my problem in this way ... maybe it will be give an better one?

instead of setting flashdata I use a model var $message ...

controller:
Code:
$this->load->library(array('form_validation'));
        $this->load->helper(array('form'));
        $data['message'] = '';
        if($this->input->post('anfrage_send')) {
            $this->form_validation->set_rules('vorname', 'Vorname', 'required');
            if ($this->form_validation->run() == FALSE) {
                $data['message'] = 'Fehler: Bitte &Uuml;berpr&uuml;fen Sie Ihre Eingaben!';
                
            } else {
                $this->session->set_flashdata('message', 'Ihre Kontaktanfrage wurde erflogreich &uuml;bermittelt!');
                redirect('contact/index');
            }
        }
        $this->load->model('Salutation');
        $data['options'] = $this->Salutation->getSalutations ();
        $header['title'] = "Kontaktformular";
        $this->load->view('header', $header);
        $this->load->view('contacts/add', $data);
        $this->load->view('footer');

view:
Code:
...
<tr><td width="50%">
                            <label for="vorname">Vorname*</label> <br />
                            &lt;input type="text" id="vorname" name="vorname"&lt;?=(form_error('vorname') != '') ? ' class="form-error"' : ''?&gt; value="&lt;?=set_value('vorname')?&gt;" size="50" maxlength="50" /&gt;
                      </td></tr>
.....
                      <tr>
<tr><td colspan="2" class="error">&lt;?=($this->session->flashdata('message')) ? $this->session->flashdata('message') : $message;?&gt;</td></tr>
#9

[eluser]TheFuzzy0ne[/eluser]
If you want to change the message, you can override it via ./system/application/language/german/form_validation.php. You'll need to create the directory and the structure and then just make a copy of the english file into the german subdirectory, and then edit it.

You'll then need to set your default language in the config.php file. Ideally, it should have been set up like this to begin with.

Another alternative, might be to us a callback method.

Code:
function valid_vorname($str="")
{
    if ( ! $str) # Check the string is not '', as it's required
    {
        $this->form_validation->set_message('valid_vorname', 'Ihre Kontaktanfrage wurde erflogreich &uuml;bermittelt!');
        return FALSE;
    }
    
    return TRUE;
}

You can also add any further validation you want. Then you can just set your rule like this:

Code:
$this->form_validation->set_rules('vorname', 'Vorname', 'callback_valid_vorname');

Hope this helps.
#10

[eluser]questionmark[/eluser]
thanks@all




Theme © iAndrew 2016 - Forum software by © MyBB