3

I just started learning Yii2 and I have a problem here. I want to set new rule on name field, which would replace all first letters to the uppercase, but I don't know how to set the rule name and I'm constantly getting error messages of it.

Now my model looks like this, I added regex, which is replacing letters to uppercase, but I've no idea how to write that empty string:

['name', 'required'],
        ['name', 'string', 'max' => 255,
            ' ' => '/(^|\s)[a-z]/g'],

Thank you for any help

devorye
  • 193
  • 2
  • 16

1 Answers1

4

For this simple case just use

['name', 'filter', 'filter' => 'ucfirst']

See docs for this one.

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • now I've written all the stuff like this: ` ['name', 'required'], ['name', 'string', 'max' => 255], ['name', 'filter', 'filter' => 'ucfirst'],` but this not helping, sorry for dumb questions, but I'm just trying to learn – devorye Apr 04 '17 at 11:58
  • You must give me more details about this "not helping" here. What do you expect, what do you get instead, and what code are you using to achieve that. – Bizley Apr 04 '17 at 12:00
  • 1
    Meh, it works, but I need to click button 'create' to change it. When I'm typing name and surname, now the name field turns into red color and after clicking 'Create' it's replacing it to the uppercase. Is it possible to set the rule that would automatically change the letter without clicking the button? Thank you! – devorye Apr 04 '17 at 12:03
  • Well, yes, validation rules are applied when the field is being validated obviously. For the automatic change while you type you need to add JS that will do this. – Bizley Apr 04 '17 at 12:06
  • Yea, I did the same task without framework and I did it with JS, so I was just thinking maybe is it possible to do that with only just framework, but now it's clear. Thank you for your help! – devorye Apr 04 '17 at 12:10