1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need help with a form PHP, Yii Framework, > < = numbers.

Discussion in 'PHP' started by ElscottHavoc, Nov 6, 2013.

  1. #1
    Basically, on the part that says "
    array('couponPrice,main', 'safe'), I would like the number in the form to be either $0 or at least equal to $10 or greater. In the array just before that one, I figured out how to make the entered number equal to or greater than $0, but what if I want the number to either be equal to 0 or greater than or equal to another number? Does this make sense what I'm asking?

    <?php
    class MDealPriceForm extends MDealPrice
    {
    public $name;

    public static function model($className=__CLASS__)
    {
    return parent::model($className);
    }

    public function rules()
    {
    return array(
    array('name,price,value', 'required'),
    array('value', 'numerical', 'min' => 0.00),
    array('couponPrice,main', 'safe'),
    );
    }

    public function attributeLabels()
    {
    return array(
    'name' => $this->t('Name'),
    'price' => $this->t('Site Pre-Registration Fee (Coupon Cost)'),
    'value' => $this->t('Normal Registration Fee / Offer Value'),
    'couponPrice' => $this->t('Registration Due w/Coupon'),
    );
    }

    public function beforeValidate()
    {
    if(is_array($this->name))
    foreach($this->name as $k=>$v)
    if(!$v)
    unset($this->name[$k]);
    return parent::beforeValidate();
    }
    }
     
    Solved! View solution.
    ElscottHavoc, Nov 6, 2013 IP
  2. ElscottHavoc

    ElscottHavoc Well-Known Member

    Messages:
    76
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    103
    #2
    Anybody???

    Here is the line in questions:

    array('price', 'numerical', 'min' => 0.00),

    Basically, what I want is for the value to be equal to 0 OR to be greater than 5.
     
    Last edited: Nov 9, 2013
    ElscottHavoc, Nov 9, 2013 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    I think you are referring to a type casting a property. correct me if im wrong, you want the value of "safe" inside the
    array('couponPrice,main', 'safe')
    Code (markup):
    to this format '$0'?
     
    bartolay13, Nov 11, 2013 IP
  4. ElscottHavoc

    ElscottHavoc Well-Known Member

    Messages:
    76
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    103
    #4
    I don't think so, but thank you. Currently, I have a form in which a number must be entered. Right now, the number that people enter must simply be equal to or greater than 0.

    But I want to change this slightly. I want the number that's entered to be either equal to 0 or greater than 5. What I don't want is for people to be able to insert a number between .01 and 4.99.
     
    ElscottHavoc, Nov 12, 2013 IP
  5. #5
    Write a custom function in your model ie:

    public function checkNumber() {
      if (($this->price == 0) || ($this->price >= 10)){
      $this->addError('price','Incorrect Price!')
      }
    }
    PHP:
    Then in your model rules add
    array('price', 'checkNumber')
    PHP:
     
    DeanIM, Dec 10, 2013 IP
  6. ElscottHavoc

    ElscottHavoc Well-Known Member

    Messages:
    76
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    103
    #6
    I know this is an extremely delayed reply, but I just wanted to thank you for you answer. It didn't work out exactly as you wrote it, but it gave me a starting point.

    What I ended up doing was using the function you created, but I had to make a slight alteration by changing the fourth line to an else statement.
    public function checkNumber()
        {
            if (($this->price == 0) || ($this->price >= 10));
                else $this->addError('price', 'Coupon costs must either be free or at least $10!');      
        }
    Code (markup):
     
    ElscottHavoc, Jan 12, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    That code doesn't make sense at all - how does that even pass without errors? You're closing the if without anything happening, and... how is that else not triggering an "no starting if-clause"? *confused*
     
    PoPSiCLe, Jan 13, 2014 IP
  8. ElscottHavoc

    ElscottHavoc Well-Known Member

    Messages:
    76
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    103
    #8
    Honestly not sure. I tried as it was originally stated, but then when I typed a number in it'd just give me an error no matter what number I typed in.

    I worked with it for days, took a break for weeks bbecause i didn't understand, and then tried as i wrote it and guarantee it works for my case.

    I'm trying to think of a way to prove its now working as necessary. The only numbers that work are 0 and 10 or greater, else you get an error.

    Could it just be Yii Framework thing because I saw ssimilarly written code elsewhere, which gave me the idea. I don't know much abot yiI frame work, but i agree it seems to be incomplete (if this, then...uh what...else this) but I aassure yo its working as i need it.
     
    Last edited: Jan 13, 2014
    ElscottHavoc, Jan 13, 2014 IP
  9. ElscottHavoc

    ElscottHavoc Well-Known Member

    Messages:
    76
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    103
    #9
    I'll try to do a screen capture video of me using the original code, then the rresults once uploaded, and then show the results as needed per the "oddly written" code.
     
    ElscottHavoc, Jan 13, 2014 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Oh, I don't doubt it works, I'm just wondering HOW it works :) Not being familiar with the Yii framework, but I'm thinking they have some sort of extra parsing involved. If it works for you, great :)
     
    PoPSiCLe, Jan 15, 2014 IP